Skip to main content

Google App Engine with Python 2.7

webapp2 framework 
webapp2 is a simple web application framework included in App Engine. It is already installed in the App Engine environment and in the SDK. webapp2 framework 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
Consider the below given code

import webapp2

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

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
Here MainPage is the request handler mapped to the root url. When an HTTP request to the url is recived, the get() method is instantiated. self.request extracts the information about the request and self.response to prepare the response. webapp2.WSGIApplication instance represents the application itself.

Using user services 
Several useful services are provided by the App Engine inorder to improve user interaction. Using user services, the application is integrated with google user accounts. The code given above is edited for implementing user services by adding the below given commands.

user = users.get_current_user()

        if user:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('Hello, ' + user.nickname())
        else:
            self.redirect(users.create_login_url(self.request.uri)
Here the user object fetches the details of the current user. If the user is signed in using the google account, the nickname associated with the user account is displayed. Else the page is redirected to the login page of google accounts.

Using the Datastore

The high replication datastore uses the paxos algorithm for replication of data. Data is written to the datastore in objects known as entities. The Datastore is extremely resilient in the face of catastrophic failure, but its consistency guarantees may differ from what you're familiar with. To use the data modeling API, import the google.appengine.ext.db module with the below command.


from google.appengine.ext import db
Usage of Templates 
Template system separates the html code into a separate file with special syntax to indicate where the data from the application appears. The two main templating engines included in the App Engine are Django and Jinja2. The corresponding changes are to be made in the configuration file to make the Jinja2 template available. For that the below given code is added to the app.yaml file.

libraries
- url: /.*
  version: latest
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. Please reffer here for more details regarding the topic.

Comments

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Elements of Programming in Scheme

A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this. primitive expressions , which represent the simplest entities the language is concerned with, means of combination , by which compound elements are built from simpler ones, and means of abstraction , by which compound elements can be named and manipulated as units. Some very basic ideas in manipulating with the data are given below. Expressions   A very simple expression is a number. Simple expressions are combined to form complex ones. For example:                   (+ 2  3  4) is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:              ...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...