Getting started with App Engine (Python 3)  |  Google Codelabs (2024)

1. Overview

Getting started with App Engine (Python 3) | Google Codelabs (2)

Google App Engine applications are easy to create, easy to maintain, and easy to scale as your traffic and data storage needs change. With App Engine, there are no servers to maintain. You simply upload your application and it's ready to go.

In this codelab, you will learn how to deploy a simple Python web app written with the Flask web framework. Although this sample uses Flask, you can use other web frameworks, including Django, Pyramid, Bottle, and web.py.

This tutorial is adapted from https://cloud.google.com/appengine/docs/standard/python3/quickstart

What you'll learn

  • How to create a simple Python server on Google App Engine.
  • How to update the code without taking the server down.

What you'll need

  • Familiarity using Python
  • Familiarity with standard Linux text editors such as vim, emacs, or nano

Survey

How will you use this tutorial?

Read it through onlyRead it and complete the exercises

How would you rate your experience with Python?

NoviceIntermediateProficient

How would you rate your experience with Google Cloud services?

NoviceIntermediateProficient

2. Setup and requirements

Self-paced environment setup

  1. Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.

Getting started with App Engine (Python 3) | Google Codelabs (3)

Getting started with App Engine (Python 3) | Google Codelabs (4)

Getting started with App Engine (Python 3) | Google Codelabs (5)

  • The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
  • The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified as PROJECT_ID). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project.
  • For your information, there is a third value, a Project Number, which some APIs use. Learn more about all three of these values in the documentation.
  1. Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the $300 USD Free Trial program.

Start Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Cloud Shell, a command line environment running in the Cloud.

Activate Cloud Shell

  1. From the Cloud Console, click Activate Cloud Shell Getting started with App Engine (Python 3) | Google Codelabs (6).

Getting started with App Engine (Python 3) | Google Codelabs (7)

If this is your first time starting Cloud Shell, you're presented with an intermediate screen describing what it is. If you were presented with an intermediate screen, click Continue.

Getting started with App Engine (Python 3) | Google Codelabs (8)

It should only take a few moments to provision and connect to Cloud Shell.

Getting started with App Engine (Python 3) | Google Codelabs (9)

This virtual machine is loaded with all the development tools needed. It offers a persistent 5 GB home directory and runs in Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this codelab can be done with a browser.

Once connected to Cloud Shell, you should see that you are authenticated and that the project is set to your project ID.

  1. Run the following command in Cloud Shell to confirm that you are authenticated:
gcloud auth list

Command output

 Credentialed AccountsACTIVE ACCOUNT* <my_account>@<my_domain.com>To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. Run the following command in Cloud Shell to confirm that the gcloud command knows about your project:
gcloud config list project

Command output

[core]project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

3. Write the web app

After Cloud Shell launches, you can use the command line to invoke the Cloud SDK gcloud command or other tools available on the virtual machine instance. You can use your $HOME directory in persistent disk storage to store files across projects and between Cloud Shell sessions. Your $HOME directory is private to you and cannot be accessed by other users.

Let's get started by creating a new folder in your $HOME directory for the application:

mkdir ~/helloworldcd ~/helloworld

Create a file named main.py:

touch main.py

Edit the file with your preferred command line editor (nano, vim, or emacs) or by clicking the Cloud Shell Editor button:

Getting started with App Engine (Python 3) | Google Codelabs (10)

To directly edit the file with Cloud Shell Editor, use this command:

cloudshell edit main.py

main.py

import flask# If `entrypoint` is not defined in app.yaml, App Engine will look for an app# called `app` in `main.py`.app = flask.Flask(__name__)@app.get("/")def hello(): """Return a friendly HTTP greeting.""" return "Hello World!\n"if __name__ == "__main__": # 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="localhost", port=8080, debug=True)

4. Define the dependencies

To specify the dependencies of your web app, go back to the terminal and create a requirements.txt file in the root directory of your project, with the exact version of Flask to use:

touch requirements.txt

To edit the file with Cloud Shell Editor, use this command:

cloudshell edit requirements.txt

requirements.txt

# https://pypi.org/project/FlaskFlask==3.0.2

5. Configure the deployment

To deploy your web app to App Engine, you need an app.yaml file. This configuration file defines your web app's settings for App Engine.

From the terminal, create and edit the app.yaml file in the root directory of your project:

touch app.yaml

To edit the file with Cloud Shell Editor, use this command:

cloudshell edit app.yaml

app.yaml

runtime: python312

6. Deploy the web app

From the terminal, check the content of your directory:

ls

You should have the 3 following files:

app.yaml main.py requirements.txt

Deploy your web app with the following command:

gcloud app deploy

The first time, you need to choose a deployment region:

Please choose the region where you want your App Engine applicationlocated: [1] asia-east2... [7] australia-southeast1 [8] europe-west [9] europe-west2... [12] northamerica-northeast1 [13] southamerica-east1... [19] us-west4...Please enter your numeric choice:

Confirm to launch the deployment:

Creating App Engine application in project [PROJECT_ID] and region [REGION]....done.Services to deploy:descriptor: [~/helloworld/app.yaml]source: [~/helloworld]target project: [PROJECT_ID]target service: [default]target version: [YYYYMMDDtHHMMSS]target url: [https://PROJECT_ID.REGION_ID.r.appspot.com]Do you want to continue (Y/n)?

Your app gets deployed:

Beginning deployment of service [default]...Created .gcloudignore file. See `gcloud topic gcloudignore` for details.Uploading 3 files to Google Cloud Storage100%File upload done.Updating service [default]...done. Setting traffic split for service [default]...done.Deployed service [default] to [https://PROJECT_ID.REGION_ID.r.appspot.com]

Your web app is now ready to respond to HTTP requests on https://PROJECT_ID.REGION_ID.r.appspot.com.

7. Test the web app

Your web app is ready to respond to HTTP requests on https://PROJECT_ID.REGION_ID.r.appspot.com.

First, retrieve your web app hostname with the gcloud app describe command:

APPENGINE_HOSTNAME=$(gcloud app describe --format "value(defaultHostname)")

Test your web app with this simple HTTP GET request:

curl https://$APPENGINE_HOSTNAME

You should get the following answer:

Hello World!

Summary

In the previous steps, you set up a simple Python web app, ran, and deployed the application on App Engine.

8. Update the web app

Modify your web app by changing the hello() function body in your main.py file.

To edit the file with Cloud Shell Editor, use this command:

cloudshell edit main.py

main.py

import flask# If `entrypoint` is not defined in app.yaml, App Engine will look for an app# called `app` in `main.py`.app = flask.Flask(__name__)@app.get("/")def hello(): """Return a friendly HTTP greeting.""" # return "Hello World!\n" # ← Replace this line who = flask.request.args.get("who", "World") return f"Hello {who}!\n"if __name__ == "__main__": # 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="localhost", port=8080, debug=True)

From the terminal, redeploy to update your web app:

gcloud app deploy --quiet

The new version of your app gets deployed:

Beginning deployment of service [default]...Uploading 1 file to Google Cloud Storage ...Deployed service [default] to [https://PROJECT_ID.REGION_ID.r.appspot.com]

Test the new version of your web app, exactly as you did previously:

curl https://$APPENGINE_HOSTNAME

You should get the same answer:

Hello World!

Test it with the optional parameter:

curl https://$APPENGINE_HOSTNAME?who=Universe

You should get the following answer:

Hello Universe!

Summary

In this step, you updated and redeployed your web app without any service interruption.

9. Congratulations!

You learned to write your first App Engine web application in Python!

Learn more

  • App Engine Documentation: https://cloud.google.com/appengine
  • Explore this tutorial to write a fully-fledged Python app on App Engine: https://cloud.google.com/appengine/docs/standard/python3/building-app

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Getting started with App Engine (Python 3)  |  Google Codelabs (2024)

FAQs

Is Google App Engine easy to use? ›

Google App Engine benefits

GAE extends the benefits of cloud computing to application development in multiple ways. Ease of setup and use. GAE is a fully managed platform, so users can write code without considering IT operations or worrying about back-end infrastructure.

How to create an app using Google App Engine? ›

  1. Overview. Google App Engine applications are easy to create, easy to maintain, and easy to scale as your traffic and data storage needs change. ...
  2. Setup and requirements.
  3. Write the web app. ...
  4. Define the dependencies. ...
  5. Configure the deployment. ...
  6. Deploy the web app. ...
  7. Test the web app. ...
  8. Update the web app.
Mar 27, 2024

What are the disadvantages of Google App Engine? ›

Disadvantages of GAE:
  • GAE is still insufficiently stable. ...
  • It is difficult to execute various data transformations with the existing libraries without native file system read/write access, and it is also not compatible with several native file system base libraries.
  • It does not offer an API for full-text searches.
Nov 9, 2023

How many applications can I create with Google App Engine? ›

Important: Each Google Cloud project can contain only a single App Engine application, and once created you cannot change the location of your App Engine application.

Is Google App Engine expensive? ›

The total cost monthly is around $110 per month for the App Engine.

Is Google App Engine free forever? ›

Daily and monthly charges

Daily: Every day you are charged for the resources you actually use. Usage up to the free tier limits is included in the usage total, but not in the billable amount. Usage above the free tier is charged at the regular rates.

What programming supports Google App Engine? ›

To create an application for an app engine, you can use Go, Java, PHP, or Python. You can develop and test an app locally using the SDK's deployment toolkit. Each language's SDK and nun time are unique.

Can I run Python on Google app Script? ›

Your Python application runs and calls the Google Apps Script API. Authorization information is stored in the file system, so the next time you run the sample code, you aren't prompted for authorization.

How do I run a Python code in Google? ›

In the Google Cloud console, open the app in Cloud Shell. Cloud Shell provides command-line access to your cloud resources directly from the browser. If you agree to clone the repository, click Confirm to download the sample code and change into the app directory.

What versions of Python does App Engine support? ›

Supported versions are Python 3.8-3.10 based on this documentation. You may also check this documentation on Python runtime for more information.

What is the advantage of Google App Engine? ›

With Google App Engine, developers can easily develop their apps and get them to the end user quickly when running in a trusted environment. Easy management: GAE brings together the tools needed to develop, test, launch and update applications. Developers can design applications on this platform in a very simple form.

What can you do with Google App Engine? ›

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand. Learn more.

Can I use Google App Engine for free? ›

Apps in the standard environment have a free tier for App Engine resources. Any use of App Engine resources beyond the free tier incurs charges as described in this section. To estimate costs for App Engine resources in the standard environment, use the pricing calculator.

What are the disadvantages of using Google Apps for work? ›

  • Limited Functionality Compared to Desktop-Based Apps Like Microsoft Office.
  • Issues with Customer Support as Reported by Some Users.
  • Certain Limitations in File-Sharing Outside the Organization.
  • Challenges with Complex Data Migration Systems.
Mar 12, 2024

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5928

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.