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:
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.
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.
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.
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
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) 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
Post a Comment