Slide 1

Slide 1 text

Introduc)on  to  Google   App  Engine  with  Python   Brian  Ly:le  (h:p://brianly:le.com)   @brianly  on  Twi:er  

Slide 2

Slide 2 text

What  is  Google  App  Engine?   Ê  Google  call  it  “cloud  development  in  a  box”.   Ê  It  is  a  hosted  development  environment  along  the  lines  of  MicrosoM   Azure.   Ê  Targets  users  building  internet  applica)ons,  but  that  is  changing.   Ê  Built  on  the  same  scalable  plaPorm  used  for  other  Google  services.   Ê  Development  is  done  locally  in  Python  (or  Java)  on  Windows,  Mac   OS  X,  or  Linux.   Ê  Push  to  the  cloud  and  Google  will  manage  everything.   Ê  We’ll  dig  into  the  details  shortly.  

Slide 3

Slide 3 text

Who’s  using  App  Engine?   Ê  Case  Studies   Ê  h:p://code.google.com/appengine/casestudies.html   Ê  Most  oMen  GAE  is  used  as  a  u)lity  alongside  another  plaPorm.   Ê  GAE  for  Business  will  increase  uptake.  Eventually.  

Slide 4

Slide 4 text

How  much  does  it  cost?   Ê  It  is  free  to  get  started,  or  run  small  applica)ons.   Ê  ~5M  page  views  per  month   Ê  Google  post  quotas  and  costs  on  their  website   Ê  h:p://code.google.com/appengine/docs/quotas.html     Ê  Once  you  reach  certain  limits  Google  will  start  charging  you  based  on  your  usage.   Ê  Usage  costs  can  be  expensive  depending  on  your  requirements,  or  alterna)ve  deployment   plaPorms.   Ê  Choice  of  datastore  can  even  impact  costs   Ê  Calcula)ng  costs  can  be  tricky,  but  this  is  the  case  with  all  usage-­‐based  cloud  providers  and   services.   Ê  You  need  to  know  your  applica)on,  and  the  how  the  plaPorm  can  be  best  used  applied  to   your  needs.   Ê  Cheaper  features  might  have  poten)al  for  higher  lock-­‐in.  

Slide 5

Slide 5 text

Mapping  use  cases  to  App  Engine   Ê  You  can’t  really  choose  a  framework  or  tool  without  some  experience.  Take  this  advice  with  a   pinch  of  salt.   Ê  Good   Ê  You  want  to  learn  a  Python  web  development  framework.   Ê  Managing  servers  is  not  something  you  want  to  do.   Ê  Your  applica)on  needs  to  support  a  ridiculous  number  of  users,  or  giant  fluctua)ons  in  usage.   Ê  You  want  to  quickly  prototype  a  larger  applica)on  that  you  may  develop  elsewhere.   Ê  Maybe   Ê  An  exis)ng  applica)on  is  wri:en  in  Python  (or  Java)  and  you  want  to  create  a  web  front  end.   Ê  Bad   Ê  Data  sources  for  your  applica)on  live  inside  your  company.   Ê  Other  data  security  or  privacy  requirements.   Ê  You  need  a  version  of  Python  newer  than  2.5.   Ê  Your  Python  library  requires  a  C  extension  and  does  not  have  a  Python  fallback.  

Slide 6

Slide 6 text

Learning  Python   Ê  Zed  Shaw’s  “Learn  Python  the  Hard  Way”  is  recommended.   Ê  h:p://learnpythonthehardway.org/index       Ê  Many  other  resources  are  available:   Ê  h:p://diveintopython.org/toc/index.html       Ê  h:p://docs.python.org/tutorial/       Ê  Focus  on  learning  Python  2.x  if  you  want  to  use  the  broadest  range   of  libraries  today.     Ê  Python  3.2  is  the  latest  but  newbies  will  have  issues  with  libraries.   Ê  GAE  makes  the  choice  easier.  Use  CPython  2.5.   Ê  Get  familiar  with  REPL-­‐style  development  in  the  shell.  

Slide 7

Slide 7 text

Development  tools   Ê  Python  2.5   Ê  Only  this  version  is  supported  at  present.   Ê  Free  from  h:p://www.python.org/Mp/python/     Ê  Get  the  latest  2.5.x  release  from  h:p://www.python.org/Mp/python/2.5.5/       Ê  Google  App  Engine  SDK   Ê  Download  the  latest  version  and  it  will  auto-­‐update.   Ê  Free  from  h:p://code.google.com/appengine/downloads.html     Ê  A  suitable  editor   Ê  Most  (good)  text  editors  provide  syntax  highligh)ng.   Ê  I  like  JetBrains’  PyCharm,  but  Ac)veState  Komodo  is  also  good.  A  lot  of   people  like  the  Wingware  IDE.   Ê  You  could  use  the  latest  Python  support  for  Visual  Studio  2010  if  you  want   but  it  does  not  have  special  support  for  App  Engine.  

Slide 8

Slide 8 text

Documenta)on  and  samples   Ê  All  docs  can  be  downloaded  as  a  ZIP   Ê  h:p://code.google.com/appengine/downloads.html  -­‐   Download_the_Google_App_Engine_Documenta)on     Ê  Documenta)on   Ê  h:p://code.google.com/appengine/docs/python/overview.html   Ê  Sample  Code   Ê  h:p://code.google.com/p/google-­‐app-­‐engine-­‐samples/   Ê  h:p://code.google.com/p/rietveld/   Ê  h:ps://github.com/search?langOverride=&language=python&q=google+app +engine&repo=&start_value=1&type=Repositories&x=0&y=0     Ê  h:p://brizzled.clapper.org/id/77/     Ê  Videos   Ê  Google  IO  Conference  on  YouTtube.   Ê  PyCon:  h:p://pycon.bliptv.com    

Slide 9

Slide 9 text

Services  provided  by  App  Engine   Datastore  (SQL  Server)   Blobstore  (File  system)   Capabili>es   Channel  (comet?)   Images  (ImageGlue.NET)   Mail  (BCL)   Memcache  (AppFabric)   Mul>tenancy  (SQL  Schemas)   OAuth  (BCL)   Prospec>ve  Search   Task  Queues  (MSMQ)   URL  Fetch  (BCL)   Users  (BCL)   XMPP  (Lync  API)   Ê  Services  are  accessed  via  “webapp”.   Ê  Most  have  equivalents  in  the  .NET  world   (shown  in  parentheses)   Ê  Sites  hosted  at  *.appspot.com   Ê  Limita)ons:   Ê  SSL  for  custom  domains   Ê  Quotas  impac)ng  cost  are  complex   Ê  Users  all  have  Gmail  emails  by  default   Ê  Python  libraries  which  depend  on  C   extensions  cannot  run  on  the  plaPorm.   Ê  Google-­‐like  search  requires  some  effort.  

Slide 10

Slide 10 text

App  Engine  Launcher  

Slide 11

Slide 11 text

SDK  Console  

Slide 12

Slide 12 text

Basic  App  Engine  demo   Ê  Bookmarkz   Ê  Bookmark  database   Ê  URL  shortener   Ê  Using  the  default  “webapp”  framework   Ê  Source  code  is  available  from   h:ps://bitbucket.org/brianly/bookmarkz/src    

Slide 13

Slide 13 text

Webapp  framework   Ê  Based  on  a  basic  framework  called  WebOb  that  runs  atop   WSGI.   Ê  WSGI  is  an  interface  to  Python  web  servers.   Ê  Whilst  you  have  basic  support  for  MVC,  you  don’t  have  the   support  provided  by  a  full  framework  like  ASP.NET  MVC.   Ê  Conven)on  or  configura)on?  Neither.   Ê  For  a  lot  of  applica)ons  you  should  be  considering  a   framework  like  Tipfy  (more  later).  

Slide 14

Slide 14 text

Example  handler   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/plain'                  self.response.out.write('Hello,  webapp  World!')     application  =  webapp.WSGIApplication(                                                                            [('/',  MainPage)],                                                                            debug=True)     def  main():          run_wsgi_app(application)     if  __name__  ==  "__main__":          main()  

Slide 15

Slide 15 text

Datastore   Ê  You  En))es  based  on  Classes  which  derive  from  a  special  base  class.   Ê  Google  provide  a  range  of  data  types  for  En)ty  proper)es.   Ê  En))es  are  stored  in  a  system  based  on  Big  Table  (Master/Slave)  or   Megastore  (High  Replica)on).   Ê  You  choose  this  when  you  create  your  applica)on.   Ê  Choice  of  storage  affects  costs  and  can’t  be  changed  easily.   Ê  Google  recommend  the  High  Replica)on  datastore  for  be:er  all   round  performance.   Ê  Fewer  peaks  and  troughs  compared  to  the  Master/Slave  store.  

Slide 16

Slide 16 text

Working  with  Models   Ê  Models  are  defined  in  Python  in  a  similar  fashion  to  other  ORM  tools.   Ê  The  challenges  are  a  li:le  different  from  your  experiences  using  ORMs  with   rela)onal  databases.   Ê  Data  types   Ê  h:p://code.google.com/appengine/docs/python/datastore/ typesandpropertyclasses.html     Query     query  =  GqlQuery("SELECT  *  FROM      Song  WHERE  composer      =  :composer",      composer="Lennon,  John")     query  =  db.Query(Song)   query.filter('composer=',  id)   result  =  query.get()   Defini>on     class  Song(db.Model):          author  =  db.UserProperty()          composer  =  db.StringProperty()          date  =  db.DateTimeProperty            (auto_now_add=True)          tags  =  db.StringListProperty()          description  =  db.TextProperty()  

Slide 17

Slide 17 text

More  Models   class  Person(db.Expando):          first_name  =  db.StringProperty()          last_name  =  db.StringProperty()          hobbies  =  db.StringListProperty()   class  Contact(polymodel.PolyModel):          phone_number  =  db.PhoneNumberProperty()          address  =  db.PostalAddressProperty()   class  Person(Contact):          first_name  =  db.StringProperty()          last_name  =  db.StringProperty()          mobile_number  =  db.PhoneNumberProperty()   class  Company(Contact):          name  =  db.StringProperty()          fax_number  =  db.PhoneNumberProperty()   Ê  Expando  and  PolyModel.   Ê  More  informa)on  can  be  found  in  the  SDK  documenta)on  

Slide 18

Slide 18 text

Blobstore   Ê  Limits  apply  to  object  size  in  the  datastore.     Ê  Store  large  objects  e.g.  images  or  documents  in  the  Blobstore   Ê  Excep)ons  might  be  small  thumbnails  where  they  are  always   returned  with  related  fields.   Ê  Use  the  BlobstoreUploadHandler  class  to  make  uploading  blob   objects  less  painful.   Ê  BlobstoreDownloadHandler  provides  a  way  to  specify  byte  ranges.   Ê  BlobReader  gives  you  a  stream-­‐like  API.   Ê  Plays  well  with  the  Images  API.  

Slide 19

Slide 19 text

URL  Fetch   Ê  Enables  requests  to  HTTP  and  HTTPS  resources.   Ê  Use  for  calls  to  REST  (and  SOAP)  web  services.   Ê  Supports  calls  to  your  intranet  through  the  Google  Secure  Data   Connector  (SDC).     from  google.appengine.api  import  urlfetch     result  =  urlfetch.fetch(url="http://www.corp.example.com/sales.csv",                                                  headers={'use_intranet':  'yes'})   if  result.status_code  ==  200:      parseCSV(result.content)   Ê  Download  size  is  limited  to  32  MB.  

Slide 20

Slide 20 text

Task  Queues   Ê  Allows  you  to  do  work  later  such  as  send  a  no)fica)on,  or   update  a  3rd  party  system.   #  Application  queues  a  task   taskqueue.add(url='/worker',  params={'key':  key})     #  Handler  does  the  work   class  CounterWorker(webapp.RequestHandler):          def  post(self):  #  should  run  at  most  1/s                  key  =  self.request.get('key')                  def  txn():                          counter  =  Counter.get_by_key_name(key)                          if  counter  is  None:                                  counter  =  Counter(key_name=key,  count=1)                          else:                                  counter.count  +=  1                          counter.put()                  db.run_in_transaction(txn)  

Slide 21

Slide 21 text

Notes  for  Windows  users   Ê  Python  works  pre:y  well  on  Windows  compared  to  other   languages.   Ê  Google’s  imaging  library  needs  an  extension  called  PIL.   Ê  64-­‐bit  versions  of  Python  and  the  Python  Imaging  Library  (PIL)   Ê  Make  sure  that  the  architectures  of  extensions  match  up.   Ê  Windows  programs  a  64-­‐bit  applica)on  cannot  load  a  32-­‐bit  DLL,   and  vice  versa.   Ê  See  my  blog  post  on  this  topic.     Ê  Windows  Firewall  and  blocked  ports   Ê  No  server  ==  no  fun  

Slide 22

Slide 22 text

App  Engine  Dashboard  

Slide 23

Slide 23 text

Health  and  up)me   Ê  Check  the  status  page  if   you  experience  issues.   Ê  Situa)ons   Ê  Read-­‐only  Datastore   Ê  “Regular”  failures   Ê  The  Capabili)es  API  is   provided  to  enable  you   to  handle  maintenance   periods.  

Slide 24

Slide 24 text

Be:er  frameworks  for  App  Engine   Ê  The  “webapp”  framework  is  bare  bones  and  based  on  WSGI.   Ê  Any  WSGI  framework  can  run  on  App  Engine.   Ê  Kay   Ê  Components:  Django,  Werkzeug,  Jinja2,  babel   Ê  h:p://code.google.com/p/kay-­‐framework/   Ê  Tipfy   Ê  Custom  framework  with  mul)ple  template  engines,  and  plugins.   Ê  h:p://www.)pfy.org/   Ê  Django-­‐nonrel   Ê  A  NoSQL-­‐op)mized  version  of  Django.   Ê  Wesley  Chun’s  from  Google  presented  on  this  at  PyCon.   Ê  h:p://www.allbu:onspressed.com/projects/django-­‐nonrel    

Slide 25

Slide 25 text

Run  your  own  App  Engine   Ê  “PlaPorm  as  a  Service”  can  lock  you  in   Ê  Data  lock-­‐in  is  probably  a  bigger  problem  than  frameworks.   Ê  Possible  solu)ons   Ê  Typhoon  AE:  h:p://code.google.com/p/typhoonae/   Ê  AppScale:  h:p://sites.google.com/site/gaeasaframework/appscale     Ê  Essen)ally  these  are  a  lot  of  pain  to  setup,  and  you  are  probably   using  the  App  Engine  framework  to  solve  the  wrong  problem.   Ê  A  be:er  solu)on  is  to  use  a  na)ve  Python  framework:   Ê  Django:  h:p://www.djangoproject.com/     Ê  Pyramid  (Pylons):  h:p://pylonsproject.org/     Ê  Flask:  h:p://flask.pocoo.org/    

Slide 26

Slide 26 text

Thanks!   Ê  Don’t  forget  to  provide  provide  feedback  on  my  talk  and   others  you  a:ended  today.   Ê  Slides  and  code  will  be  posted  to  h:p://brianly:le.com   shortly.