Monday, 10 December 2012

Python Webapp2 framework

App engine includes simple web application framework called webapp2. Webapp2 is already installed in google app engine environment. It can be used in python by importing webapp2 library.
      Webapp2 includes two features:
         Request Handler class that processes requests and build classes
         WSGIApplication instance which routes incoming requests to handlers.

 import webapp2

     class MainPage(webapp2.RequestHandler):
         def get(self):
             self.response.write('hello')

 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. webapp2 sends a response based on the final state 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.

Google app engine is equipped with features which provides an interactive session with users:
  • User Service:  get_current_user() function checks for the user is signed in or not.If the user is signed in then returns the user object otherwise returns None and users.create_login_url() returns the google account sign in page so as to sign in after the check for the current user fails.
  • High Replication DataStore : which uses paxos algorithm to replicate data along multiple data centres.The Datastore is extremely resilient in the face of catastrophic failure, but its consistency guarantees may differ from what you're familiar with. It includes a data modelling API which can be used by importing the google.appengine.ext.db module. Datastore has a sophisticated query engine for datamodels. Like sql language we can call gql by which provides access to the App Engine Datastore query engine's features using a familiar syntax.
  • Templates  : There are many templating system in python two of them are jinja2 and Django.  App Engine includes the Django and Jinja2 templating engines. To configure jinja2 library by adding the following in app.yaml file     
                                libraries:
                                 -  name: jinja2
                                    version: latest

for a template file one has to implement an html file which specifies the template of the app, by using that template we can implement jinja_environment.get_template(name) takes the name of a template file, and returns a template object. template.render(template_values) takes a dictionary of values, and returns the rendered text. The template uses Jinja2 templating syntax to access and iterate over the values, and can refer to properties of those values. In many cases, you can pass datastore model objects directly as values, and access their properties from templates.

1 comment:

  1. Hai Prasanth kumar. I am a software developer,currently working on google app engine,webapp2 framework with python as platform.
    I have some confussion on sessions section.how can i put an user object into session or how can i pass an object from view to template
    can u please explain with example.do you have any notes on sessions,authentications and internationalization,templates kindly provide some help.
    ..............................................
    dineshmandepudi@gmail.com

    ReplyDelete