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.
13. Think of App Engine as a highly scalable,
distributed database server that you can program
with Python 2.5...
13
14. 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
34. Render some HTML
from google.appengine.ext.webapp import template
html = template.render('templates/header.html',
{'title': 'Report a Possible Sighting'})
34
35. Rendering a Page
from google.appengine.ext.webapp import template
html = 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
37. Django Forms to the Rescue!
from google.appengine.ext.db import djangoforms
import dwwgDB
class SightingForm(djangoforms.ModelForm):
class Meta:
model = dwwgDB.Sighting
37
38. Rendering a Page, Again
from google.appengine.ext.webapp import template
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': ''})
38
40. You need to start your webapp
from google.appengine.ext import webapp
from 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
42. Put this code in dwwgapp.py
from google.appengine.ext import webapp
from 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
44. Responding to a GET Request
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)
44
53. You need to POST data!
POST data with put()
53
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)
54
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):
55
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()
56
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')
57
58. 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
59. And don't 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
68. ...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