From 776e5fae12432da7c08d929ddfc1f34764ded016 Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Thu, 11 Oct 2018 16:54:32 -0700 Subject: [PATCH 1/3] Add sample for using BigQuery in App Engine Python 3.7 runtime --- appengine/standard_python37/bigquery/main.py | 43 +++++++++++++++++++ .../standard_python37/bigquery/main_test.py | 24 +++++++++++ .../bigquery/requirements.txt | 2 + .../bigquery/templates/query_result.html | 21 +++++++++ 4 files changed, 90 insertions(+) create mode 100644 appengine/standard_python37/bigquery/main.py create mode 100644 appengine/standard_python37/bigquery/main_test.py create mode 100644 appengine/standard_python37/bigquery/requirements.txt create mode 100644 appengine/standard_python37/bigquery/templates/query_result.html diff --git a/appengine/standard_python37/bigquery/main.py b/appengine/standard_python37/bigquery/main.py new file mode 100644 index 00000000000..c8a7117cf59 --- /dev/null +++ b/appengine/standard_python37/bigquery/main.py @@ -0,0 +1,43 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START gae_python37_bigquery] +from flask import Flask, render_template +from google.cloud import bigquery + +app = Flask(__name__) +bigquery_client = bigquery.Client() + + +@app.route('/') +def main(): + query_job = bigquery_client.query(""" + SELECT + CONCAT( + 'https://stackoverflow.com/questions/', + CAST(id as STRING)) as url, + view_count + FROM `bigquery-public-data.stackoverflow.posts_questions` + WHERE tags like '%google-bigquery%' + ORDER BY view_count DESC + LIMIT 10 + """) + + results = query_job.result() + return render_template('query_result.html', results=results) + + +if __name__ == '__main__': + app.run(debug=True) +# [END gae_python37_bigquery] diff --git a/appengine/standard_python37/bigquery/main_test.py b/appengine/standard_python37/bigquery/main_test.py new file mode 100644 index 00000000000..727f0c1533c --- /dev/null +++ b/appengine/standard_python37/bigquery/main_test.py @@ -0,0 +1,24 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def test_main(): + import main + + main.app.testing = True + client = main.app.test_client() + + r = client.get('/') + assert r.status_code == 200 + assert 'Query Result' in r.data.decode('utf-8') diff --git a/appengine/standard_python37/bigquery/requirements.txt b/appengine/standard_python37/bigquery/requirements.txt new file mode 100644 index 00000000000..b3050e0bf8e --- /dev/null +++ b/appengine/standard_python37/bigquery/requirements.txt @@ -0,0 +1,2 @@ +google-cloud-bigquery==1.6.0 +Flask==1.0.2 diff --git a/appengine/standard_python37/bigquery/templates/query_result.html b/appengine/standard_python37/bigquery/templates/query_result.html new file mode 100644 index 00000000000..0213cb2c6cf --- /dev/null +++ b/appengine/standard_python37/bigquery/templates/query_result.html @@ -0,0 +1,21 @@ + + + + + Query Result + + + + + + + + {% for result in results %} + + + + + {% endfor %} +
URLView Count
{{ result[0] }}{{ result[1] }}
+ + \ No newline at end of file From 76aaed8f0b600623b229053a805f34293b10d65d Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Fri, 12 Oct 2018 15:57:55 -0700 Subject: [PATCH 2/3] Added app.yaml. --- appengine/standard_python37/bigquery/app.yaml | 1 + 1 file changed, 1 insertion(+) create mode 100644 appengine/standard_python37/bigquery/app.yaml diff --git a/appengine/standard_python37/bigquery/app.yaml b/appengine/standard_python37/bigquery/app.yaml new file mode 100644 index 00000000000..6ae7e5ade33 --- /dev/null +++ b/appengine/standard_python37/bigquery/app.yaml @@ -0,0 +1 @@ +runtime: python37 \ No newline at end of file From c4dbb546a445eb025455862060416522ca96af52 Mon Sep 17 00:00:00 2001 From: michaelawyu Date: Mon, 22 Oct 2018 13:11:32 -0700 Subject: [PATCH 3/3] Minor fix. --- appengine/standard_python37/bigquery/app.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appengine/standard_python37/bigquery/app.yaml b/appengine/standard_python37/bigquery/app.yaml index 6ae7e5ade33..a0b719d6dd4 100644 --- a/appengine/standard_python37/bigquery/app.yaml +++ b/appengine/standard_python37/bigquery/app.yaml @@ -1 +1 @@ -runtime: python37 \ No newline at end of file +runtime: python37