Write a basic web service for App Engine

Note: If you are deploying a new Python web service to Google Cloud,we recommend getting started withCloud Run.

Write and locally test a web service that serves a static HTML file using Flask.Then, create the configurationfiles 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 thatdisplays placeholder data. The goal here is to ensure that your basic webservice is working before adding Datastore and Firebase authentication.

Before you begin

  1. If you have not already created a Google Cloud project,create a Google Cloud project.

  2. If you have not already, set up your local environment for Python 3development by completing the following:

Structure your web service files

The project directory where you create your web service will have thefollowing 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 yourproject directory.

Write your web service

The initial iteration of your web service uses Flask to serve aJinja-based HTML template.

To set up your web service:

  1. Create yourtemplates/index.html file:

    <!doctype html><!-- 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.--><html><head>  <title>Datastore and Firebase Auth Example</title>  <script src="{{ url_for('static', filename='script.js') }}"></script>  <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"></head><body>  <h1>Datastore and Firebase Auth Example</h1>  <h2>Last 10 visits</h2>  {% for time in times %}    <p>{{ time }}</p>  {% endfor %}</body></html>
  2. Add behaviors and styles withstatic/script.js andstatic/style.css files:

    'use strict';window.addEventListener('load',function(){console.log("Hello World!");});
    /** * 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. */body{font-family:"helvetica",sans-serif;text-align:center;}
  3. In yourmain.py file, use Flask to render your HTML template with theplaceholder data:

    importdatetimefromflaskimportFlask,render_templateapp=Flask(__name__)@app.route("/")defroot():# For the sake of example, use static information to inflate the template.# This will be replaced with real information in later steps.dummy_times=[datetime.datetime(2018,1,1,10,0,0),datetime.datetime(2018,1,2,10,30,0),datetime.datetime(2018,1,3,11,0,0),]returnrender_template("index.html",times=dummy_times)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. This# can be configured by adding an `entrypoint` to app.yaml.# Flask's development server will automatically serve static files in# the "static" directory. See:# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,# App Engine itself will serve those files as configured in app.yaml.app.run(host="127.0.0.1",port=8080,debug=True)
  4. Configure all dependencies you will need for your web service in yourrequirements.txt file:

    Flask==3.0.0

Test your web service

Test your web service by running it locally in a virtual environment:

Mac OS / Linux

  1. Create anisolated Python environment:
    python3-mvenvenvsourceenv/bin/activate
  2. If you're not in the directory that contains the sample code, navigate to the directory that contains thehello_world sample code. Then install dependencies:
    cdYOUR_SAMPLE_CODE_DIRpipinstall-rrequirements.txt
  3. Run the application:
    pythonmain.py
  4. In your web browser, enter the following address:
    http://localhost:8080

Windows

Use PowerShell to run your Python packages.

  1. Locate your installation ofPowerShell.
  2. Right-click on the shortcut to PowerShell and start it as an administrator.
  3. Create anisolated Python environment.
    python-mvenvenv.\env\Scripts\activate
  4. 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 thehello_world sample code. Then, install dependencies:
    cdYOUR_SAMPLE_CODE_DIRpipinstall-rrequirements.txt
  5. Run the application:
    pythonmain.py
  6. 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 anapp.yaml file.This configuration file defines your web service's settings forApp Engine.

To configure your web service for deployment to App Engine, createyourapp.yaml file in the root directory of your project, for examplebuilding-an-app:

# 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:python313handlers:# This configures Google App Engine to serve the files in the app's static# directory.-url:/staticstatic_dir:static# This handler routes all requests not caught above to your main app. It is# required when static routes are defined, but can be omitted (along with# the entire handlers section) when there are no static files defined.-url:/.*script:auto

Notice that for this simple web service, yourapp.yaml file needs to defineonly the runtime setting and handlers for static files.

For more complicated web services, you can configure additional settingsin yourapp.yaml, like scaling, additional handlers, and otherapplication elements like environment variables and service names.For more information and a list of all the supported elements, see theapp.yaml reference.

Next steps

Now that you've configured, created, and tested your web service, you candeploy this version of your web service to App Engine.

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

Last updated 2025-12-15 UTC.