arrow_back

App Dev - Storing Application Data in Cloud Datastore: Python

Sign in Join
Get access to 700+ labs and courses

App Dev - Storing Application Data in Cloud Datastore: Python

Lab 2 hours universal_currency_alt 5 Credits show_chart Advanced
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

Overview

Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. In this lab, you use Datastore to store application data for an online Quiz application. You also configure the application to retrieve from Datastore and display the data in the quiz.

The Quiz application skeleton has already been written. You clone the repository that contains the skeleton using Google Cloud Shell, review the code using the Cloud Shell editor, and view it using the Cloud Shell web preview feature. You then modify the code that stores data to use Cloud Datastore.

Objectives

In this lab, you learn how to perform the following tasks:

  • Harness Cloud Shell as your development environment
  • Preview the application
  • Update the application code to integrate Cloud Datastore

Setup and requirements

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Qwiklabs using an incognito window.

  2. Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
    There is no pause feature. You can restart if needed, but you have to start at the beginning.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. Click Use another account and copy/paste credentials for this lab into the prompts.
    If you use other credentials, you'll receive errors or incur charges.

  7. Accept the terms and skip the recovery resource page.

Activate Google Cloud Shell

Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.

Google Cloud Shell provides command-line access to your Google Cloud resources.

  1. In Cloud console, on the top right toolbar, click the Open Cloud Shell button.

    Highlighted Cloud Shell icon

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Project ID highlighted in the Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  • You can list the active account name with this command:
gcloud auth list

Output:

Credentialed accounts: - @.com (active)

Example output:

Credentialed accounts: - google1623327_student@qwiklabs.net
  • You can list the project ID with this command:
gcloud config list project

Output:

[core] project =

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: Full documentation of gcloud is available in the gcloud CLI overview guide .

Task 1. Create a virtual environment

virtualenv is used to create user space virtual environments that allow you to install different sets of Python packages for different projects.

Using virtualenv also means you don't have to install Python packages globally which can cause system tools or other Python projects to break.

In this lab virtualenv is also used to make sure that Python3 is used for all Python commands.

  1. Configure a virtualenv environment for Python 3:

    virtualenv -p python3 vrenv
  2. Activate the virtual environment:

    source vrenv/bin/activate

Task 2. Prepare the Quiz application

The repository that contains the Quiz application is located on GitHub.com.

In this section, you use Cloud Shell to enter commands that clone repository and run the application.

Clone source code in Cloud Shell

  1. Clone the repository for the class:
git clone https://github.com/GoogleCloudPlatform/training-data-analyst
  1. Create a soft link as a shortcut to the working directory:
ln -s ~/training-data-analyst/courses/developingapps/v1.3/python/datastore ~/datastore

Configure and run the Quiz application

  1. Change the directory that contains the files for this lab:

    cd ~/datastore/start
  2. Export an environment variable, GCLOUD_PROJECT that references the GCP Project ID:

    export GCLOUD_PROJECT=$DEVSHELL_PROJECT_ID Note: GCP Project ID in Cloud Shell. While working in Cloud Shell, you will have access to the Project ID in the $DEVSHELL_PROJECT_ID environment variable.
  3. Install the application dependencies:

    python3 -m pip install -r requirements.txt
  4. Run the application:

    python3 run_server.py

    The application is running when you see a message similar to the following:

    * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 179-313-240

Review the Quiz application

  1. In Cloud Shell, click Web preview > Preview on port 8080 to preview the quiz application.

    Expanded web preview menu with the Preview on port 8080 option highlighted

    You should see the user interface for the web application. The three main parts to the application are:

    • Create Question
    • Take Test
    • Leaderboard

    Welcome to the Quite Interesting Quiz page

  2. In the navigation bar, click Create Question.

    You should see a simple form that contains textboxes for the question and answers with radio buttons to select the correct answer.

    Note: Quiz authors can add questions in this part of the application. This part of the application is written as a server-side web application using the popular Python web application framework Flask.
  3. In the navigation bar, click Take Test, and then GCP to access the GCP questions.

    You should see a sample question.

    Quite interesting quiz sample question displaying options A, B, C and D, and a submit answer button

    Quiz takers answer questions in this part of the application.

    Note: This part of the application is written as a client-side web application.
  4. To return to the server-side application, click on the Quite Interesting Quiz link in the navigation bar.

Task 3. Examine the Quiz application code

In this lab you'll view and edit files. You can use the shell editors that are installed on Cloud Shell, such as nano or vim or the Cloud Shell code editor.

This lab uses the Cloud Shell code editor to review the Quiz application code.

Launch the Cloud Shell code editor

  • In Cloud Shell, click Open Editor, and then click Open in a new window to launch the Cloud Editor.
Note: The code editor launches in a separate tab of your browser. In the original tab, click Open Terminal to reopen Cloud Shell.

Review the Flask Web application

  1. Navigate to the /datastore/start folder using the file browser panel on the left side of the editor.
Note: Paths will be relative to this folder. This application is a standard Python application written using the popular Flask application framework.
  1. Select the ...run-server.py file.

    This file contains the entrypoint for the application, and runs it on port 8080.

  2. Select the ...quiz/__init__.py file.

    This file imports routes for the web application and REST API.

  3. Select the ...quiz/webapp/questions.py and ...quiz/webapp/routes.py file.

    These files contain the routes that map URIs to handlers that display the form and collect form data posted by quiz authors in the web application.

  4. Select the ...quiz/webapp/templates folder.

    This folder contains templates for the web application user interface using Jinja2 templates.

  5. View the ...quiz/webapp/templates/add.html file.

    This file contains the Jinja2 template for the Create Question form.

    Notice how there is a select list to pick a quiz, textboxes where an author can enter the question and answers, and radio buttons to select the correct answer.

  6. Select the ...quiz/api/api.py file.

    This file contains the handler that sends JSON data to students taking a test.

  7. Select the ...quiz/gcp/datastore.py file.

    This is the file where you write Datastore code to save and load quiz questions to and from Cloud Datastore.

    This module will be imported into the web application and API.

Task 4. Adding entities to Cloud Datastore

In this section, you write code to save form data in Cloud Datastore.

Note: Update or add code between the following comments:

// TODO

// END TODO

To maximize your learning, review the code, inline comments, and related API documentation.

Create an App Engine application to provision Cloud Datastore

  1. Return to Cloud Shell and stop the application by pressing Ctrl+C.

  2. To create an App Engine application in your project, use the following command:

    gcloud app create --region "us-central"

You'll see this message when the App Engine has been created:

Creating App Engine application in project [qwiklabs-gcp-f67238775c00cfaa] and region [us-central]....done. Success! The app is now created. Please use `gcloud app deploy` to deploy your first app. Note: You aren't using App Engine for your web application yet. However, Cloud Datastore requires you to create an App Engine application in your project.

Import and use the Python Datastore module

  • Open the ...quiz/gcp/datastore.py file in the Cloud Shell editor and add the following code to perform the following:

    • Import the os module.
    • Use the os module to get the GCLOUD_PROJECT environment variable.
    • Import the datastore module from the google.cloud package.
    • Declare a datastore.Client client object named datastore_client.

Updated datastore.py

# TODO: Import the os module import os # END TODO # TODO: Get the GCLOUD_PROJECT environment variable project_id = os.getenv('GCLOUD_PROJECT') # END TODO from flask import current_app # TODO: Import the datastore module from the google.cloud package from google.cloud import datastore # END TODO # TODO: Create a Cloud Datastore client object # The datastore client object requires the Project ID. # Pass through the Project ID you looked up from the # environment variable earlier datastore_client = datastore.Client(project_id) # END TODO

Write code to create a Cloud Datastore entity

  1. Still in ...quiz/gcp/datastore.py, move to the save_question() function and remove the existing pass placeholder statement.

  2. Perform the following by adding the code:

    • Use the Datastore client object to create a key for a Datastore entity whose kind is 'Question'.
    • Use Datastore to create a Datastore question entity with the key.
    • Iterate over the items in the dictionary of values supplied from the Web application form.
    • In the body of the loop, assign each key and value to the Datastore entity object.
    • Use the Datastore client to save the data.

datastore.py - save_question() function

""" Create and persist and entity for each question The Datastore key is the equivalent of a primary key in a relational database. There are two main ways of writing a key: 1. Specify the kind, and let Datastore generate a unique numeric id 2. Specify the kind and a unique string id """ def save_question(question): # TODO: Create a key for a Datastore entity # whose kind is Question key = datastore_client.key('Question') # END TODO # TODO: Create a Datastore entity object using the key q_entity = datastore.Entity(key=key) # END TODO # TODO: Iterate over the form values supplied to the function for q_prop, q_val in question.items(): # END TODO # TODO: Assign each key and value to the Datastore entity q_entity[q_prop] = q_val # END TODO # TODO: Save the entity datastore_client.put(q_entity) # END TODO
  1. Save datastore.py.

Run the application and create a Cloud Datastore entity

  1. Save the ...quiz/gcp/datastore.py file and then return to the Cloud Shell command prompt.
  2. To run the application, execute the following command:
python3 run_server.py
  1. In Cloud Shell, click Web preview > Preview on port 8080 to preview the quiz application.
  2. Click Create Question.
  3. Complete the form with the following values, and then click Save:

Form Field

Value

Author

Your Name

Quiz

Google Cloud Platform

Title

Which company owns GCP?

Answer 1

Amazon

Answer 2

Google (select the Answer 2 radio button!)

Answer 3

IBM

Answer 4

Microsoft

You should returned to the application home page.

  1. Return to the Console, click Navigation menu > Datastore > Entities.

Expanded navigation menu highlighting the Datastore submenu and Entities option

You should see your new question!

Task 5. Retrieve Cloud Datastore entities

In this section, you write code to retrieve entity data from Cloud Datastore to view your question in the application.

Write code to retrieve Cloud Datastore entities

  1. In the code editor, in the ...quiz/gcp/datastore.py file, remove the code for the list_entities(quiz, redact) function and replace it with a query that:

    • Retrieves Question entities for a specific quiz from Cloud Datastore.
    • Uses the Datastore client to fetch the query, and uses the returned data to create a list.
    • Enumerate the list of items, and promote each entity's Key identifier to a top level property.
    • Return the results.
  2. Replace this code:

""" Returns a list of question entities for a given quiz - filter by quiz name, defaulting to gcp - no paging - add in the entity key as the id property - if redact is true, remove the correctAnswer property from each entity """ def list_entities(quiz='gcp', redact=True): return [{'quiz':'gcp', 'title':'Sample question', 'answer1': 'A', 'answer2': 'B', 'answer3': 'C', 'answer4': 'D', 'correctAnswer': 1, 'author': 'Nigel'}] """

With this code:

""" Returns a list of question entities for a given quiz - filter by quiz name, defaulting to gcp - no paging - add in the entity key as the id property - if redact is true, remove the correctAnswer property from each entity """ def list_entities(quiz='gcp', redact=True): query = datastore_client.query(kind='Question') query.add_filter('quiz', '=', quiz) results =list(query.fetch()) for result in results: result['id'] = result.key.id if redact: for result in results: del result['correctAnswer'] return results """
  1. Save datastore.py.

Run the application and test the Cloud Datastore query

Now to test if your question is retrieved from Datastore and loaded into your Quiz application.

  1. In Cloud Shell, press Ctrl+c to stop the application, then restart the application:
python3 run_server.py
  1. Preview the quiz. If the browser running the quiz is still open, reload the browser. Otherwise, click Web preview > Preview on port 8080.

  2. Click Take Test > GCP.

You should see the questions you created.

Quite Interesting Quiz displaying the question "Which company owns GCP?"

End your lab

When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.