-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (39 loc) · 1.73 KB
/
main.py
File metadata and controls
58 lines (39 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import concurrent.futures
import flask
from google.cloud import bigquery
app = flask.Flask(__name__)
bigquery_client = bigquery.Client()
import logging
logging.basicConfig(level=logging.DEBUG)
@app.route("/")
def main():
return flask.render_template("home.html")
@app.route("/results")
def results():
ingredient_query = flask.request.args.get("ingredient")
query = """
WITH dat AS (SELECT name, steps, SPLIT(ingredients_clean, "|") as ingredients FROM `plenary-line-305512.flaska_data.recipes_raw` WHERE DATE(_PARTITIONTIME) = "2022-06-18" )
SELECT name, ingredients FROM dat, UNNEST(ingredients) ingredient WHERE CONTAINS_SUBSTR(ingredient, @ingredient) LIMIT 20
"""
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("ingredient", "STRING", ingredient_query),
]
)
query_job = bigquery_client.query(query, job_config=job_config)
# Make an API request.
try:
# Set a timeout because queries could take longer than one minute.
results = query_job.result(timeout=30)
results_rows = [row for row in results]
print("\nresults:{}".format(results_rows))
except concurrent.futures.TimeoutError:
return flask.render_template("timeout.html", job_id=query_job.job_id)
return flask.render_template("query_result.html", results=results_rows)
if __name__ == "__main__":
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="127.0.0.1", port=8000, debug=True)
# [END gae_python3_bigquery]
# [END gae_python38_bigquery]