15
15
# [START gae_python37_bigquery]
16
16
import concurrent .futures
17
17
18
- from flask import Flask , render_template
18
+ import flask
19
19
from google .cloud import bigquery
20
20
21
21
22
- app = Flask (__name__ )
22
+ app = flask . Flask (__name__ )
23
23
bigquery_client = bigquery .Client ()
24
24
25
25
26
- @app .route ('/' )
26
+ @app .route ("/" )
27
27
def main ():
28
- query_job = bigquery_client .query ("""
28
+ query_job = bigquery_client .query (
29
+ """
29
30
SELECT
30
31
CONCAT(
31
32
'https://stackoverflow.com/questions/',
@@ -35,20 +36,43 @@ def main():
35
36
WHERE tags like '%google-bigquery%'
36
37
ORDER BY view_count DESC
37
38
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
+ )
39
63
40
64
try :
41
65
# Set a timeout because queries could take longer than one minute.
42
66
results = query_job .result (timeout = 30 )
43
67
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 )
45
69
46
- return render_template (' query_result.html' , results = results )
70
+ return flask . render_template (" query_result.html" , results = results )
47
71
48
72
49
- if __name__ == ' __main__' :
73
+ if __name__ == " __main__" :
50
74
# This is used when running locally only. When deploying to Google App
51
75
# Engine, a webserver process such as Gunicorn will serve the app. This
52
76
# 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 )
54
78
# [END gae_python37_bigquery]
0 commit comments