diff --git a/appengine/standard_python37/bigquery/app.yaml b/appengine/standard_python37/bigquery/app.yaml new file mode 100644 index 00000000000..a0b719d6dd4 --- /dev/null +++ b/appengine/standard_python37/bigquery/app.yaml @@ -0,0 +1 @@ +runtime: python37 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 @@ + + +
+ +| URL | +View Count | +
|---|---|
| {{ result[0] }} | +{{ result[1] }} | +