Google App Engine in 40 minutes (the absolute essentials)

670 views
611 views

Published on

This talk covers just the stuff needed to get you up-to-speed with Google App Engine and its associated technologies (based on the Python run-time, of course). In addition to a bit of talking, Paul will also demo a working webapp built and deployed on the App Engine cloud... all in 40 minutes.

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

No Downloads
Views
Total views
670
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Google App Engine in 40 minutes (the absolute essentials)

  1. 1. Google App Engine in 40 Minutes (The Absolute Essentials) Paul Barry – Institute of Technology, Carlow in Ireland PyCon Ireland 2011 2
  2. 2. Grab the slides:http://paulbarry.itcarlow.ie/GAE.pdf
  3. 3. 4
  4. 4. 5
  5. 5. The Absolute Essentials in 40 Minutes... 6
  6. 6. What is Google App Engine? 7
  7. 7. Cloud-based Application Deployment Environment 8
  8. 8. Integrated Collection of Google APIs 9
  9. 9. Guidos Playground 10
  10. 10. Is App Engine “just another framework”? 11
  11. 11. It is all about your data... 12
  12. 12. Think of App Engine as a highly scalable,distributed database server that you can program with Python 2.5... 13
  13. 13. Think of App Engine as a highly scalable,distributed database server that you can program with Python 2.5... and (cough) Java and (cough, cough) Go 14
  14. 14. MVC 15
  15. 15. Lets solve a real problem... 16
  16. 16. 17
  17. 17. 18
  18. 18. 19
  19. 19. 20
  20. 20. Step 1 Download the SDK from: http://code.google.com/appengine/and sign-up for an App Engine account ID 21
  21. 21. Step 2Create a new project... 22
  22. 22. ...by creating an app.yaml file application: dwwgapp version: 1 runtime: python api_version: 1 handlers: - url: /static static_dir: static - url: /.* script: dwwgapp.py 23
  23. 23. Step 3Model Your Data 24
  24. 24. Create dwwgDB.py (1 of 3)from google.appengine.ext import db 25
  25. 25. Create dwwgDB.py (2 of 3)from google.appengine.ext import dbclass Sighting(db.Model): 26
  26. 26. 27
  27. 27. Create dwwgDB.py (3 of 3)from google.appengine.ext import dbclass Sighting(db.Model): name = db.StringProperty() email = db.StringProperty() date = db.DateProperty() time = db.TimeProperty() location = db.StringProperty() fin_type = db.StringProperty() whale_type = db.StringProperty() blow_type = db.StringProperty() wave_type = db.StringProperty() 28
  28. 28. Step 4Define your UI in HTML 29
  29. 29. The header.html template<html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ title }}</h1> 30
  30. 30. The footer.html template <p> {{ links }} </p> </body></html> 31
  31. 31. The form_start.html template<form method=”POST” action=”/”> <table> 32
  32. 32. The form_end.html <tr><th>&nbsp;</th> <td><input type="submit" value="{{ sub_title }}"></td> </tr> </table></form> 33
  33. 33. Render some HTMLfrom google.appengine.ext.webapp import templatehtml = template.render(templates/header.html, {title: Report a Possible Sighting}) 34
  34. 34. Rendering a Pagefrom google.appengine.ext.webapp import templatehtml = template.render(templates/header.html, {title: Report a Possible Sighting})html = html + template.render(templates/form_start.html, {})# Do something in here to render the form?!?!?!?!?!?!?html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting})html = html + template.render(templates/footer.html, {links: }) 35
  35. 35. Step 5Write code to render your form 36
  36. 36. Django Forms to the Rescue!from google.appengine.ext.db import djangoformsimport dwwgDBclass SightingForm(djangoforms.ModelForm): class Meta: model = dwwgDB.Sighting 37
  37. 37. Rendering a Page, Againfrom google.appengine.ext.webapp import templatehtml = template.render(templates/header.html, {title: Report a Possible Sighting})html = html + template.render(templates/form_start.html, {})html = html + str(SightingForm())html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting})html = html + template.render(templates/footer.html, {links: }) 38
  38. 38. Step 6Tie it all together with logic 39
  39. 39. You need to start your webappfrom google.appengine.ext import webappfrom google.appengine.ext.webapp.util import run_wsgi_app …app = webapp.WSGIApplication([(/.*, SightingInputPage)], debug=True)def main(): run_wsgi_app(app)if __name__ == __main__: main() 40
  40. 40. Remember the app.yaml file? application: dwwgapp version: 1 runtime: python api_version: 1 handlers: - url: /static static_dir: static - url: /.* script: dwwgapp.py 41
  41. 41. Put this code in dwwgapp.pyfrom google.appengine.ext import webappfrom google.appengine.ext.webapp.util import run_wsgi_app …app = webapp.WSGIApplication([(/.*, SightingInputPage)], debug=True)def main(): run_wsgi_app(app)if __name__ == __main__: main() 42
  42. 42. Whats this SightingInputPage thing? 43
  43. 43. Responding to a GET Requestclass SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) 44
  44. 44. Step 7Take your local app for a spin 45
  45. 45. Click the “Run” button... or$ dev_appserver.py dwwgapp 46
  46. 46. Ta Da! :-( 47
  47. 47. Looks kinda shitty, doesnt it? 48
  48. 48. Add a little CSS goodness..<link type="text/css" rel="stylesheet" href="/static/dwwg.css" /><link type="text/css" rel="stylesheet" href="/static/styledform.css" /> 49
  49. 49. ...and some code to dwwgDB.py_FINS = [Falcate, Triangular, Rounded]_WHALES = [Humpback, Orca, Blue, Killer, Beluga, Fin, Gray, Sperm]_BLOWS = [Tall, Bushy, Dense]_WAVES = [Flat, Small, Moderate, Large, Breaking, High] ...location = db.StringProperty(multiline=True)fin_type = db.StringProperty(choices=_FINS)whale_type = db.StringProperty(choices=_WHALES)blow_type = db.StringProperty(choices=_BLOWS)wave_type = db.StringProperty(choices=_WAVES) 50
  50. 50. Ta Da! :-) 51
  51. 51. Step 8Do something with your data 52
  52. 52. You need to POST data! POST data with put() 53
  53. 53. class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) 54
  54. 54. class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) def post(self): 55
  55. 55. class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) def post(self): new_sighting = dwwgDB.Sighting() 56
  56. 56. class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) def post(self): new_sighting = dwwgDB.Sighting() new_sighting.name = self.request.get(name) new_sighting.email = self.request.get(email) new_sighting.date = self.request.get(date) new_sighting.time = self.request.get(time) new_sighting.location = self.request.get(location) new_sighting.fin_type = self.request.get(fin_type) new_sighting.whale_type = self.request.get(whale_type) new_sighting.blow_type =self.request.get(blow_type) new_sighting.wave_type = self.request.get(wave_type) 57
  57. 57. class SightingInputPage(webapp.RequestHandler): def get(self): html = template.render(templates/header.html, {title: Report a Possible Sighting}) html = html + template.render(templates/form_start.html, {}) html = html + str(SightingForm()) html = html + template.render(templates/form_end.html, {sub_title: Submit Sighting}) html = html + template.render(templates/footer.html, {links: }) self.response.out.write(html) def post(self): new_sighting = dwwgDB.Sighting() new_sighting.name = self.request.get(name) new_sighting.email = self.request.get(email) new_sighting.date = self.request.get(date) new_sighting.time = self.request.get(time) new_sighting.location = self.request.get(location) new_sighting.fin_type = self.request.get(fin_type) new_sighting.whale_type = self.request.get(whale_type) new_sighting.blow_type =self.request.get(blow_type) new_sighting.wave_type = self.request.get(wave_type) new_sighting.put() 58
  58. 58. And dont forget to say “Thanks!”html = template.render(templates/header.html, {title: Thank you!})html = html + "<p>Thank you for providing your sighting data.</p>"html = html + template.render(templates/footer.html, {links: Enter <a href="/">another sighting</a>.})self.response.out.write(html) 59
  59. 59. 60
  60. 60. Step 9Deploy to Googles cloud 61
  61. 61. Click the “Deploy” button... or$ appcfg.py upload dwwgapp/ 62
  62. 62. http://dwwgapp.appspot.com 63
  63. 63. Step 10Realise your not done... 64
  64. 64. Damn That Spam!Some smart-alec posted a sightingof a Great White Shark on the top of Mount Leinster... 65
  65. 65. What to do? 66
  66. 66. A tiny config tweak to app.yaml... application: dwwgapp version: 1 runtime: python api_version: 1 handlers: - url: /static static_dir: static - url: /.* script: dwwgapp.py login: required 67
  67. 67. ...and two tiny code changes Add this to the dwwgDB.py model: which_user = db.UserProperty()Add this to your post() method in dwwgapp.py:new_sighting.which_user = users.get_current_user() 68
  68. 68. http://dwwgapp.appspot.com 69
  69. 69. http://appengine.google.com 70
  70. 70. And after all that,what do you end up with? 71
  71. 71. Happy Clients! 72
  72. 72. Want to learn moreabout this talks example code? 73
  73. 73. Head First Python 74
  74. 74. Dont forget to check outthe App Engine Tutorial on Sunday with Seán Murphy 75
  75. 75. 76

×