Explaining the webapp2 Framework
App Engine includes a simple web application framework, called
webapp2
.
A
webapp2
application 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 the Users Service
Google App Engine provides several useful services based on Google
infrastructure, accessible by applications using libraries included with
the SDK. One such service is the Users service, which lets our application integrate with Google user accounts. With the Users service, our users can use the Google accounts they already have to sign in to our application.
import webapp2 from google.appengine.api import users class MainPage(webapp2.RequestHandler): def get(self): 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)) app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
Handling Forms with webapp2
This version has two handlers:
MainPage
, mapped to the URL /
, displays a web form. Guestbook
, mapped to the URL /sign
, displays the data submitted by the web form.The Guestbook
handler has a post()
method instead of a get()
method. This is because the form displayed by MainPage
uses the HTTP POST method (method="post"
)
to submit the form data. If for some reason we need a single handler
to handle both GET and POST actions to the same URL, we can define a
method for each action in the same class.The code for the post()
method gets the form data from self.request
. Before displaying it back to the user, it uses cgi.escape()
to escape HTML special characters to their character entity equivalents. cgi
is a module in the standard Python library.Using the Datastore
App Engine's data repository, the High Replication Datastore (HRD),
uses the
Paxos algorithm
to replicate data across multiple data centers .Data is written to the Datastore
in objects known as
entities.
Each entity has a that uniquely identifies it.An entity can optionally designate another entity
as its parent; the first entity is a child of the parent
entity. The entities in the Datastore thus form a hierarchically structured
space similar to the directory structure of a file system. An entity's parent,
parent's parent, and so on recursively, are its ancestors; its
children, children's children, and so on, are its descendants. An
entity without a parent is a root entity.
Using Jinja2 Templates
Add the following code to the bottom of the configuration file app.yaml
libraries:- name: jinja2 version: latest
Add following code to the top of the mainpage handler
import jinja2
import os jinja_environment = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
Replace the
MainPage
handler with code that resembles the following:
class MainPage(webapp2.RequestHandler): def get(self): guestbook_name=self.request.get('guestbook_name') greetings_query = Greeting.all().ancestor( guestbook_key(guestbook_name)).order('-date') greetings = greetings_query.fetch(10) if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' else: url = users.create_login_url(self.request.uri) url_linktext = 'Login' template_values = { 'greetings': greetings, 'url': url, 'url_linktext': url_linktext, } template = jinja_environment.get_template('index.html') self.response.out.write(template.render(template_values))
Using Static Files
Images, CSS stylesheets, JavaScript code, movies and Flash animations
are all typically stored with a web application and served directly to
the browser.
We create a configuration file app.yaml with the following content
application: helloworldversion: 1runtime: python27api_version: 1threadsafe: true handlers:- url: /stylesheets static_dir: stylesheets - url: /.* script: helloworld.app libraries:- name: jinja2 version: latest
Create a
css file with following content for set background colour and font etc
body {
font-family: Verdana, Helvetica, sans-serif;
background-color: #DDDDDD;
}
Using following codes in html file for access the css file
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
After testing we can upload our file to google app engine using foolowing commands
python appcfg.py update "set path"
No comments:
Post a Comment