google app engine for python - unit01: basic
DESCRIPTION
1. Introduction to Google App Engine (GAE) 2. GAE for Python 3. Using Static Files in GAE 4. Using Google User Service in GAETRANSCRIPT
Ubiquitous Computing and Ambient Networking Laboratory 1
Google App Engine (GAE)
Wei-Tsung Su (蘇維宗 )
06/30/2014 (Ver. 1.0)
Ubiquitous Computing and Ambient Networking Laboratory 2
Change LogDate Ver. Authors Description
6/30/2014 1.0 Wei-Tsung Su Google App Engine (GAE); GAE for Python; Using Static Files; Google User Service
What?
• Google Cloud Platform– App Engine provides Platform as a Service (PaaS)– Compute Engine provides Infrastrcture as a Servce (IaaS)– CloudSQL, Cloud Storage, and Cloud Datastore provide storage options– BigQuery and Hadoop provide big data processing capability
• Language supported by GAE– Python, Java, PHP, and GO– Different languages may provide different features.
Ubiquitous Computing and Ambient Networking Laboratory 3
Why?
• Easily deploy your web services to public
• Seamlessly integrate with other Google services
• Free quotas (https://developers.google.com/appengine/docs/quotas) – Code & Static Data Store (1GB free)– Google Cloud Storage (5GB free) – Blobstore Stored Data (5GB free) – API access (which depends on different Google API)– ...
Ubiquitous Computing and Ambient Networking Laboratory 4
How?
• Read Support Documentation– https://developers.google.com/appengine/
• Try Administration Console– https://appengine.google.com/– https://console.developers.google.com
• Download SDK– https://developers.google.com/appengine/downloads
Start Your Engine 3, 2, 1
Ubiquitous Computing and Ambient Networking Laboratory 5
Ubiquitous Computing and Ambient Networking Laboratory 6
GAE for Python
Ubiquitous Computing and Ambient Networking Laboratory 7
Setup Development Environment
• Install Python 2.7– https://www.python.org/download/releases/2.7.7/
• Install App Engine SDK for Python– https://developers.google.com/appengine/downloads– Ex. we assume that google_appengine/ is the SDK directory.
• Install Git (optional)– http://git-scm.com/downloads
Ubiquitous Computing and Ambient Networking Laboratory 8
How GAE for Python Works
• A Python web app interacts with the App Engine web server using Web Server Gateway Interface (WSGI) protocol.
• WSGI-compatible web application frameworks, such as– webapp2 (by default, simple)– Django– CherryPy– Pylons– web.py– web2py
Ubiquitous Computing and Ambient Networking Laboratory 9
Project Structure of GAE for Python
• Modules– Dynamic web page generation– http://webapp-improved.appspot.com/ – Filename: xxx.py (ex. helloword.py)
• Configuration File– Project configuration– Routing requests to modules– https://developers.google.com/appengine/docs/python/config/appconfig– Filename: app.yaml (壓謀 !)
• Ex. we assume that helloworld/ is the project directory
Ubiquitous Computing and Ambient Networking Laboratory 10
Module: helloworld. py• import webapp2
class MainPage(webapp2.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/ html' self.response.write('Hello, World!')
app = webapp2.WSGIApplication([ ('/', MainPage),], debug=True)
• For security issue, you should disable debug which will display a stack trace in the browser when a handler raises an exception.
Ubiquitous Computing and Ambient Networking Laboratory 11
Configuration File: app.yaml• application: your-app-idversion: 1runtime: python27api_version: 1threadsafe: true
handlers:- url: /.* script: helloworld.app
• How to test your project locally– # google_appengine/dev_appserver.py helloworld/– Test your web app by visiting the URL: http://localhost:8080
Hello, World!
Ubiquitous Computing and Ambient Networking Laboratory 12
Ubiquitous Computing and Ambient Networking Laboratory 13
Using Static Files
• Sometimes, you may put static files, such images, CSS stylesheets, JavaScript code, and so on, into your web applications.
• For example, – <img src="/images/logo.png" >
• Unfortunately, you cannot just create a directory and put thestatic files into the directory.
• You also need to add route intocon figuration file: app.yaml– - url: /images static_dir: images
Ubiquitous Computing and Ambient Networking Laboratory 14
Google User Service• Google user service (https://developers.google.com/appengine/docs/python/users/)• Import user service module
– from google.appengine.api import users
• Get current user– user = users.get_current_user() # Google Account, Google Apps Account, OpenID
• Access user information– user.nickname() – user.email() – user.user_id()
• Administrator check– user.is_current_user_admin()
• Create login/logout pages– users.create_login_url('[destination URL]')– users.create_logout_url('[destination URL]')
Ubiquitous Computing and Ambient Networking Laboratory 15
Google User Service - Example• from google.appengine.api import users
import webapp2
class MainPage(webapp2.RequestHandler): def get(self): user = users.get_current_user() if user: self.response.headers['Content-Type'] = 'text/ html' self.response.write('Hello, World, %s!<br>', %(user.nickname())) self.response.write('<a href="%s">Sign Out</a>' % users.create_logout_url(self.request.url)) else: self.redirect(users.create_login_url(self.request.url) )
app = webapp2.WSGIApplication([ ('/', MainPage), ], debug=True)
Hello, World, User!
Ubiquitous Computing and Ambient Networking Laboratory 16
Deploy Your Application
• Step 1: Create Project in Developer Console– https://console.developers.google.com– You will get a random project id (Ex. your-app-id).– The public URL of your web application will be
• http://your-app-id.appspot.com
• Step 2: Check if the application ID in app.yaml matches your-app-id– application: your-app-id
• Step 3: Deploy your local project– #google_appengine/appcfg.py update helloworld/– Test your web app by visiting the URL: http://your-app-id.appspot.com
Ubiquitous Computing and Ambient Networking Laboratory 17
Q&A
You can clone the sample code bygit clone https://github.com/ucanlab/gae_python_basic_helloworld.git
or visiting https://github.com/ucanlab/gae_python_basic_helloworld
Ubiquitous Computing and Ambient Networking Laboratory 18