Getting Started with Flask on App Engine Standard Environment
Python 2.7|Python 3.7
This guide is designed to help you learn how to develop and deploy basic Python
2.7 applications that run on the Google App Engine Standard
This guide is intended for use by those new to Google App Engine, its related
services, and in particular, using App Engine with the Python language. For that
reason, the guide provides deeper explanations for each task than what is found
in the Quickstart guide.
Before you begin
Before you can develop an application:
Create a new GCP Console project or retrieve the project ID of
an existing project from the Google Cloud Platform Console:
This guide uses the Flask web application framework because of its simplicity,
ease of use, and extensibility, but the same principles can be applied to any
framework that you want to use. This guide teaches you:
How to create a basic user comment form that will display the content that the
user submits via that form on an HTML template that you create.
How to create a basic application that can serve static files, such as CSS or
After you have set up your development environment, you can write the code for
the application and deploy it to App Engine.
Basic structure of an app project
This guide uses the following structure for the flask-app project:
flask-app project structure
app.yaml: Configure the settings of your App Engine application
main.py: Write the content of your application
static: Directory to store your static files
style.css: Basic stylesheet that formats the look and feel of your
templates: Directory for all of your HTML templates
form.html: HTML template to display your form
submitted_form.html: HTML template to display the content of a submitted
Setting up libraries to enable development
This tutorial puts a copy of the Flask library in your app's directory. Note
that although the App Engine Python 2.7 runtime environment has a
library, that bundled library is an older version that might not work with this
To set up the required libraries:
Create a file named appengine_config.py in the root project directory.
When you deploy your application, you can use this file to specify where
App Engine should look for third-party libraries:
To run this app on your local computer, you'll need a Python
development environment set up, including Python, pip, and virtualenv. For
instructions, refer to
Setting Up a Python Development Environment for Google Cloud
Install dependencies using virtualenv:
Mac OS / Linux
Create an isolated Python environment in a directory external to your project and activate it:
virtualenv envsource env/bin/activate
At the end of the tutorial, you can exit your virtualenv
by typing deactivate.
Navigate to your project directory and install dependencies:
cd YOUR_PROJECTpip install -t lib -r requirements.txt
If you have installed the Cloud SDK, you should already have Python 2.7
installed, typically in C:\python27_x64\ (for 64-bit systems). Use
Powershell to run your Python packages.
The -r requirements.txt flag tells pip to install everything from a
Creating the app.yaml file
You can configure your App Engine application's settings in the app.yaml file
that you create manually or as part of the creation of your development project.
The app.yaml file is a configuration file that tells App Engine how to run
your application and how to map URLs to static files and Python modules.
To create the app.yaml file:
Create a file named app.yaml in the root directory of your project.
When App Engine receives a web request for your application, it calls the
handler script that corresponds to the URL, as described in the application's
app.yaml configuration file. The Python 2.7 runtime supports the
WSGI is preferred, and some features of Python 2.7 do not work without it. The
configuration of your application's script handlers determines whether a request
is handled using WSGI.
The server determines which Python application object to call by comparing the
URL of the request to the URL patterns in the app's configuration file. It then
calls the application object using the arguments as defined in the
The application object performs actions appropriate to the request, then
prepares a response and returns it as a list of strings.
The following request handlers take the information that is submitted in the
form in the /templates/form.html file and places that information onto the
/templates/submitted_form.html template file:
Create a new file called main.py in the root directory of your application.
Import the Flask framework and the Flask interfaces that you want to use:
name = request.form['name']
email = request.form['email']
site = request.form['site_url']
comments = request.form['comments']
The application stores the form information in the variables that you created
here. These variables will allow you to post the data from the form onto the
submitted_form.html template that you will create.
You can easily extend the functionality of this form. For example, you can use
the Mail API,
SendGrid to send the comments that users
submit to yourself or to others.
Setting up Jinja2 templates
Since HTML that is embedded in code can be difficult to maintain, you should use
a templating system, storing HTML in a separate file that uses special syntax to
specify where the data returned from an application appears. You can use your
template engine of choice by bundling it with your application code. For your
convenience, App Engine includes the
Add the following line to the end of the submitted_form() function:
The handlers section defines two handlers for URLs. When App Engine
receives a request for a URL beginning with /static, it maps the remainder
of the path to files in the static directory, and if an appropriate file
is found, the contents of the file are returned to the client.
For more information on URL mapping and other options you can specify in
app.yaml, see the
Test the application
Test the application using the local development server (dev_appserver.py),
which is included with the SDK.
From within the root directory where the app's
configuration file is located, start the local development server with the
The local development server is now running and listening for requests on