Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appengine/standard_python37/bigquery/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: python37
43 changes: 43 additions & 0 deletions appengine/standard_python37/bigquery/main.py
Original file line number Diff line number Diff line change
@@ -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]
24 changes: 24 additions & 0 deletions appengine/standard_python37/bigquery/main_test.py
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 2 additions & 0 deletions appengine/standard_python37/bigquery/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-cloud-bigquery==1.6.0
Flask==1.0.2
21 changes: 21 additions & 0 deletions appengine/standard_python37/bigquery/templates/query_result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Query Result</title>
</head>
<body>
<table>
<tr>
<th>URL</th>
<th>View Count</th>
</tr>
{% for result in results %}
<tr>
<td>{{ result[0] }}</td>
<td>{{ result[1] }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>