Zhixian's Tech Blog

2014-12-21

Google App Engine development on Ubuntu for Python (Part 2 of 2)

Filed under: web application development — Tags: , , , , , — Zhixian @ 23:46:12 pm

This blog post describes how to run Google App Engine (GAE for short) on Ubuntu.
This is the second part of a 2-part blog post.
This blog post focus on writing a simple (helloworld) application just to demonstrate that the setup works and that we can deploy our application.

Parts

  1. Setting up Google App Engine SDK
  2. Running Google App Engine (this article)

An aside related article, How to Add Google App Engine project to Google Developers Console.

Assumptions

This blog post builds on the previous blog post Setting up Google App Engine SDK and assumes that you have followed the instructions there.

This blog post also assumes you know how to register a Google App Engine project in the Google Developers Console.
If you need help with this, you can take a look at my blog post, How to Add Google App Engine project to Google Developers Console.

Part 2 Summary

  1. Create project folders (opinionated)
  2. Create request handler
  3. Create configuration file
  4. Test project
  5. Deploying project

Create Project Directory

(Opinionated) I usually store my projects in a Projects folder in my home folder.

zhixian_084

Inside this Projects folder, right-click the folder area to display the context menu.
Click Create Folder from the context menu.

zhixian@SARA: ~-Projects_085

Name the folder HelloWorld.
So your Projects folder, should look like the below.
The files (request handler and configuration) will be stored inside the HelloWorld folder that you have created.

Projects_086

Create Request Handler

Double-click the HelloWorld folder to open the folder.
Right-click in the folder area to display the context menu.
Click Empty File from the context menu.

HelloWorld_087

Name the file as

HelloWorld.py

Your HelloWorld folder should look like the below:

HelloWorld_088

Right-click the HelloWorld.py file that you just created to display its context menu.
Click Open with Text Editor from the context menu to edit the file.
This will open the file in your configured text editor. By default, this should be gedit.

HelloWorld_090

Copy and paste the below into the file.

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

After you paste the above content into the file, it should look like the below.
Note the amount of space used for indentation.
This is important in Python as indentation is used to indicate code blocks.

Click on Save on the toolbar to save the file.

HelloWorld.py (~-Projects-HelloWorld) - gedit_092

Create Configuration File

In your HelloWorld folder, right-click in the folder area to display the context menu.
Click Empty File from the context menu.

HelloWorld_093

Name this file

app.yaml

HelloWorld_095

Right-click the app.yaml file that you just created to display its context menu.
Click Open with Text Editor from the context menu to edit the file.

HelloWorld_096

Copy and paste the below into the file, replacing the your-app-id with the project id that you defined in your Google Developers Console.

application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: HelloWorld.application

Now, you are ready to test the project.

Test Project

To test your project, open a terminal session and go to the folder that holds your project folder.
For my example, my HelloWorld project is created in a folder call Projects in my home folder.
So I would type the following command after starting the terminal session.

cd Projects

If I list the contents of the Projects folder, it should look like the below:

zhixian@SARA: ~-Projects_105

To run the HelloWorld application,

  1. Enter the following at the command prompt:

    dev_appserver.py HelloWorld/

  2. You will be prompt to give permission for the dev_appserver script to check for updates on startup.
    Enter “n” to disallow this check. Leaving this as “Y” or blank will allow dev_appserver to check for updates.
  3. Note the url of the “default” module.
    This is the url that your application is being hosted at.
    By default it is:

    http://localhost:8080

  4. Note the url of the admin server.
    This is the url that of the local testing administration server to your application.
    By default it is:

    http://localhost:8000

zhixian@SARA: ~-Projects_104

If you open your browser and navigate to the url of the “default” module (http://localhost:8080), you should see:

Firefox Developer Edition_107

If you open your browser and navigate to the url of the admin server (http://localhost:8000), you should see:

Instances - Firefox Developer Edition_108

To stop the test server, press <Control>-<C> keys combination.

zhixian@SARA: ~-Projects_109

Deploy Project

To upload the project to Google:

  1. Enter the following command at the command prompt:

    appcfg.py update HelloWorld/

  2. Enter your Google (gmail) account.
  3. Enter the password to your account.
  4. After the update has complete, you should see “Deployment Success”, somewhere near the end.

zhixian@SARA: ~-Projects_110

After you have deployed the application, you can access it at:

http://<project-id>.appspot.com/

Firefox Developer Edition_111

Reference

  1. Hello, World! – Python – Google Cloud Platform
  2. Uploading Your Application – Python – Google Cloud Platform
Advertisements

How to Add Google App Engine project to Google Developers Console

This blog post describes how to create add Google App Engine project.

Assumption

You have a Google account.

Summary

  1. Log in to Google Developers Console
  2. Create Project

Log in to Google Developers Console

In your web browser, navigate to Google Developers Console (https://console.developers.google.com/)

Google Developers Console - Firefox Developer Edition_097

After you logged in, you should be redirected to your project page.

Create Project

Click Create Project to display the new project.

Google Developers Console - Firefox Developer Edition_098

On the New Project dialog, enter:

  1. A name for your project.
    For example, I am naming my hello world project as zx-helloworld.
  2. An id for your project.
    Important This id will be used for the deployment of your project.
    For example, I am naming my hello world id as zx-helloworld.
  3. Click Create to add the project.

Note

The project id needs meet the follow criteria:

  1. Starts with a lower-case letter (a-z)
  2. May contains lower-case letters, digits (0-9) or hyphen (-)

Google Developers Console - Firefox Developer Edition_099

After you clicked Create, the dialog will close.
You should see a small popup at the lower right-hand corner of the page.

Google Developers Console - Firefox Developer Edition_100

The popup should indicate that its creating the project.

Google Developers Console - Firefox Developer Edition_100

After the project is created, it should redirect you to the project dashboard.

Google Developers Console - Firefox Developer Edition_101

The popup in the lower right corner of the page, should have a green checkbox.

Google Developers Console - Firefox Developer Edition_102

With this, your Google App Engine project can be deployed using the project id that you defined.

Blog at WordPress.com.