Google App Engine is a platform for developing and hosting web applications.App engine applications are easy to build, easy to maintain. You can build your application using Python, Java or Go environments. You just upload your application, and it's ready to serve to your users. With App Engine you write your application code, test it on your local machine and upload it to Google. You can create an account and publish an application that people can use right away at no charge from Google, and with no obligation.
This tutorial describes how to develop and deploy a simple Python 2.7 project with Google App Engine. You can build web applications using the Python programming language, and take advantage of the many libraries, tools and frameworks for Python.A Python web app interacts with the App Engine web server using the WSGI protocol, so apps can use any WSGI-compatible web application framework. App Engine includes a simple web application framework. Apps can use the App Engine Datastore for reliable, scalable persistent storage of data.Apps use the URL Fetch service to access resources over the web, and to communicate with other hosts using the HTTP and HTTPS protocols.
For our App Engine implementation use Python 2.7v and use the python Software Development Kit(SDK).Download SDK for linux and extract it on your Home folder.You develop and upload Python applications for Google App Engine using the App Engine Python software development kit (SDK).
Let's begin by a simple 'Hello World!' application that show the message 'Hello World! in our browser.
STEP-1
Create a directory helloworld. all files for this application reside in this directory. create a file named helloworld.py
Give the following content in it.
This Python script responds to a request with an HTTP header that describes the content and the display Hello, world!. webapp2 is light weighted web frame work. This webapp2 has two parts a request handler and a WSGIApplication instants that route incoming request to handle based on URLs.
STEP-2
Create a configuration file app.yaml and add following contents:
STEP-3
With these python script and configuration file mapping every URL to the handler. Start the web server with the following command, giving it the path to the helloworld directory:
http://localhost:8080/
Considering an example students details system.The application will provide you enter details of students and will get back all data by sorting order by age and mark, also providing delete option and search option. Before we get started, let’s create the folders needed for this application:
the configuration 'app.yaml' file in above example changed as
For our App Engine implementation use Python 2.7v and use the python Software Development Kit(SDK).Download SDK for linux and extract it on your Home folder.You develop and upload Python applications for Google App Engine using the App Engine Python software development kit (SDK).
Let's begin by a simple 'Hello World!' application that show the message 'Hello World! in our browser.
STEP-1
Create a directory helloworld. all files for this application reside in this directory. create a file named helloworld.py
CQ40-Notebook-PC:~$ mkdir helloworld
CQ40-Notebook-PC:~$ cd helloworld/
CQ40-Notebook-PC:~/helloworld$ vi helloworld.py
CQ40-Notebook-PC:~$ cd helloworld/
CQ40-Notebook-PC:~/helloworld$ vi helloworld.py
Give the following content in it.
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
This Python script responds to a request with an HTTP header that describes the content and the display Hello, world!. webapp2 is light weighted web frame work. This webapp2 has two parts a request handler and a WSGIApplication instants that route incoming request to handle based on URLs.
STEP-2
Create a configuration file app.yaml and add following contents:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.app
This configuration file describes the application identifier is helloworld. Every new application on App Engine has a unique application identifier. This is version number 1 of this application's code. This code runs in the python27 runtime environment, version "1". Additional runtime environments and languages may be supported in the future.Every request to a URL whose path matches the regular expression /.* (all URLs) should be handled by the app object in the helloworld module.version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.app
STEP-3
With these python script and configuration file mapping every URL to the handler. Start the web server with the following command, giving it the path to the helloworld directory:
google_appengine/dev_appserver.py helloworld/
Now the web server is ready. You can test the application by visiting the following URL in your web browser:http://localhost:8080/
STUDENTS DETAILS APPLICAION:
Considering an example students details system.The application will provide you enter details of students and will get back all data by sorting order by age and mark, also providing delete option and search option. Before we get started, let’s create the folders needed for this application:
/student
/stylesheets
app.yaml
student.py
main.html
details.html
search.html
remove_details.html
/stylesheets
app.yaml
student.py
main.html
details.html
search.html
remove_details.html
the configuration 'app.yaml' file in above example changed as
application: students-jastech
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: student.app
libraries:
- name: jinja2
version: latest
In python script student.py, includes a simple application frame work webapp2.It have two parts one or more RequestHandler classes that process requests and build responses and a WSGIApplication instance that routes incoming requests to handlers based on the URL.Using class defines all pages.version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: student.app
libraries:
- name: jinja2
version: latest
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'html'
template = jinja_environment.get_template('main.html')
self.response.out.write(template.render({}));
def get(self):
self.response.headers['Content-Type'] = 'html'
template = jinja_environment.get_template('main.html')
self.response.out.write(template.render({}));
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. Typically, the method sets properties on self.response to prepare the response, then exits. webapp2 sends a response based on the final state of the MainPage instance. where we use templates instead of direct html code in this python script. The template file main.html is executed using jinja2. Jinja2 is a modern and designer friendly templating language for Python. To use template in python script import:
import jinja2
Next page is details.The get part of the page use a template 'details.html' file to provide enter the details of student.
class details(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('details.html')
self.response.out.write(template.render({}));
def post(self):
name = self.request.get('studentname')
sex = self.request.get('sex')
age = self.request.get('age')
dob = self.request.get('dob')
mark = self.request.get('mark')
data = student(key_name=name, Name=name,Sex=sex ,Age=age, DOB=dob, Mark=mark)
data.put()
self.redirect("/")
def get(self):
template = jinja_environment.get_template('details.html')
self.response.out.write(template.render({}));
def post(self):
name = self.request.get('studentname')
sex = self.request.get('sex')
age = self.request.get('age')
dob = self.request.get('dob')
mark = self.request.get('mark')
data = student(key_name=name, Name=name,Sex=sex ,Age=age, DOB=dob, Mark=mark)
data.put()
self.redirect("/")
The post part of the page get the values from the details.html and store it in variables.
For handling data, define a class student. Define the table name and entry details. To use the data modeling API, import the google.appengine.ext.db module:
For handling data, define a class student. Define the table name and entry details. To use the data modeling API, import the google.appengine.ext.db module:
from google.appengine.ext import db
following defines a data model for student.This defines a student model with six properties
class student(db.Model):
"""Models a student detail entry with name, roll number, DOB, Mark"""
Name = db.StringProperty()
Sex = db.StringProperty()
Age = db.StringProperty()
DOB = db.StringProperty()
Mark = db.StringProperty()
Date = db.DateTimeProperty(auto_now_add=True)
"""Models a student detail entry with name, roll number, DOB, Mark"""
Name = db.StringProperty()
Sex = db.StringProperty()
Age = db.StringProperty()
DOB = db.StringProperty()
Mark = db.StringProperty()
Date = db.DateTimeProperty(auto_now_add=True)
Now retrieve the stored data using GQLQuery. Get data from the table student sort by age. Using a iteration function separate all the data.
details = db.GqlQuery('SELECT * FROM student ORDER BY Age ASC')
self.response.headers['Content-Type'] = 'html'
for i in details:
self.response.write('<p>'+i.Name +' '+i.Sex+' '+i.Age +' '+i.DOB+' '+i.Mark+ '</p>')
self.response.headers['Content-Type'] = 'html'
for i in details:
self.response.write('<p>'+i.Name +' '+i.Sex+' '+i.Age +' '+i.DOB+' '+i.Mark+ '</p>')
For remove the details of student using key_name, was set when storing data. Where 'remove_details' is from get part of the class remove_details
remove_details=self.request.get('remove_details')
address_key = db.Key.from_path('student', remove_details)
db.delete(address_key)
address_key = db.Key.from_path('student', remove_details)
db.delete(address_key)
For searching detail of a student by name use the 'search' variable from method get in class search_details.
search=self.request.get('search')
data = db.GqlQuery("""SELECT * FROM student WHERE Name = :1 """, search)
self.response.headers['Content-Type'] = 'html'
self.response.writ(html code is here)
for i in data:
self.response.write('<p>'+i.Name +' '+i.Sex+' '+i.Age+' '+i.DOB+' '+i.Mark+ '</p>')
data = db.GqlQuery("""SELECT * FROM student WHERE Name = :1 """, search)
self.response.headers['Content-Type'] = 'html'
self.response.writ(html code is here)
for i in data:
self.response.write('<p>'+i.Name +' '+i.Sex+' '+i.Age+' '+i.DOB+' '+i.Mark+ '</p>')
The WSGIApplication that use to route incoming request to handle based on URLs. Here define all URLs and corresponding routing.
app = webapp2.WSGIApplication([('/', MainPage),('/details', details),('/view', view), ('/sort_age', sort_age), ('/sort_mark', sort_mark), ('/search_details', search_details), ('/remove_details', remove_details), ('/searchresult', searchresult)], debug=True)
For running the application use the command
The web server is now running visit http://localhost:8080/
google_appengine/dev_appserver.py student/
Uploading the application:
register the application using the link https://appengine.google.com/, you upload it to your website using appcfg.py, a command-line tool provided in the SDK. Change the value of "application: "setting from "helloworld" to your registered application id(my id "student") in your "app.yaml" file . To upload your finished application to Google App Engine, run the following command:
google_appengine/appcfg.py update student/
Now run your application from Google server.Click here to see the application student information system.
Source code is available in github, you can download it from below link:
https://github.com/jaseemkp/google-app-engine/archive/master.zip
No comments:
Post a Comment