Checkpoints
Configure the Quiz application
/ 50
Work with Cloud Pub/Sub
/ 20
Create an cloud spanner instance and database
/ 30
App Dev: Developing a Backend Service - Python
GSP187
Overview
Google App Engine lets you manage resources from the command line, debug source code in production and run API backends. This lab concentrates on the backend service, putting together Pub/Sub, Natural Language, and Spanner services and APIs to collect and analyze feedback and scores from an online Quiz application.
Objectives
In this lab, you perform the following tasks:
-
Create and publish messages to a Cloud Pub/Sub topic.
-
Subscribe to the topic to receive messages in a separate worker application.
-
Use the Cloud Natural Language Machine Learning API.
-
Create and configure a Cloud Spanner database instance, then insert data into the database.
Setup and requirements
Lab setup
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
Cloud Console
How to start your lab and sign in to the Google Cloud Console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.
-
Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.
Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Cloud Console opens in this tab.
Activate Cloud Shell
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. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell
at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
-
(Optional) You can list the active account name with this command:
-
Click Authorize.
-
Your output should now look like this:
Output:
-
(Optional) You can list the project ID with this command:
Output:
Example output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Launch the Cloud Shell code editor
- From Cloud Shell, click Open Editor to launch the code editor.
-
Run the following command to configure your Project ID, replacing
<YOUR-PROJECT-ID>
with your Project ID:
Task 1. Prepare the quiz application
In this section, you access Cloud Shell and enter commands to:
-
Clone the git repository containing the Quiz application
-
Configure environment variables
-
Run the application
Clone source code in Cloud Shell
-
Clone the repository for the class:
Configure and run the Quiz application
In this section you'll open two Cloud Shell windows, one for the web part of the Quiz application, the other the worker part of the application that handles the console.
-
Change the working directory:
cd ~/training-data-analyst/courses/developingapps/v1.2/python/pubsub-languageapi-spanner/start -
Enter a script file to configure the web application:
This script file:
-
Creates an App Engine application.
-
Exports environment variables:
GCLOUD_PROJECT
andGCLOUD_BUCKET
. -
Updates pip then runs pip install -r requirements.txt.
-
Creates entities in Cloud Datastore.
-
Prints out the Project ID.
. prepare_web_environment.sh Ignore the incompatibility messages.
Click Check my progress to verify the objective.
Configure the Quiz application -
-
Run the web application:
python run_server.py The application is running when you see a message similar to the example output:
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 110-448-781 -
Click the (+) icon next to the Cloud Shell tab to open a second Cloud Shell tab. This tab runs the Worker (console) application.
-
In the second window, change the working directory:
cd ~/training-data-analyst/courses/developingapps/v1.2/python/pubsub-languageapi-spanner/start -
Run the worker application in the second Cloud Shell window:
. run_worker.sh This script file:
- Exports environment variables
GCLOUD_PROJECT
andGCLOUD_BUCKET
. - Creates and configures a Google Cloud Service Account.
- Prints out the Project ID.
- Runs the worker application
python -m quiz.console.worker
.
- Exports environment variables
Check out the Quiz application
-
In Cloud Shell, click Web preview > Preview on port 8080 to preview the Quiz application.
-
In the navigation bar, click Take Test.
-
Click Places.
-
Answer the question.
After you answer the question, you should see a final screen inviting you to submit feedback.
You can put information in the form, but the Send Feedback button does not yet work.
-
Return to the first Cloud Shell window, and press Ctrl+c to stop the web application.
Task 2. 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.
Review the Google Cloud application code structure
-
Navigate to the
/training-data-analyst/courses/developingapps/v1.2/python/pubsub-languageapi-spanner/start
folder using the file browser panel on the left side of the editor. -
Select the
pubsub.py
file in the.../quiz/gcp
folder.This file contains a module that allows applications to publish feedback messages to a Cloud Pub/Sub topic and register a callback to receive messages from a Cloud Pub/Sub subscription.
-
Select the
languageapi.py
file in the.../quiz/gcp
folder.This file contains a module that allows users to send text to the Cloud Natural Language ML API and to receive the sentiment score from the API.
-
Select the
spanner.py
file.This file contains a module that allows users to save the feedback and Natural Language API response data in a Cloud Spanner database instance.
Review the web application code
-
Select the
api.py
file in the.../quiz/api
folder.The handler for POST messages sent to the
/api/quizzes/feedback/:quiz
route publishes the feedback data received from the client to Pub/Sub. -
Select the
worker.py
file in the.../quiz/console
folder.This file runs as a separate console application to consume the messages delivered to a Pub/Sub subscription.
Task 3. Work with Cloud Pub/Sub
In this section, you create a Cloud Pub/Sub topic and subscription in your Google Cloud project, publish a message, and retrieve it.
Create a Cloud Pub/Sub topic
-
In the Cloud Platform Console, click Navigation menu > Pub/Sub > Topics.
-
Click CREATE TOPIC.
-
For Topic ID, type
feedback
, and then click CREATE TOPIC.
Create a Cloud Pub/Sub subscription
-
Return to the second Cloud Shell window and press Ctrl+c to stop the application.
-
Create a Cloud Pub/Sub subscription named
worker-subscription
against thefeedback
topic:gcloud pubsub subscriptions create worker-subscription --topic feedback
Click Check my progress to verify the objective.
Publish a message to a Cloud Pub/Sub topic
-
Publish a
"Hello World"
message into thefeedback
topic:
Retrieve a message from a Cloud Pub/Sub subscription
-
Now pull the message from the
feedback
topic with automatic acknowledgement of the message:
Output:
Task 4. Publish messages to Cloud Pub/Sub programmatically
Write code to publish messages to Cloud Pub/Sub
# TODO
# END TODO
To maximize your learning, review the code, inline comments, and related API documentation.Import and use the Python Cloud Pub/Sub module
In this section, you'll update ...quiz/gcp/pubsub.py
to do the following:
- Open the
...quiz/gcp/pubsub.py
file in the editor. - Load the
pubsub_v1
module from thegoogle.cloud
package. - Construct a Cloud Pub/Sub Publisher client.
- Get the fully qualified path referencing the feedback Pub/Sub topic you created earlier.
quiz/gcp/pubsub.py
Write code to publish a message to Cloud Pub/Sub
- In the
publish_feedback(feedback)
function, publish a message to the feedback topic.
quiz/gcp/pubsub.py
-
Save the file.
Write code to use the Pub/Sub publish functionality
-
In the
.../quiz/api/api.py
file, load thepubsub
module from thequiz.gcp
package.
quiz/api/api.py
- In the
publish_feedback(...)
function, remove the placeholderpass
statement. - Invoke the
pubsub.publish_feedback(feedback)
function. - Then, return a response to the client indicating that feedback was received.
quiz/api/api.py__, publish_feedback(...) function
-
Save the file.
Run the application and create a Pub/Sub message
-
In the first Cloud Shell window, restart the web application (if it is running, stop and start it).
-
Preview the web application.
-
Click Take Test.
-
Click Places.
-
Answer the question, select the rating, enter some feedback text, and click Send Feedback.
-
In the second Cloud Shell window, to pull a message from the
worker-subscription
, execute the following command:gcloud pubsub subscriptions pull worker-subscription --auto-ack Output:
-
Stop the web and console applications.
Task 5. Subscribe to Cloud Pub/Sub topics programmatically
In this section you write the code to create a subscription to a Cloud Pub/Sub topic and receive message notifications in the worker console application.
Write code to create a Cloud Pub/Sub subscription and receive messages
The code you add performs these actions:
-
Return to the
...quiz/gcp/pubsub.py
file. -
Declare a Cloud Pub/Sub Subscriber Client.
-
Get the fully qualified path referencing the
'worker-subscription'
. -
Move to the
pull_feedback(callback)
function, and remove the placeholderpass
statement. -
Use the subscriber client to subscribe to the worker subscription, invoking the callback when a message is received.
/quiz/gcp/pubsub.py
# TODO: Create a Pub/Sub Subscriber Client sub_client = pubsub_v1.SubscriberClient() # END TODO # TODO: Create a Subscription object named # worker-subscription sub_path = sub_client.subscription_path(project_id, 'worker-subscription') # END TODO def pull_feedback(callback): # TODO: Subscribe to the worker-subscription, # invoking the callback sub_client.subscribe(sub_path, callback=callback) # END TODO -
Save the file.
Write code to use the Pub/Sub subscribe functionality
The code you add performs these actions:
- In the
...quiz/console/worker.py
file, load thepubsub
module from thequiz.gcp
package. - In the
pubsub_callback(message)
function, acknowledge the message - Log the message to the console.
- In the
main()
function, register the handler function as the Pub/Sub subscription callback.
console/worker.py
-
Save the file.
Run the web and worker application and create a Pub/Sub message
-
In the first Cloud Shell window, start the web application if it's not already running.
python run_server.py -
In the second Cloud Shell window, start the worker application.
. run_worker.sh -
In Cloud Shell, click Web preview > Preview on port 8080 to preview the quiz application.
-
Click Take Test.
-
Click Places.
-
Answer the question, select the rating, enter some feedback text, and then click Send Feedback.
-
Return to the second Cloud Shell window.
The worker application should show that it has received the feedback message via its handler and displayed details it in the window. The message itself is truncated.
INFO:root:Worker starting... INFO:root:Message received INFO:root:Message { data: b'{\n "email": "app.dev.student@example.org",\n "fee...' ordering_key: '' attributes: {} } -
Stop the web and console applications.
Task 6. Use the Cloud Natural Language API
In this section you write the code to perform sentiment analysis on the feedback text submitted by the user.
Write code to invoke the Cloud Natural Language API
The code you add performs these actions:
- In the editor, move to the top of the
...quiz/gcp/languageapi.py
file. - Load the
language
module from thegoogle.cloud
package. - Load the
enums
andtypes
modules from thegoogle.cloud.language
package. - Create a Cloud Natural Language client object.
- Move to the
analyze(...)
function, and create a Document object to pass to the Natural Language client. - Configure this object with two properties:
content
andtype
. - Assign the feedback text to this object's
content
property. - Set the
type
property value to the type that corresponds toPLAIN_TEXT
. - Use the Natural Language client object to analyze the sentiment of the document.
- Then, return the sentiment score from the Natural Language API.
quiz/gcp/languageapi.py
-
Save the file.
Write code to use the Natural Language API functionality
-
In the
.../quiz/console/worker.py
file, load thelanguageapi
module. -
In the
pubsub_callback(message)
function, and after the existing code, perform sentiment detection on the feedback. -
Then, log the score to the console.
-
Assign a new score property to the feedback object.
-
Return the message data.
console/worker.py
-
Save the file.
Run the web and worker application and test the Natural Language API
- Return to the first Cloud Shell window.
- Run the web application.
- Switch to the second Cloud Shell window.
- Restart the worker application.
- Preview the web application.
- Click Take Test.
- Click Places.
- Answer the questions, select the rating, enter some feedback text, and then click Send Feedback.
- Return to the second Cloud Shell window.
You should see that the worker application has invoked the Cloud Natural Language API and displayed the sentiment score in the console.
-
Stop the web and console applications.
Task 7. Persist data to Cloud Spanner
In this section you create a Cloud Spanner instance, database, and table. Then you write the code to persist the feedback data into the database.
Create a Cloud Spanner instance
-
Return to the Cloud Console and click Navigation menu > Spanner > CREATE A PROVISIONED INSTANCE.
-
For Instance name, type quiz-instance
-
In the Configuration section, select us-central1 as the region.
-
Click CREATE.
Create a Cloud Spanner database and table
-
On the Instance Overview page for quiz-instance, click CREATE DATABASE.
-
For Name, type quiz-database.
-
Under Define your schema, type the following SQL statement:
- Click CREATE.
Click Check my progress to verify the objective.
Write code to persist data into Cloud Spanner
The code you add performs these actions:
-
Return to the code editor, and move to the top of the
.../quiz/gcp/spanner.py
file. -
Load the
spanner
module from thegoogle.cloud
package. -
Construct a Cloud Spanner client.
-
Get a reference to the Spanner instance.
-
Get a reference to the Spanner database.
quiz/gcp/spanner.py
- Move to the
saveFeedback(...)
function. - Create a database.batch object using a
with
block. This can be used to perform multiple operations against a Spanner database. - Create a key for the feedback record from the email, quiz, and timestamp properties from the data. For the email property, use the
reverse_email(...)
function to take the input email and create a reversed string. For example: Input: app.dev.student@example.com Output: com_example_student_dev_app - Use the batch object to insert a record, using a set of columns and values.
quiz/gcp/spanner.py
-
Save the file.
Write code to use the Cloud Spanner functionality
The code you add performs these actions:
- In the
.../quiz/console/worker.py
file, load thespanner
module. - After the existing code in the
pubsub_callback(message)
function, save the feedback into Spanner. - Log a message to the console to say that the feedback has been saved.
quiz/console/worker.py
Run the web and worker application and test Cloud Spanner
- Save all the files, and then return to the first Cloud Shell window.
- Start the web application and then the worker application.
- Preview the web application.
- Click Take Test > Places.
- Answer the questions, select the rating, enter some feedback text, and then click Send Feedback.
- Return to the second Cloud Shell window.
You should see that the worker application has invoked the Cloud Spanner API and displayed the message in the console window.
-
Return to the Cloud Platform Console and click Navigation menu > Spanner.
-
Select quiz-instance > quiz-database > Query.
-
To execute a query, for Query, type
SELECT * FROM Feedback
, and then click Run query.
You should see the new feedback record in the Cloud Spanner database, including the message data from Cloud Pub/Sub and the score from the Cloud Natural Language API.
Congratulations!
This concludes the self-paced lab, App Dev: Developing a Backend Service - Python. You managed resources from the command line, debugged source code in production and ran API backends.
Finish your quest
This self-paced lab is part of the Application Development - Python and Cloud Development quests. A quest is a series of related labs that form a learning path. Completing this quest earns you a badge to recognize your achievement. You can make your badge or badges public and link to them in your online resume or social media account.Enroll in this Quest and get immediate completion credit. See the Google Cloud Skills Boost catalog to see all available quests.
Next steps / learn more
Learn more about Backend Services. Check out more cloud services, see About the Google Cloud Services
Manual last updated January 17, 2023
Lab last tested January 17, 2023
Copyright 2023 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.