Jun 6, 2012

First App with Google App Engine: Python

This is a follow-on post from 'Getting Started with Google App Engine: Python'. Here we will explore using the default webapp framework that comes with the Google App Engine SDK (Note: You can replace this with another framework such as Django, but that is outside the scope of this post).

Note that we are using Python 2.7 and webapp2 for this tutorial

Quick overview

A basic application in the webapp2 framework will consist of three parts:
  • One or more handlers as defined by the RequestHandler class
  • A WSGIApplication instance that maps URLs to specific handlers
  • A configuration YAML file that tells Google App Engine to use Python 2.7
It's nothing fancy; it's supposed to be a very simple webapp framework to get you started. Now let's dive into some code....

The YAML configuration file

We will start with the configuration file because a) it's simple, and b) you shouldn't have to edit it ever again. Create a file called app.yaml and add the following:

application: almightyolive
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
An explanation of all the fields:
  • application is where you will store the appid you were given by the GAE (Google App Engine) dashboard
  • version is your code's version number. GAE offers basic version control, so updating this number will force GAE to back-up your old code an allow you revert at a later date
  • runtime the specific Python version you are using (at the time of writing Python 2.5 is also supported)
  • api_version tells GAE which API you are using. If Google ever updates their code, your application will still run on the API it was coded for
  • threadsafe is a Python 2.7 specific parameter that allows your app to handle concurrent requests
  • handlers is a list that maps specific URLs to your modules. In this case, everything will be routed to our main.app, which is what we are going to code next....

Hello World!

Create a new file called main.py and add in the following code:

# The webapp2 framework
import webapp2

#Our main handler class that just outputs "Hello Webapp World!"
class MainPage(webapp2.RequestHandler):
    # Respond to a HTTP GET request
    def get(self):
        # Output to the browser that it will receive plan text
        self.response.headers['Content-Type'] = 'text/plain'
        # Output the Hello World message
        self.response.out.write('Hello, webapp World!')

# Create our application instance that maps the root to our
# MainPage handler
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Run the python web server (right click on the project and select 'Run As..' - > 'PyDev: Google App Run') and see the results on your browser (default should be http://localhost:8080/)!

Something a bit more advanced....

Edit the main.py code as follows:

# The webapp2 framework
import webapp2

#Adds two numbers together that are passed through URL
class MainPage(webapp2.RequestHandler):
    # Respond to a HTTP GET request
    def get(self):
        # A try-catch statement
        try:
            # Grab the numbers from using GET
            first = int(self.request.get('first'))
            second = int(self.request.get('second'))

            # Outputs the addition of the numbers

            self.response.out.write("<html><body><p>%d + %d = %d</p></body></html>" % (first, second, first + second))

        # Our exception code
        except (TypeError, ValueError):
            self.response.out.write("<html><body><p>Invalid inputs</p></body></html>")

# Create our application instance that maps the root to our
# MainPage handler
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Test it out by navigating to http://localhost:8080/?first=1&second=2

References

No comments:

Post a Comment

Thanks for contributing!! Try to keep on topic and please avoid flame wars!!