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
12 changes: 6 additions & 6 deletions appengine/flexible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

These are samples for using Python on Google App Engine Flexible Environment. These samples are typically referenced from the [docs](https://cloud.google.com/appengine/docs).

For code samples of Python version 3.7 and earlier, please check
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/appengine/flexible.

See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform) for sample applications and
scaffolding for other frameworks and use cases.

Expand All @@ -31,12 +34,9 @@ In general, the samples typically require:
cd python-docs-samples/appengine/flexible
```

4. Open a sample folder, create a virtualenv, install dependencies, and run the sample:
4. Follow https://cloud.google.com/python/docs/setup to set up a Python development environment. Then run:

```
cd hello-world
virtualenv env
source env/bin/activate
pip install -r requirements.txt
python main.py
```
Expand Down Expand Up @@ -66,8 +66,8 @@ Some samples in this repositories may have special deployment instructions. Refe

## Contributing changes

* See [CONTRIBUTING.md](../CONTRIBUTING.md)
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../LICENSE)
* See [LICENSE](../../LICENSE)
4 changes: 2 additions & 2 deletions appengine/flexible/hello_world/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3
operating_system: ubuntu22

# This sample incurs costs to run on the App Engine flexible environment.
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
Expand Down
4 changes: 2 additions & 2 deletions appengine/flexible/hello_world/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google LLC.
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@


@app.route('/')
def hello():
def hello() -> str:
"""Return a friendly HTTP greeting."""
return 'Hello World!'

Expand Down
4 changes: 2 additions & 2 deletions appengine/flexible/hello_world/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google LLC.
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
import main


def test_index():
def test_index() -> str:
main.app.testing = True
client = main.app.test_client()

Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/hello_world/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest==7.0.1
pytest==6.2.4
3 changes: 1 addition & 2 deletions appengine/flexible/hello_world/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Flask==2.1.0; python_version > '3.6'
Flask==2.0.3; python_version < '3.7'
Flask==2.1.1
gunicorn==20.1.0
5 changes: 2 additions & 3 deletions appengine/flexible/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ When running locally, you can use the [Google Cloud SDK](https://cloud.google.co

$ gcloud init

Install dependencies, preferably with a virtualenv:
Install dependencies, please follow https://cloud.google.com/python/docs/setup
to set up a Python development environment. Then run:

$ virtualenv env
$ source env/bin/activate
$ pip install -r requirements.txt

Then set environment variables before starting your application:
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/pubsub/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3
operating_system: "ubuntu22"

# [START gae_flex_pubsub_env]
env_variables:
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/static_files/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3
operating_system: "ubuntu22"
4 changes: 2 additions & 2 deletions appengine/flexible/storage/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3
operating_system: "ubuntu22"

#[START gae_flex_storage_yaml]
env_variables:
CLOUD_STORAGE_BUCKET: your-bucket-name
CLOUD_STORAGE_BUCKET: [your-bucket-name]
#[END gae_flex_storage_yaml]
30 changes: 17 additions & 13 deletions appengine/flexible/storage/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google LLC.
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -23,10 +23,10 @@
app = Flask(__name__)

# Configure this environment variable via app.yaml
CLOUD_STORAGE_BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
CLOUD_STORAGE_BUCKET = os.environ["CLOUD_STORAGE_BUCKET"]


@app.route('/')
@app.route("/")
def index() -> str:
return """
<form method="POST" action="/upload" enctype="multipart/form-data">
Expand All @@ -36,13 +36,13 @@ def index() -> str:
"""


@app.route('/upload', methods=['POST'])
@app.route("/upload", methods=["POST"])
def upload() -> str:
"""Process the uploaded file and upload it to Google Cloud Storage."""
uploaded_file = request.files.get('file')
uploaded_file = request.files.get("file")

if not uploaded_file:
return 'No file uploaded.', 400
return "No file uploaded.", 400

# Create a Cloud Storage client.
gcs = storage.Client()
Expand All @@ -54,8 +54,7 @@ def upload() -> str:
blob = bucket.blob(uploaded_file.filename)

blob.upload_from_string(
uploaded_file.read(),
content_type=uploaded_file.content_type
uploaded_file.read(), content_type=uploaded_file.content_type
)

# Make the blob public. This is not necessary if the
Expand All @@ -69,15 +68,20 @@ def upload() -> str:

@app.errorhandler(500)
def server_error(e: Union[Exception, int]) -> str:
logging.exception('An error occurred during a request.')
return """
logging.exception("An error occurred during a request.")
return (
"""
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
""".format(
e
),
500,
)


if __name__ == '__main__':
if __name__ == "__main__":
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
app.run(host="127.0.0.1", port=8080, debug=True)
# [END gae_flex_storage_app]
18 changes: 7 additions & 11 deletions appengine/flexible/storage/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google LLC.
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,15 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import os
import uuid

import flask
import flask.testing
from google.cloud import storage
import pytest

import requests
from six import BytesIO

import main

Expand All @@ -32,7 +33,7 @@ def client() -> flask.testing.FlaskClient:


def test_index(client: flask.testing.FlaskClient) -> None:
r = client.get('/')
r = client.get("/")
assert r.status_code == 200


Expand All @@ -50,17 +51,12 @@ def test_upload(client: flask.testing.FlaskClient, blob_name: str) -> None:
# Upload a simple file
file_content = b"This is some test content."

r = client.post(
'/upload',
data={
'file': (BytesIO(file_content), blob_name)
}
)
r = client.post("/upload", data={"file": (io.BytesIO(file_content), blob_name)})

assert r.status_code == 200

# The app should return the public cloud storage URL for the uploaded
# file. Download and verify it.
cloud_storage_url = r.data.decode('utf-8')
cloud_storage_url = r.data.decode("utf-8")
r = requests.get(cloud_storage_url)
assert r.text.encode('utf-8') == file_content
assert r.text.encode("utf-8") == file_content
2 changes: 1 addition & 1 deletion appengine/flexible/storage/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7"],
"ignored_versions": ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10"],
Comment thread
jinglundong marked this conversation as resolved.
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": True,
Expand Down
3 changes: 1 addition & 2 deletions appengine/flexible/storage/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pytest==7.0.1
google-cloud-storage==2.0.0; python_version < '3.7'
google-cloud-storage==2.1.0; python_version > '3.6'
google-cloud-storage==2.1.0;
12 changes: 4 additions & 8 deletions appengine/flexible/storage/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
Flask==2.1.0; python_version > '3.6'
Flask==2.0.3; python_version < '3.7'
werkzeug==2.1.2; python_version > '3.6'
werkzeug==2.0.3; python_version < '3.7'
google-cloud-storage==2.0.0; python_version < '3.7'
google-cloud-storage==2.1.0; python_version > '3.6'
gunicorn==20.1.0; python_version > '3.0'
gunicorn==19.10.0; python_version < '3.0'
Flask==2.1.0;
werkzeug==2.1.2;
google-cloud-storage==2.1.0;
gunicorn==20.1.0;
11 changes: 8 additions & 3 deletions appengine/flexible/tasks/app.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Copyright 2019 Google LLC.
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
# 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.

runtime: python37
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
operating_system: "ubuntu22"
2 changes: 1 addition & 1 deletion appengine/flexible/tasks/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
<body>
<p>Printed task payload: {{ payload }}</p>
</body>
</html>
</html>
76 changes: 0 additions & 76 deletions appengine/flexible_ubuntu/README.md

This file was deleted.

Loading