1515# [START gae_python37_bigquery]
1616import concurrent .futures
1717
18- from flask import Flask , render_template
18+ import flask
1919from google .cloud import bigquery
2020
2121
22- app = Flask (__name__ )
22+ app = flask . Flask (__name__ )
2323bigquery_client = bigquery .Client ()
2424
2525
26- @app .route ('/' )
26+ @app .route ("/" )
2727def main ():
28- query_job = bigquery_client .query ("""
28+ query_job = bigquery_client .query (
29+ """
2930 SELECT
3031 CONCAT(
3132 'https://stackoverflow.com/questions/',
@@ -35,20 +36,43 @@ def main():
3536 WHERE tags like '%google-bigquery%'
3637 ORDER BY view_count DESC
3738 LIMIT 10
38- """ )
39+ """
40+ )
41+
42+ return flask .redirect (
43+ flask .url_for (
44+ "results" ,
45+ project_id = query_job .project ,
46+ job_id = query_job .job_id ,
47+ location = query_job .location ,
48+ )
49+ )
50+
51+
52+ @app .route ("/results" )
53+ def results ():
54+ project_id = flask .request .args .get ("project_id" )
55+ job_id = flask .request .args .get ("job_id" )
56+ location = flask .request .args .get ("location" )
57+
58+ query_job = bigquery_client .get_job (
59+ job_id ,
60+ project = project_id ,
61+ location = location ,
62+ )
3963
4064 try :
4165 # Set a timeout because queries could take longer than one minute.
4266 results = query_job .result (timeout = 30 )
4367 except concurrent .futures .TimeoutError :
44- return render_template (' timeout.html' , job_id = query_job .job_id )
68+ return flask . render_template (" timeout.html" , job_id = query_job .job_id )
4569
46- return render_template (' query_result.html' , results = results )
70+ return flask . render_template (" query_result.html" , results = results )
4771
4872
49- if __name__ == ' __main__' :
73+ if __name__ == " __main__" :
5074 # This is used when running locally only. When deploying to Google App
5175 # Engine, a webserver process such as Gunicorn will serve the app. This
5276 # can be configured by adding an `entrypoint` to app.yaml.
53- app .run (host = ' 127.0.0.1' , port = 8080 , debug = True )
77+ app .run (host = " 127.0.0.1" , port = 8080 , debug = True )
5478# [END gae_python37_bigquery]
0 commit comments