SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
Hands-On with
Google App Engine
workshop
         Ikai Lan
    Developer Relations
     Google App Engine
      ikai.l@google.com
        OSCON 2010
       bit.ly/gcodelabs
Your Workshop Instructor (drill sgt)
Engineer
   Lots of experience building and scaling web
   applications (Java/Ruby on MySQL)
   Front-end - JavaScript, OpenSocial
   Focused on teaching and supporting Google
   App Engine
Contact
   Twitter: @ikai
   Email: ikai.l@google.com
Google App Engine codelab
Objective
   Hands-on experience developing* an App Engine
   app
Requirements
   Computer, Python 2.5.x & App Engine SDK
     Text editor and command shell or IDE
   Optional: valid Google account, SMS on cell
Session
   Single app, seven fast iterations. Crash? Go to
   teammate!
   Copy, edit, run (lather, rinse, repeat)
   Look for diffs/changes in pink
Check your Python install
Linux/Mac
 $ python
In Windows:
   Go to Start->Programs->Python
  Make sure you are running Python 2.5!
Download!
SDK
   http://code.google.com/appengine/
Codelab
   http://bit.ly/gcodelabs
Why App Engine?
Easy to start
   Download the SDK and begin writing
   code!
Easy to scale
   Push your code onto Google servers,
   capacity added as needed
Tips before we begin
Be mindful of whitespace
Follow along at http://bit.ly.gcodelabs
Ask questions
Create your directory
YOUR_APP_DIRECTORY/
  main.py
  app.yaml
  index.yaml
Set up your config (app.yaml)
application: YOUR_APP_ID
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: main.py
Your first App Engine App (main.
py)
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class MainHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Hello world!')

def main():
  application = webapp.WSGIApplication([('/',
     MainHandler)], debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
   main()
Run your app!
From the launcher click "Run". OR

Command line:
$ dev_appserver.py YOUR_APP_DIR

Browse to: http://localhost:8080
Alternatively: http://127.0.0.1:8080
Putting code in a function (main1.py)
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
   self.response.out.write('Hello World!')

application = webapp.WSGIApplication([
   ('/', MainPage),
], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
   main()
Add HTML (main2.py)
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write('<h1>Hello World!</h1>')

:
Add HTML form (make3.py)
class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<h1>Hello world!</h1>')
    self.response.out.write('''
         <form action="/sign" method=post>
         <input type=text name=content>
         <input type=submit value="Sign Guestbook">
       </form>
    ''')
Add signing handler (main4.py)
from google.appengine.ext import webapp
:
class MainPage(webapp.RequestHandler):
:

class GuestBook(webapp.RequestHandler):
  def post(self):
    self.response.out.write(
       '<h3>You wrote:</h3>%s' % self.request.get('content'))

application = webapp.WSGIApplication([
   ('/', MainPage),
   ('/sign', GuestBook),
], debug=True)
:
  Storing data (main5.py)
from google.appengine.ext import db, webapp
:
class Greeting(db.Model):
   content = db.StringProperty(multiline=True)
   date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<h1>My Guestbook</h1><ol>')
    greetings = Greeting.all()
    for greeting in greetings:
       self.response.out.write('<li> %s </li>' % greeting.content)
       self.response.out.write('''</ol><hr>
          <form action="/sign" method=post>
          <textarea name=content rows=3 cols=60></textarea>
         <input type=submit value="Sign Guestbook">
         </form>''')
class GuestBook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()
    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/')
View your data in admin console
Browse to:
http://localhost:8080/_ah/admin

Alternatively:
http://127.0.0.1:8080/_ah/admin
Adding users (& authors) (main6.py)
:
from google.appengine.api import users
:
class Greeting(db.Model):
   author = db.UserProperty()
   content = db.StringProperty(multiline=True)
   date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if user:
       self.response.out.write('Hello %s!' % user.nickname())
    else:
       self.redirect(users.create_login_url(self.request.uri))
       self.response.out.write('<h1>My Guestbook</h1><ol>')
:
class GuestBook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()
    user = users.get_current_user()
    if user:
       greeting.author = user
       greeting.content = self.request.get('content')
Adding an HTML template (main7.py)
:
from os import path
:
from google.appengine.ext.webapp.template import render
:

class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    greetings = Greeting.all()
    context = {
       'user': user,
       'greetings': greetings,
       'login': users.create_login_url(self.request.uri),
       'logout': users.create_logout_url(self.request.uri),
    }
    tmpl = path.join(path.dirname(__file__), 'index.html')
    self.response.out.write(render(tmpl, context))
Adding an HTML template (index.html)
 <html><body>Hello
 {% if user %}
 {{ user.nickname }}!
 [<a href="{{ logout }}"><b>sign out</b></a>]
 {% else %}
 World!
 [<a href="{{ login }}"><b>sign in</b></a>]
 {% endif %}

 <h1>My Guestbook</h1><ol>
 {% for greeting in greetings %}
 <li>
 {% if greeting.author %}
 {{ greeting.author.nickname }}
 {% else %}
 <i>anonymous</i>
 {% endif %}
 {{ greeting.content|escape }}
 {% endfor %}
 </ol><hr>
 <form action="/sign" method=post>
 <textarea name=content rows=3 cols=60></textarea>
 <input type=submit value="Sign Guestbook">
 </form></body></html>
Now what?
  Keep going! We'll be here to help
  http://bit.ly/gcodelabs
  Push your application live
  http://appspot.com
  Read more documentation
  http://code.google.com/appengine

Contenu connexe

Tendances

Flask patterns
Flask patternsFlask patterns
Flask patterns
it-people
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
Alessandro Molina
 

Tendances (20)

Filling the flask
Filling the flaskFilling the flask
Filling the flask
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 
Flask patterns
Flask patternsFlask patterns
Flask patterns
 
Django Heresies
Django HeresiesDjango Heresies
Django Heresies
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
WebGUI Developers Workshop
WebGUI Developers WorkshopWebGUI Developers Workshop
WebGUI Developers Workshop
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORM
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
 
LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in details
 
Why Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebWhy Task Queues - ComoRichWeb
Why Task Queues - ComoRichWeb
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3
 
Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
 
Kyiv.py #17 Flask talk
Kyiv.py #17 Flask talkKyiv.py #17 Flask talk
Kyiv.py #17 Flask talk
 
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
 
CodeIgniter 3.0
CodeIgniter 3.0CodeIgniter 3.0
CodeIgniter 3.0
 

Similaire à OSCON Google App Engine Codelab - July 2010

Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
fool2nd
 
Hi5 Hackathon Presentation
Hi5 Hackathon PresentationHi5 Hackathon Presentation
Hi5 Hackathon Presentation
Lou Moore
 
Cloud Endpoints _Polymer_ Material design by Martin Görner
Cloud Endpoints_Polymer_Material design by Martin GörnerCloud Endpoints_Polymer_Material design by Martin Görner
Cloud Endpoints _Polymer_ Material design by Martin Görner
European Innovation Academy
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
fiyuer
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
Anthony Montalbano
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
Python Ireland
 

Similaire à OSCON Google App Engine Codelab - July 2010 (20)

Gae
GaeGae
Gae
 
Gae
GaeGae
Gae
 
Introduccion app engine con python
Introduccion app engine con pythonIntroduccion app engine con python
Introduccion app engine con python
 
Gae Meets Django
Gae Meets DjangoGae Meets Django
Gae Meets Django
 
Hi5 Hackathon Presentation
Hi5 Hackathon PresentationHi5 Hackathon Presentation
Hi5 Hackathon Presentation
 
Introduction to Google App Engine
Introduction to Google App EngineIntroduction to Google App Engine
Introduction to Google App Engine
 
Cloud Endpoints _Polymer_ Material design by Martin Görner
Cloud Endpoints_Polymer_Material design by Martin GörnerCloud Endpoints_Polymer_Material design by Martin Görner
Cloud Endpoints _Polymer_ Material design by Martin Görner
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
 
Google app-engine-with-python
Google app-engine-with-pythonGoogle app-engine-with-python
Google app-engine-with-python
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Google Apps Script for Beginners- Amazing Things with Code
Google Apps Script for Beginners- Amazing Things with CodeGoogle Apps Script for Beginners- Amazing Things with Code
Google Apps Script for Beginners- Amazing Things with Code
 
The Google App Engine Oil Framework
The Google App Engine Oil FrameworkThe Google App Engine Oil Framework
The Google App Engine Oil Framework
 
Android Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdfAndroid Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdf
 
You've done the Django Tutorial, what next?
You've done the Django Tutorial, what next?You've done the Django Tutorial, what next?
You've done the Django Tutorial, what next?
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
 
End-to-end testing with geb
End-to-end testing with gebEnd-to-end testing with geb
End-to-end testing with geb
 

Plus de ikailan

2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore
ikailan
 
What is App Engine? O
What is App Engine? OWhat is App Engine? O
What is App Engine? O
ikailan
 
Building TweetEngine
Building TweetEngineBuilding TweetEngine
Building TweetEngine
ikailan
 

Plus de ikailan (14)

Your language doesn't scale
Your language doesn't scaleYour language doesn't scale
Your language doesn't scale
 
From 0-1 billion in 46 days
From 0-1 billion in 46 daysFrom 0-1 billion in 46 days
From 0-1 billion in 46 days
 
2011 august-gdd-mexico-city-rest-json-oauth
2011 august-gdd-mexico-city-rest-json-oauth2011 august-gdd-mexico-city-rest-json-oauth
2011 august-gdd-mexico-city-rest-json-oauth
 
2011 aug-gdd-mexico-city-high-replication-datastore
2011 aug-gdd-mexico-city-high-replication-datastore2011 aug-gdd-mexico-city-high-replication-datastore
2011 aug-gdd-mexico-city-high-replication-datastore
 
2011 july-nyc-gtug-go
2011 july-nyc-gtug-go2011 july-nyc-gtug-go
2011 july-nyc-gtug-go
 
2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore2011 july-gtug-high-replication-datastore
2011 july-gtug-high-replication-datastore
 
Intro to App Engine - Agency Dev Day NYC 2011
Intro to App Engine - Agency Dev Day NYC 2011Intro to App Engine - Agency Dev Day NYC 2011
Intro to App Engine - Agency Dev Day NYC 2011
 
2011 june-kuala-lumpur-gtug-hackathon
2011 june-kuala-lumpur-gtug-hackathon2011 june-kuala-lumpur-gtug-hackathon
2011 june-kuala-lumpur-gtug-hackathon
 
2011 June - Singapore GTUG presentation. App Engine program update + intro to Go
2011 June - Singapore GTUG presentation. App Engine program update + intro to Go2011 June - Singapore GTUG presentation. App Engine program update + intro to Go
2011 June - Singapore GTUG presentation. App Engine program update + intro to Go
 
Rapid web development using tornado web and mongodb
Rapid web development using tornado web and mongodbRapid web development using tornado web and mongodb
Rapid web development using tornado web and mongodb
 
Introducing the App Engine datastore
Introducing the App Engine datastoreIntroducing the App Engine datastore
Introducing the App Engine datastore
 
What is App Engine? O
What is App Engine? OWhat is App Engine? O
What is App Engine? O
 
Boot camp 2010_app_engine_101
Boot camp 2010_app_engine_101Boot camp 2010_app_engine_101
Boot camp 2010_app_engine_101
 
Building TweetEngine
Building TweetEngineBuilding TweetEngine
Building TweetEngine
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

OSCON Google App Engine Codelab - July 2010

  • 1. Hands-On with Google App Engine workshop Ikai Lan Developer Relations Google App Engine ikai.l@google.com OSCON 2010 bit.ly/gcodelabs
  • 2. Your Workshop Instructor (drill sgt) Engineer Lots of experience building and scaling web applications (Java/Ruby on MySQL) Front-end - JavaScript, OpenSocial Focused on teaching and supporting Google App Engine Contact Twitter: @ikai Email: ikai.l@google.com
  • 3. Google App Engine codelab Objective Hands-on experience developing* an App Engine app Requirements Computer, Python 2.5.x & App Engine SDK Text editor and command shell or IDE Optional: valid Google account, SMS on cell Session Single app, seven fast iterations. Crash? Go to teammate! Copy, edit, run (lather, rinse, repeat) Look for diffs/changes in pink
  • 4. Check your Python install Linux/Mac $ python In Windows: Go to Start->Programs->Python Make sure you are running Python 2.5!
  • 5. Download! SDK http://code.google.com/appengine/ Codelab http://bit.ly/gcodelabs
  • 6. Why App Engine? Easy to start Download the SDK and begin writing code! Easy to scale Push your code onto Google servers, capacity added as needed
  • 7. Tips before we begin Be mindful of whitespace Follow along at http://bit.ly.gcodelabs Ask questions
  • 8. Create your directory YOUR_APP_DIRECTORY/ main.py app.yaml index.yaml
  • 9. Set up your config (app.yaml) application: YOUR_APP_ID version: 1 runtime: python api_version: 1 handlers: - url: /.* script: main.py
  • 10. Your first App Engine App (main. py) from google.appengine.ext import webapp from google.appengine.ext.webapp import util class MainHandler(webapp.RequestHandler): def get(self): self.response.out.write('Hello world!') def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main()
  • 11. Run your app! From the launcher click "Run". OR Command line: $ dev_appserver.py YOUR_APP_DIR Browse to: http://localhost:8080 Alternatively: http://127.0.0.1:8080
  • 12. Putting code in a function (main1.py) from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self): self.response.out.write('Hello World!') application = webapp.WSGIApplication([ ('/', MainPage), ], debug=True) def main(): run_wsgi_app(application) if __name__ == '__main__': main()
  • 13. Add HTML (main2.py) from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/html' self.response.out.write('<h1>Hello World!</h1>') :
  • 14. Add HTML form (make3.py) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write('<h1>Hello world!</h1>') self.response.out.write(''' <form action="/sign" method=post> <input type=text name=content> <input type=submit value="Sign Guestbook"> </form> ''')
  • 15. Add signing handler (main4.py) from google.appengine.ext import webapp : class MainPage(webapp.RequestHandler): : class GuestBook(webapp.RequestHandler): def post(self): self.response.out.write( '<h3>You wrote:</h3>%s' % self.request.get('content')) application = webapp.WSGIApplication([ ('/', MainPage), ('/sign', GuestBook), ], debug=True)
  • 16. : Storing data (main5.py) from google.appengine.ext import db, webapp : class Greeting(db.Model): content = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write('<h1>My Guestbook</h1><ol>') greetings = Greeting.all() for greeting in greetings: self.response.out.write('<li> %s </li>' % greeting.content) self.response.out.write('''</ol><hr> <form action="/sign" method=post> <textarea name=content rows=3 cols=60></textarea> <input type=submit value="Sign Guestbook"> </form>''') class GuestBook(webapp.RequestHandler): def post(self): greeting = Greeting() greeting.content = self.request.get('content') greeting.put() self.redirect('/')
  • 17. View your data in admin console Browse to: http://localhost:8080/_ah/admin Alternatively: http://127.0.0.1:8080/_ah/admin
  • 18. Adding users (& authors) (main6.py) : from google.appengine.api import users : class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): user = users.get_current_user() if user: self.response.out.write('Hello %s!' % user.nickname()) else: self.redirect(users.create_login_url(self.request.uri)) self.response.out.write('<h1>My Guestbook</h1><ol>') : class GuestBook(webapp.RequestHandler): def post(self): greeting = Greeting() user = users.get_current_user() if user: greeting.author = user greeting.content = self.request.get('content')
  • 19. Adding an HTML template (main7.py) : from os import path : from google.appengine.ext.webapp.template import render : class MainPage(webapp.RequestHandler): def get(self): user = users.get_current_user() greetings = Greeting.all() context = { 'user': user, 'greetings': greetings, 'login': users.create_login_url(self.request.uri), 'logout': users.create_logout_url(self.request.uri), } tmpl = path.join(path.dirname(__file__), 'index.html') self.response.out.write(render(tmpl, context))
  • 20. Adding an HTML template (index.html) <html><body>Hello {% if user %} {{ user.nickname }}! [<a href="{{ logout }}"><b>sign out</b></a>] {% else %} World! [<a href="{{ login }}"><b>sign in</b></a>] {% endif %} <h1>My Guestbook</h1><ol> {% for greeting in greetings %} <li> {% if greeting.author %} {{ greeting.author.nickname }} {% else %} <i>anonymous</i> {% endif %} {{ greeting.content|escape }} {% endfor %} </ol><hr> <form action="/sign" method=post> <textarea name=content rows=3 cols=60></textarea> <input type=submit value="Sign Guestbook"> </form></body></html>
  • 21. Now what? Keep going! We'll be here to help http://bit.ly/gcodelabs Push your application live http://appspot.com Read more documentation http://code.google.com/appengine