-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
106 changed files
with
2,615 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
runtime: python38 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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_python38_bigquery] | ||
import concurrent.futures | ||
|
||
import flask | ||
from google.cloud import bigquery | ||
|
||
|
||
app = flask.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 | ||
""" | ||
) | ||
|
||
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) | ||
|
||
|
||
if __name__ == "__main__": | ||
# This is used when running locally only. When deploying to Google App | ||
# Engine, a webserver process such as Gunicorn will serve the app. This | ||
# can be configured by adding an `entrypoint` to app.yaml. | ||
app.run(host="127.0.0.1", port=8080, debug=True) | ||
# [END gae_python38_bigquery] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# 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. | ||
|
||
import concurrent.futures | ||
from unittest import mock | ||
|
||
from google.cloud import bigquery | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def flask_client(): | ||
import main | ||
|
||
main.app.testing = True | ||
return main.app.test_client() | ||
|
||
|
||
def test_main(flask_client): | ||
r = flask_client.get("/") | ||
assert r.status_code == 302 | ||
assert "/results" in r.headers.get("location", "") | ||
|
||
|
||
def test_results(flask_client, monkeypatch): | ||
import main | ||
|
||
fake_job = mock.create_autospec(bigquery.QueryJob) | ||
fake_rows = [("example1.com", "42"), ("example2.com", "38")] | ||
fake_job.result.return_value = fake_rows | ||
|
||
def fake_get_job(self, job_id, **kwargs): | ||
return fake_job | ||
|
||
monkeypatch.setattr(main.bigquery.Client, "get_job", fake_get_job) | ||
|
||
r = flask_client.get( | ||
"/results?project_id=123&job_id=456&location=my_location" | ||
) | ||
response_body = r.data.decode("utf-8") | ||
|
||
assert r.status_code == 200 | ||
assert "Query Result" in response_body # verifies header | ||
assert "example2.com" in response_body | ||
assert "42" in response_body | ||
|
||
|
||
def test_results_timeout(flask_client, monkeypatch): | ||
import main | ||
|
||
fake_job = mock.create_autospec(bigquery.QueryJob) | ||
fake_job.result.side_effect = concurrent.futures.TimeoutError() | ||
|
||
def fake_get_job(self, job_id, **kwargs): | ||
return fake_job | ||
|
||
monkeypatch.setattr(main.bigquery.Client, "get_job", fake_get_job) | ||
|
||
r = flask_client.get("/results", follow_redirects=True) | ||
|
||
assert r.status_code == 200 | ||
assert "Query Timeout" in r.data.decode("utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest==5.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
google-cloud-bigquery==1.24.0 | ||
Flask==1.1.2 |
31 changes: 31 additions & 0 deletions
31
appengine/standard_python3/bigquery/templates/query_result.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
{# | ||
Copyright 2019 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 | ||
|
||
https://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. | ||
#} | ||
<meta charset="utf-8" /> | ||
<title>Query Result</title> | ||
|
||
<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> |
20 changes: 20 additions & 0 deletions
20
appengine/standard_python3/bigquery/templates/timeout.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
{# | ||
Copyright 2019 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 | ||
|
||
https://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. | ||
#} | ||
<meta charset="utf-8" /> | ||
<title>Query Timeout</title> | ||
|
||
<p>Query job {{ job_id }} timed out. |
13 changes: 13 additions & 0 deletions
13
appengine/standard_python3/building-an-app/building-an-app-1/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
runtime: python38 | ||
|
||
handlers: | ||
# This configures Google App Engine to serve the files in the app's static | ||
# directory. | ||
- url: /static | ||
static_dir: static | ||
|
||
# This handler routes all requests not caught above to your main app. It is | ||
# required when static routes are defined, but can be omitted (along with | ||
# the entire handlers section) when there are no static files defined. | ||
- url: /.* | ||
script: auto |
44 changes: 44 additions & 0 deletions
44
appengine/standard_python3/building-an-app/building-an-app-1/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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_python38_render_template] | ||
import datetime | ||
|
||
from flask import Flask, render_template | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def root(): | ||
# For the sake of example, use static information to inflate the template. | ||
# This will be replaced with real information in later steps. | ||
dummy_times = [datetime.datetime(2018, 1, 1, 10, 0, 0), | ||
datetime.datetime(2018, 1, 2, 10, 30, 0), | ||
datetime.datetime(2018, 1, 3, 11, 0, 0), | ||
] | ||
|
||
return render_template('index.html', times=dummy_times) | ||
|
||
|
||
if __name__ == '__main__': | ||
# This is used when running locally only. When deploying to Google App | ||
# Engine, a webserver process such as Gunicorn will serve the app. This | ||
# can be configured by adding an `entrypoint` to app.yaml. | ||
# Flask's development server will automatically serve static files in | ||
# the "static" directory. See: | ||
# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed, | ||
# App Engine itself will serve those files as configured in app.yaml. | ||
app.run(host='127.0.0.1', port=8080, debug=True) | ||
# [START gae_python38_render_template] |
23 changes: 23 additions & 0 deletions
23
appengine/standard_python3/building-an-app/building-an-app-1/main_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2015 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import main | ||
|
||
|
||
def test_index(): | ||
main.app.testing = True | ||
client = main.app.test_client() | ||
|
||
r = client.get('/') | ||
assert r.status_code == 200 |
1 change: 1 addition & 0 deletions
1
appengine/standard_python3/building-an-app/building-an-app-1/requirements-test.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest==5.3.2 |
1 change: 1 addition & 0 deletions
1
appengine/standard_python3/building-an-app/building-an-app-1/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Flask==1.1.2 |
24 changes: 24 additions & 0 deletions
24
appengine/standard_python3/building-an-app/building-an-app-1/static/script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
// [START gae_python38_log] | ||
'use strict'; | ||
|
||
window.addEventListener('load', function () { | ||
|
||
console.log("Hello World!"); | ||
|
||
}); | ||
// [END gae_python38_log] |
4 changes: 4 additions & 0 deletions
4
appengine/standard_python3/building-an-app/building-an-app-1/static/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
body { | ||
font-family: "helvetica", sans-serif; | ||
text-align: center; | ||
} |
18 changes: 18 additions & 0 deletions
18
appengine/standard_python3/building-an-app/building-an-app-1/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Datastore and Firebase Auth Example</title> | ||
<script src="{{ url_for('static', filename='script.js') }}"></script> | ||
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> | ||
</head> | ||
<body> | ||
|
||
<h1>Datastore and Firebase Auth Example</h1> | ||
|
||
<h2>Last 10 visits</h2> | ||
{% for time in times %} | ||
<p>{{ time }}</p> | ||
{% endfor %} | ||
|
||
</body> | ||
</html> |
13 changes: 13 additions & 0 deletions
13
appengine/standard_python3/building-an-app/building-an-app-2/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
runtime: python38 | ||
|
||
handlers: | ||
# This configures Google App Engine to serve the files in the app's static | ||
# directory. | ||
- url: /static | ||
static_dir: static | ||
|
||
# This handler routes all requests not caught above to your main app. It is | ||
# required when static routes are defined, but can be omitted (along with | ||
# the entire handlers section) when there are no static files defined. | ||
- url: /.* | ||
script: auto |
Oops, something went wrong.