I am learning Google App Engine and created a simple app that queries a table on BigQuery and returns an HTML table.
Relevant pieces:
@app.route("/")
def main():
query_job = bigquery_client.query(
"""
SELECT DISTINCT *
FROM github_project.labeled_data_dev
WHERE cluster = 1
ORDER BY commits DESC
LIMIT 10
"""
)
return flask.redirect(
flask.url_for(
"results",
project_id=query_job.project,
job_id=query_job.job_id,
location=query_job.location,
)
)
@app.route("/results")
def results():
project_id = flask.request.args.get("project_id")
job_id = flask.request.args.get("job_id")
location = flask.request.args.get("location")
query_job = bigquery_client.get_job(
job_id,
project=project_id,
location=location,
)
try:
# Set a timeout because queries could take longer than one minute.
results = query_job.result(timeout=30)
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)
I have a few variations on the above query that I would like to also return (changing the WHERE, etc).
Editing in some more information, per request.
The above script runs the query and returns a simple table. What I am after is a modification that will return multiple tables.
I have tried to edit the main script, as follows:
@app.route("/")
def main():
query_job_1 = bigquery_client.query(
"""
SELECT DISTINCT *
FROM github_project.labeled_data_dev
WHERE cluster = 1
ORDER BY commits DESC
LIMIT 10
"""
)
query1 = flask.redirect(
flask.url_for(
"results",
project_id=query_job.project,
job_id=query_job.job_id,
location=query_job.location,
)
)
query_job_2 = bigquery_client.query(
"""
SELECT DISTINCT *
FROM github_project.labeled_data_dev
WHERE cluster = 2
ORDER BY commits DESC
LIMIT 10
"""
)
query2 = flask.redirect(
flask.url_for(
"results",
project_id=query_job.project,
job_id=query_job.job_id,
location=query_job.location,
)
)
return query1, query2
The idea was to then be able to call both query1 and query2 in the @app.route("/results") section and return results1, results2 which could then be used in my html template, but this doesn't work.
I believe I could potentially use a class, like the one created in the second answer here using sqlite3, but I am unsure how to rewrite it with my BigQuery connections in mind.
Any help would be greatly appreciated.