Wednesday, December 17, 2008

Using google webapp framework in google appengine

Google includes python based web application framework with app engine.This framework pretty similar with MVC pattern.A webapp application has three 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
  • a main routine that runs the WSGIApplication using a CGI adaptor.
Now the time to look at example code of webapp framwork.We are going to write webapp.py


from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

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

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

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()

our app.yaml
application: webapptest

version: 1

runtime: python

api_version: 1



handlers:

- url: /

script: webapp.py
http://code.google.com/appengine/docs/webapp/
What the above code does

  1. The webapp module is in the google.appengine.ext
  2. This code defines one request handler, MainPage, mapped to the root URL (/).We can map diffrent urls with diffrent handlers
  3. When webapp 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 on self.response to prepare the response.
Now copy the webapptest folder to google appengine folder and type localhost:8080/ !

No comments: