Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiquanMah authored Dec 17, 2024
1 parent c7fd753 commit a0ba486
Show file tree
Hide file tree
Showing 86 changed files with 3,077 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Google App Engine Flexible Environment Python Samples

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[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/README.md

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.

## Run Locally

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.

In general, the samples typically require:

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).

2. Setup the gcloud tool. This provides authentication to Google Cloud APIs and services.

```
gcloud init
```

3. Clone this repo.

```
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
cd python-docs-samples/appengine/flexible
```

4. Follow https://cloud.google.com/python/docs/setup to set up a Python development environment. Then run:

```
pip install -r requirements.txt
python main.py
```

5. Visit the application at [http://localhost:8080](http://localhost:8080).


## Deploying

Some samples in this repositories may have special deployment instructions. Refer to the readme in the sample directory.

1. Use the [Google Developers Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)

2. Setup the gcloud tool, if you haven't already.

```
gcloud init
```

3. Use gcloud to deploy your app.

```
gcloud app deploy
```

4. Congratulations! Your application is now live at `your-app-id.appspot.com`

## Contributing changes

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

## Licensing

* See [LICENSE](../../LICENSE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 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.

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

runtime_config:
operating_system: ubuntu22

# 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
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.
# 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_flex_quickstart]
from flask import Flask


app = Flask(__name__)


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


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.
app.run(host="127.0.0.1", port=8080, debug=True)
# [END gae_flex_quickstart]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.
# 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() -> str:
main.app.testing = True
client = main.app.test_client()

r = client.get("/")
assert r.status_code == 200
assert "Hello World" in r.data.decode("utf-8")
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021 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.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7", "3.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": True,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==8.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Flask==3.0.3; python_version > '3.6'
Flask==2.3.3; python_version < '3.7'
Werkzeug==3.0.3; python_version > '3.6'
Werkzeug==2.3.7; python_version < '3.7'
gunicorn==22.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Django sample for Google App Engine Flexible Environment

[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[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/hello_world_django/README.md

This is a basic hello world [Django](https://www.djangoproject.com/) example
for [Google App Engine Flexible Environment](https://cloud.google.com/appengine).

## Running locally

You can run locally using django's `manage.py`:

$ python manage.py runserver

## Deployment & how the application runs on Google App Engine.

Follow the standard deployment instructions in
[the top-level README](../README.md). Google App Engine runs the application
using [gunicorn](http://gunicorn.org/) as defined by `entrypoint` in
[`app.yaml`](app.yaml). You can use a different WSGI container if you want, as
long as it listens for web traffic on port `$PORT` and is declared in
[`requirements.txt`](requirements.txt).

## How this was created

This project was created using standard Django commands:

$ virtualenv env
$ source env/bin/activate
$ pip install django gunicorn
$ pip freeze > requirements.txt
$ django-admin startproject project_name
$ python manage.py startapp helloworld

Then, we added a simple view in `hellworld.views`, added the app to
`project_name.settings.INSTALLED_APPS`, and finally added a URL rule to
`project_name.urls`.

In order to deploy to Google App Engine, we created a simple
[`app.yaml`](app.yaml).

## Database notice

This sample project uses Django's default sqlite database. This isn't suitable
for production as your application can run multiple instances and each will
have a different sqlite database. Additionally, instance disks are ephemeral,
so data will not survive restarts.

For production applications running on Google Cloud Platform, you have
the following options:

* Use [Cloud SQL](https://cloud.google.com/sql), a fully-managed MySQL database.
There is a [Flask CloudSQL](../cloudsql) sample that should be straightforward
to adapt to Django.
* Use any database of your choice hosted on
[Google Compute Engine](https://cloud.google.com/compute). The
[Cloud Launcher](https://cloud.google.com/launcher/) can be used to easily
deploy common databases.
* Use third-party database services, or services hosted by other providers,
provided you have configured access.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 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.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT project_name.wsgi

runtime_config:
python_version: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
# 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.
# 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.


from django.http import HttpResponse


def index(request):
return HttpResponse("Hello, World. This is Django running on Google App Engine")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# Copyright 2021 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 os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2023 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.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
# Skipping for Python 3.9 due to pyarrow compilation failure.
"ignored_versions": ["2.7", "3.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": False,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
Loading

0 comments on commit a0ba486

Please sign in to comment.