Store and retrieve data

Region ID

TheREGION_ID is an abbreviated code that Google assignsbased on the region you select when you create your app. The code does notcorrespond to a country or province, even though some region IDs may appearsimilar to commonly used country and province codes. For apps created after February 2020,REGION_ID.r is included in App Engine URLs. For existing apps created before this date, the region ID is optional in the URL.

Learn moreabout region IDs.

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

Update your web service to connect to and handle data throughFirestore in Datastore mode (Datastore). Use theDatastore clientlibrariesto connect your web service to Datastore, a non-relational (NoSQL)database built for automatic scaling, high performance, and ease of applicationdevelopment.

In this step, you update your web service so that it stores page request data inDatastore and then displays a list of the last ten page requests.The goal here is to get data storage working for your web servicebefore you add Firebase Authentication and personalize data storage forauthenticated users.

Before you begin

If you have completed all the previous steps in this guide, skip this section.Otherwise, complete one of the following:

  • Start fromBuild a Python 3 Appand complete all the steps leading up to this one.

  • If you already have aGoogle Cloud project,you can continue by downloading a copy of the web service:

    1. Download the sample application repository usingGit:

      gitclonehttps://github.com/GoogleCloudPlatform/python-docs-samples

      Alternatively, you candownload the sample as a zip file and then extract it.

    2. Navigate to the directory that contains a copy of the files from theprevious step:

      cdpython-docs-samples/appengine/standard_python3/building-an-app/building-an-app-1
    3. Enable the Datastore API:

      gcloud services enable datastore.googleapis.com

Store and retrieve Datastore entities

Store and retrieve site request times as Datastore entities bycompleting the following:

  1. Add the following code to yourmain.py file:

    fromgoogle.cloudimportdatastoredatastore_client=datastore.Client()defstore_time(dt):entity=datastore.Entity(key=datastore_client.key("visit"))entity.update({"timestamp":dt})datastore_client.put(entity)deffetch_times(limit):query=datastore_client.query(kind="visit")query.order=["-timestamp"]times=query.fetch(limit=limit)returntimes

    Thestore_time method above uses the Datastore client librariesto create a new entity in Datastore. Datastoreentities are data objects that consist ofkeys andproperties. In thiscase, the entity's key is its customkind,visit. The entity also hasone property,timestamp, containing time of a page request.

    Thefetch_times method uses the keyvisit to querythe database for the ten most recentvisit entities and then storesthose entities in a list in descending order.

  2. Update yourroot method to call your new methods:

    @app.route("/")defroot():# Store the current access time in Datastore.store_time(datetime.datetime.now(tz=datetime.timezone.utc))# Fetch the most recent 10 access times from Datastore.times=fetch_times(10)returnrender_template("index.html",times=times)
  3. Update yourtemplates/index.html file to print thetimestamp ofeach entity:

    <h2>Last 10 visits</h2>{% for time in times %}  <p>{{ time['timestamp'] }}</p>{% endfor %}
  4. Ensure that yourrequirements.txt file includes all necessary dependencies:

    Flask==3.0.0google-cloud-datastore==2.15.1

For more information about Datastore entities, properties, and keys,seeEntities, Properties, and Keys.For more information on using Datastore client libraries, seeDatastore Client Libraries.

Test your web service

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

  1. Run the following commands in yourproject's main directory to install new dependencies and runyour web service.If you have not set up a virtual environment for local testing, seetesting your web service.

    pipinstall-rrequirements.txtpythonmain.py
  2. Enter the following address in your web browser to view your web service:

    http://localhost:8080
    Tip: Refresh the page to create additional page requests and store moreentities in Datastore.

You can view the entities that are created by your web service in theGoogle Cloud console:

Go to the Datastore entities page

Deploy your web service

Now that you have Datastore working locally, you can re-deploy your webservice to App Engine.

Run the following command from the root directory of your project,where yourapp.yaml file is located:

gcloudappdeploy

All traffic is automatically routed to the new version you deployed.

For more information on managing versions,seeManaging Services and Versions.

View your service

To quickly launch your browser and access your web service athttps://PROJECT_ID.REGION_ID.r.appspot.com, run the following command:

gcloudappbrowse

Next Steps

Now that you have Datastore working with your web service, you're readyto learn how to add Firebase to your web service.

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.