Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



The webapp2 Framework

Learning objectives
  • Learn how to build and deploy an App Engine app, a simple guestbook
Prerequisites
Related

Amy Unruh, Dan Sanderson, Oct 2012
Google Developer Relations

Introduction

This class picks up where the Introduction to App Engine course left off. As a starting point, we assume that you have built a simple Python 2.7 "Hello World" application as described in the Introduction to App Engine course, or have auto-generated one via File > New Application from the Launcher menu. For reference purposes, we'll assume that you've named your application directory helloworld , and that the Python file containing your request handler is named main.py . Confirm also that your app.yaml file specifies the Python 2.7 runtime.

In this lesson, we'll continue exploring the structure of an App Engine application by looking at App Engine's built-in support for the Web Server Gateway Interface (WSGI) standard.

The WSGI standard is simple, but it would be cumbersome to write all of the code that uses it by hand. Web application frameworks handle these details for you, so you can focus your development efforts on your application's features. Google App Engine supports any framework written in pure Python that speaks WSGI, and includes a simple framework, webapp2 , which we'll be using throughout this course. The webapp2 framework is already installed in the App Engine environment and in the SDK, so you can use it right "out of the box"; there's no need to bundle it with your application code. In this lesson, you'll learn how to incorporate it in your App Engine application.

Hello, webapp2!

Before we get started, confirm that your app.yaml file specifies the Python 2.7 runtime. If you auto-generated app.yaml via the Launcher, you'll see something along these lines:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.1"

Now, let's take a closer look at the Python code. A webapp2 application has two parts:

  • One or more RequestHandler classes that process requests and build responses
  • A WSGIApplication instance that routes incoming requests to handlers based on the URL

Let's take a look at the Greeting application you generated in the Introduction to App Engine class:

import webapp2

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

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

This code defines one request handler, MainPage , mapped to the root URL ( / ). When webapp2 receives an HTTP GET request to the URL / , it instantiates the MainPage class and calls the instance's get method. Inside the method, information about the request is available using self.request . Typically, the method sets properties and call methods on self.response to prepare the response, then exits. webapp2 sends a response based on the final state of the response object of the MainPage instance.

The application itself is represented by a webapp2.WSGIApplication instance. The parameter debug=True passed to its constructor tells webapp2 to print stack traces to the browser output if a handler encounters an error or raises an uncaught exception. You may wish to remove this option from the final version of your application.

We'll use a few more features of webapp2 later in this course. For more information, see the webapp2 documentation .

Summary and Review

Frameworks make web application development easier, faster, and less error-prone. webapp2 is just one of many such frameworks available for Python, which you can use to add features to your application.

In the next lesson , we'll start with a look at the Users service.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.