Google App Engine lets you run web applications on Google's
infrastructure. With App Engine, you only pay for what you use. There are no set-up
costs and no recurring fees. App Engine applications are easy to build, easy to maintain. Different data storage options it can be provided. In App Engine there is no servers to maintain an application. The applications are generally build and test on your local host, after the successful test it will upload to Google, then it's ready to serve users.
You can build your app using Python , Java , Go runtime environments. These runtime environments are built to ensure that your application
runs quickly, securely, and without interference from other apps on the
system. App Engine can deal with heavy amount of data and it provide following features.
- Dynamic web serving, with full support for common web technologies
- Persistent storage with queries, sorting and transactions
- Automatic scaling and load balancing
- APIs for authenticating users and sending email using Google
Accounts
- A fully featured local development environment that simulates
Google App Engine on your computer
- Task queues for performing work outside of the scope of a web
request
- Scheduled tasks for triggering events at specified times and
regular intervals
This post will consider only the App
engine with Python environment. Using these environment you can
implement your app in Python programming language and run it on an
optimized Python interpreter. The Python environment provides rich
Python API's for the data store, Google accounts, URL fetch and email
services.
For our app engine implementation use Python 2.7(
download ) version, and use the Python software
development kit(
download). The SDK versions are
available for Linux, Mac-Os and Windows. Download SDK for Linux and
unzip it on your home, we will only handle Linux Development
environment.
Basic application development
Let's begin by implementing a tiny application that displays a short
message "Hello World". Create a directory helloworld. All the files for this application inside
in these directory. Inside the helloworld directory, create a file name helloworld.py
and copy the following
contents
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
This Python script responds to request with an HTTP header that
describes the content and message hello,
world!, the webapp2, it is a light weighted web frame work for Python.
The webapp2 application has two parts a request handler and a
WSGIApplication. The requestHandler classes that process request and
build response. The WSGIApplication instance that route incoming request
to handle based on URLs.
Next step to create a configuration file. An App Engine
applications has configuration file called app.yaml, this file describes which
handler scripts should used for which URLs. Inside the helloworld directory, create a file named app.yaml and copy the following contents.
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: helloworld.app
Now you can test your first app using SDK using the command
google_appengine/dev_appserver.py helloword/
Implementing a Student application
Considering an example student information system.The application will
provide you enter details of students and will get back all data by
sorting order by mark and age, also providing delete option and search
option.
Source code is available in github, you can clone it into the your local
machine by running the code
git clone git@github.com:rahulthrissur/studentapp.git
Now you can see a folder in your local machine as student.
student/
app.yaml
test.html
test.py
Just go through the some parts of code, for these application
changed the app.yaml file as
- url: /.*
script: <python file name>.app
From python
script test.py
app = webapp2.WSGIApplication([('/', MainPage),('/details', details), ('/sort1', sort1), ('/sort2', sort2),('/search',search), ('/remove', remove), ('/searchresult', searchresult)],
debug=True)
The WSGIApplication that use to route incoming request to handle
based on URLs. Here define all URLs and corresponding routing.
Using class define all the pages.
class <page name>(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'html'
self.response.write("html contents")
You can write simple html file inside the python script but it is not a
good practice, use always templates, will discuss later.
Designing of html page refer the links
Consider the next page 'details'.
class details(webapp2.RequestHandler):
def get(self):
self.response.out.write(template.render("test.html",{}));
def post(self):
name=self.request.get('student_name')
sex=self.request.get('sex')
age=self.request.get('age')
mark=self.request.get('mark')
data=student(key_name =name, Name=name, Sex=sex, Age=age, Mark=mark)
data.put()
self.redirect("/")
The get part of the page use a template,
test.html file to
provide enter the details of student. The
test.html
file was defined separate and rendered to python script. To use templat
in python script import:
from google.appengine.ext.webapp import template
The template is separate the presentation document from its data, also
handle the variables in template files.
The post part of the page get the values from the
test.html and store it in variables. For handling data,
define a class. Define the table name and entry details.
from google.appengine.ext import db
class student(db.Model):
Name = db.StringProperty()
Age = db.StringProperty()
Sex = db.StringProperty()
Mark = db.StringProperty()
Selt.redirect use to redirect to main page
self.redirect("/")
Now retrieve the stored data using GQLQuery. Get data from the table
student sort by age. Using a iteration function separate all the data.
self.response.write("Details of all student sorted by Age")
data = db.GqlQuery('SELECT * FROM student ORDER BY Age ASC')
self.response.headers['Content-Type'] = 'text/plain'
for i in data:
self.response.write('\n'+i.Name +' '+i.Sex+' '+i.Age+' '+i.Mark+ '\n')
For delete the details of student using key_name, was set when storing
data.
address_k = db.Key.from_path('student', <key_name>)
db.delete(address_k)
For searching detail of a student by name.
For running the application use the command
google_appengine/dev_appserver.py student/
Upload the application
You can it upload into Google server. Follow the steps:
Edit the
app.yaml file, then change the value of
application: setting from
helloworld to your registered application id(my id myApp1) and using the command upload
google_appengine/appcfy.py update student/
Now run your application from Google server.