Skip to content

Commit 3e28b78

Browse files
authored
Add an App Engine Flexiable Python application. It is to demonstrate … (#9191)
* Add an App Engine Flexiable Python application. It is to demonstrate the newly released Python 3.10 runtime on Ubuntu 22. Documentation changes have been published to https://cloud.google.com/appengine/docs/flexible/python/runtime. * Update copyright holder and license header. * Attempt to fix nox tests. * Add python type annotation. * Update the license header.
1 parent a2ae0ba commit 3e28b78

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed

appengine/flexible_ubuntu/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Google App Engine Flexible Environment Python on Ubuntu Samples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=appengine/flexible_ubuntu/README.md
7+
8+
These are samples for using Python on Google App Engine Flexible Environment on the Ubuntu 18 and Ubuntu 22 OS. These samples are typically referenced from the [docs](https://cloud.google.com/appengine/docs).
9+
10+
See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform) for sample applications and
11+
scaffolding for other frameworks and use cases.
12+
13+
## Run Locally
14+
15+
Some samples have specific instructions. If there is a README in the sample folder, please refer to it for any additional steps required to run the sample.
16+
17+
In general, the samples typically require:
18+
19+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
20+
21+
2. Setup the gcloud tool. This provides authentication to Google Cloud APIs and services.
22+
23+
```
24+
gcloud init
25+
```
26+
27+
3. Clone this repo.
28+
29+
```
30+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
31+
cd python-docs-samples/appengine/flexible
32+
```
33+
34+
4. Open a sample folder, create a virtualenv, install dependencies, and run the sample:
35+
36+
```
37+
cd hello-world
38+
virtualenv env
39+
source env/bin/activate
40+
pip install -r requirements.txt
41+
python main.py
42+
```
43+
44+
5. Visit the application at [http://localhost:8080](http://localhost:8080).
45+
46+
47+
## Deploying
48+
49+
Some samples in this repositories may have special deployment instructions. Refer to the readme in the sample directory.
50+
51+
1. Use the [Google Developers Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
52+
53+
2. Setup the gcloud tool, if you haven't already.
54+
55+
```
56+
gcloud init
57+
```
58+
59+
3. Use gcloud to deploy your app. Your application must use a `gcloud` CLI
60+
[version **420.0.0 or later**](/sdk/docs/install-sdk#installing_the_latest_version).
61+
To view the current `gcloud` version, run the [`gcloud
62+
version`](/sdk/gcloud/reference/version) command.
63+
64+
```
65+
gcloud app deploy
66+
```
67+
68+
4. Congratulations! Your application is now live at `your-app-id.appspot.com`
69+
70+
## Contributing changes
71+
72+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
73+
74+
## Licensing
75+
76+
* See [LICENSE](../LICENSE)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2021 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+
runtime: python
16+
env: flex
17+
entrypoint: gunicorn -b :$PORT main:app
18+
19+
runtime_config:
20+
operating_system: ubuntu22
21+
# We also support 3.8, 3.9 and 3.11.
22+
# See https://cloud.google.com/appengine/docs/flexible/python/runtime#interpreter for more info.
23+
runtime_version: "3.10"
24+
25+
# This sample incurs costs to run on the App Engine flexible environment.
26+
# The settings below are to reduce costs during testing and are not appropriate
27+
# for production use. For more information, see:
28+
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
29+
manual_scaling:
30+
instances: 1
31+
resources:
32+
cpu: 1
33+
memory_gb: 0.5
34+
disk_size_gb: 10
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2023 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_flex_quickstart]
16+
from flask import Flask
17+
18+
19+
app = Flask(__name__)
20+
21+
22+
@app.route('/')
23+
def hello() -> str:
24+
"""Return a friendly HTTP greeting."""
25+
return 'Hello World!'
26+
27+
28+
if __name__ == '__main__':
29+
# This is used when running locally only. When deploying to Google App
30+
# Engine, a webserver process such as Gunicorn will serve the app.
31+
app.run(host='127.0.0.1', port=8080, debug=True)
32+
# [END gae_flex_quickstart]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 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+
import main
16+
17+
18+
def test_index() -> str:
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Hello World' in r.data.decode('utf-8')
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.6"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==6.2.4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==2.1.1
2+
gunicorn==20.1.0

0 commit comments

Comments
 (0)