Write a basic web service for App Engine Stay organized with collections Save and categorize content based on your preferences.
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
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 3development by completing the following:
Download and install Python 3for developing your web service and running the Google Cloud CLI.
Use your Google Cloud user credentials to authenticate with theGoogle Cloud CLI and enable local testing with Datastore:
Tip: For more extensive testing, it is recommended that you set up aservice account rather than using user-end credentials. For moreinformation on service accounts and other types of authentication, seeAuthentication Overview.gcloudauthapplication-defaultlogin
Structure your web service files
The project directory where you create your web service will have thefollowing file structure:
building-an-app/app.yamlmain.pyrequirements.txtstatic/script.jsstyle.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:
Create your
templates/index.htmlfile:<!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>Add behaviors and styles with
static/script.jsandstatic/style.cssfiles:'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;}In your
main.pyfile, 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)Configure all dependencies you will need for your web service in your
requirements.txtfile:Flask==3.0.0
Test your web service
Test your web service by running it locally in a virtual environment:
Mac OS / Linux
- Create anisolated Python environment:
python3-mvenvenvsourceenv/bin/activate - If you're not in the directory that contains the sample code, navigate to the directory that contains the
hello_worldsample code. Then install dependencies:cdYOUR_SAMPLE_CODE_DIRpipinstall-rrequirements.txt - Run the application:
pythonmain.py
- In your web browser, enter the following address:
http://localhost:8080
Windows
Use PowerShell to run your Python packages.
- Locate your installation ofPowerShell.
- Right-click on the shortcut to PowerShell and start it as an administrator.
- Create anisolated Python environment.
python-mvenvenv.\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_worldsample code. Then, install dependencies:cdYOUR_SAMPLE_CODE_DIRpipinstall-rrequirements.txt - Run the application:
pythonmain.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 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:autoNotice 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.