Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ca5565

Browse filesBrowse files
author
chenyumic
authored
Add sample for using BigQuery in App Engine Python 3.7 runtime (GoogleCloudPlatform#1752)
* Add sample for using BigQuery in App Engine Python 3.7 runtime * Added app.yaml. * Minor fix.
1 parent 423a6e6 commit 1ca5565
Copy full SHA for 1ca5565

File tree

Expand file treeCollapse file tree

5 files changed

+91
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+91
-0
lines changed
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: python37
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START gae_python37_bigquery]
16+
from flask import Flask, render_template
17+
from google.cloud import bigquery
18+
19+
app = Flask(__name__)
20+
bigquery_client = bigquery.Client()
21+
22+
23+
@app.route('/')
24+
def main():
25+
query_job = bigquery_client.query("""
26+
SELECT
27+
CONCAT(
28+
'https://stackoverflow.com/questions/',
29+
CAST(id as STRING)) as url,
30+
view_count
31+
FROM `bigquery-public-data.stackoverflow.posts_questions`
32+
WHERE tags like '%google-bigquery%'
33+
ORDER BY view_count DESC
34+
LIMIT 10
35+
""")
36+
37+
results = query_job.result()
38+
return render_template('query_result.html', results=results)
39+
40+
41+
if __name__ == '__main__':
42+
app.run(debug=True)
43+
# [END gae_python37_bigquery]
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_main():
17+
import main
18+
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Query Result' in r.data.decode('utf-8')
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-bigquery==1.6.0
2+
Flask==1.0.2
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype <!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Query Result</title>
6+
</head>
7+
<body>
8+
<table>
9+
<tr>
10+
<th>URL</th>
11+
<th>View Count</th>
12+
</tr>
13+
{% for result in results %}
14+
<tr>
15+
<td>{{ result[0] }}</td>
16+
<td>{{ result[1] }}</td>
17+
</tr>
18+
{% endfor %}
19+
</table>
20+
</body>
21+
</html>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.