220 likes | 383 Views
lecture14 Programming with Google App Engine. Keke Chen. Outline. Using google app engine - walk through a simple example. How cgi works. Request: request a url, click a button, etc. Client. server. Request a url: “get” url Response: CGI returns some html code
E N D
lecture14 Programming with Google App Engine • Keke Chen
Outline Using google app engine - walk through a simple example
How cgi works Request: request a url, click a button, etc Client server Request a url: “get” url Response: CGI returns some html code Click buttons: “post” something Request : content/url sent by the client Response: content return to the client’s browser
Python “hello world” CGI program helloworld.py print 'Content-Type: text/plain'print ''print 'Hello, world!'
Preparation • Have your gmail account • Installing google apps tool • dev_appserver.py, the development web server • appcfg.py, for uploading your app to App Engine
Setup the development env Windows/mac: check http://code.google.com/appengine/downloads.html to find the package Nimbus17: already installed at /usr/local/gae Python version needs 2.5: /usr/bin/python2.5
Webapp does • Wraps the CGI functions • API for handling request/response • Python Web Server Gateway Interface (WSGI) • A framework for developing CGI programs with Python
from google.appengine.ext importwebappfrom google.appengine.ext.webapp.util importrun_wsgi_appclassMainPage(webapp.RequestHandler):defget(self):self.response.headers['Content-Type']='text/plain'self.response.out.write('Hello, webapp World!')application = webapp.WSGIApplication([('/',MainPage)],debug=True)def main(): run_wsgi_app(application)if __name__ =="__main__": main()
Configuration file • app.yaml • A standard file format for configuration • A set of API designed to access the configuration • http://www.yaml.org/ • Don’t need to know much about yaml • Check GAE’s typical configurations
App.yaml If threadsafe is set to true, use helloworld.application application: helloworld version: 1 runtime: python27 threadsafe: false api_version: 1 handlers: - url: /.* script: helloworld.py
Start development server • Use python 2.5 /usr/bin/python2.5 /usr/local/gae/dev_appserver.py helloworld/ -p port -a address
Use users service Use the google account service
Using users service 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))
Handling forms with webapp self.response.out.write(""" <html> <body> <form action="/sign" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="Sign Guestbook"></div> </form> </body> </html>""")
db - datastore db.Model GQL: sql like query language greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10") Or omit select *: greetings = Greeting.gql("ORDER BY date DESC LIMIT 10") With parameters: greetings = Greeting.gql("WHERE author = :1 ORDER BY date DESC",users.get_current_user()) greetings = Greeting.all() greetings.filter("author =", users.get_current_user()) greetings.order("-date")
Entity group Passing parameters
Use templates Templates are used to generate the similar webpages with different variables
Use stylesheet Change app.yaml Include the code for using stylesheet
Upload application Create an application at Google AppEngine Change the app name in app.yaml correspondingly Run appcfg.py update helloworld/