Google App Engine for Python - Unit01: Basic

1,000 views

Published on

1. Introduction to Google App Engine (GAE)
2. GAE for Python
3. Using Static Files in GAE
4. Using Google User Service in GAE

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,000
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Google App Engine for Python - Unit01: Basic

  1. 1. Google App Engine (GAE) Wei-Tsung Su (蘇維宗) ellington.su@gmail.com 06/30/2014 (Ver. 1.0) Ubiquitous Computing and Ambient Networking Laboratory 1
  2. 2. Change Log Date Ver. Authors Description 6/30/2014 1.0 Wei-Tsung Su Google App Engine (GAE); GAE for Python; Using Static Files; Google User Service Ubiquitous Computing and Ambient Networking Laboratory 2
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. GAE for Python Ubiquitous Computing and Ambient Networking Laboratory 6
  7. 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 7
  8. 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 8
  9. 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 9
  10. 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 10
  11. 11. Configuration File: app.yaml • application: your-app-id version: 1 runtime: python27 api_version: 1 threadsafe: 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 Ubiquitous Computing and Ambient Networking Laboratory 11
  12. 12. Hello, World! Ubiquitous Computing and Ambient Networking Laboratory 12
  13. 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 the static files into the directory. • You also need to add route into con figuration file: app.yaml – - url: /images static_dir: images Ubiquitous Computing and Ambient Networking Laboratory 13
  14. 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 14
  15. 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) Ubiquitous Computing and Ambient Networking Laboratory 15
  16. 16. Hello, World, User! Ubiquitous Computing and Ambient Networking Laboratory 16
  17. 17. 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
  18. 18. Q&A You can clone the sample code by git 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

×