Write and locally test a web service that serves a static HTML file using Flask. Then, create the configuration files that you need for deploying the web service to App Engine.
In this step, you create and locally test a version of a web service that displays placeholder data. The goal here is to ensure that your basic web service is working before adding Datastore and Firebase authentication.
Before you begin
If you have not already created a Google Cloud project, create a Google Cloud project.
If you have not already, set up your local environment for Python 3 development by completing the following:
Download and install Python 3 for developing your web service and running the Google Cloud CLI.
Use your Google Cloud user credentials to authenticate with the Google Cloud CLI and enable local testing with Datastore:
gcloud auth application-default login
Structure your web service files
The project directory where you create your web service will have the following file structure:
building-an-app/
app.yaml
main.py
requirements.txt
static/
script.js
style.css
templates/
index.html
The following sections provide an example of how to set up the files in your project directory.
Write your web service
The initial iteration of your web service uses Flask to serve a Jinja-based HTML template.
To set up your web service:
Create your
templates/index.html
file:Add behaviors and styles with
static/script.js
andstatic/style.css
files:In your
main.py
file, use Flask to render your HTML template with the placeholder data:Configure all dependencies you will need for your web service in your
requirements.txt
file:
Test your web service
Test your web service by running it locally in a virtual environment:
Mac OS / Linux
- Create an
isolated Python environment:
python3 -m venv env
source env/bin/activate
- If you're not in the directory that contains the sample code, navigate
to the directory that contains the
hello_world
sample code. Then install dependencies:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
- Run the application:
python main.py
- In your web browser, enter the following address:
http://localhost:8080
Windows
Use PowerShell to run your Python packages.
- Locate your installation of PowerShell.
- Right-click on the shortcut to PowerShell and start it as an administrator.
- Create an
isolated Python environment.
python -m venv env
.\env\Scripts\activate
- Navigate to your project directory and install dependencies. If you're not in the directory
that contains the sample code, navigate to the directory that contains the
hello_world
sample code. Then, install dependencies:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
- Run the application:
python main.py
- In your web browser, enter the following address:
http://localhost:8080
Configure your web service for App Engine
To deploy your web service to App Engine, you need an app.yaml
file.
This configuration file defines your web service's settings for
App Engine.
To configure your web service for deployment to App Engine, create
your app.yaml
file in the root directory of your project, for example
building-an-app
:
Notice that for this simple web service, your app.yaml
file needs to define
only the runtime setting and handlers for static files.
For more complicated web services, you can configure additional settings
in your app.yaml
, like scaling, additional handlers, and other
application elements like environment variables and service names.
For more information and a list of all the supported elements, see the
app.yaml
reference.
Next steps
Now that you've configured, created, and tested your web service, you can deploy this version of your web service to App Engine.