Skip to content

Commit

Permalink
Gae python version update (#3699)
Browse files Browse the repository at this point in the history
  • Loading branch information
engelke authored May 14, 2020
1 parent ec30b6a commit 2e502b1
Show file tree
Hide file tree
Showing 106 changed files with 2,615 additions and 0 deletions.
1 change: 1 addition & 0 deletions appengine/standard_python3/bigquery/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: python38
78 changes: 78 additions & 0 deletions appengine/standard_python3/bigquery/main.py
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]
73 changes: 73 additions & 0 deletions appengine/standard_python3/bigquery/main_test.py
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")
1 change: 1 addition & 0 deletions appengine/standard_python3/bigquery/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==5.3.2
2 changes: 2 additions & 0 deletions appengine/standard_python3/bigquery/requirements.txt
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 appengine/standard_python3/bigquery/templates/query_result.html
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 appengine/standard_python3/bigquery/templates/timeout.html
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.
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
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]
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==5.3.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==1.1.2
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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
font-family: "helvetica", sans-serif;
text-align: center;
}
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>
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
Loading

0 comments on commit 2e502b1

Please sign in to comment.