building scalable web apps with python and google cloud...
TRANSCRIPT
![Page 1: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/1.jpg)
Building Scalable Web Apps with Python and Google Cloud Platform
Dan Sanderson, April 2015
![Page 2: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/2.jpg)
![Page 3: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/3.jpg)
June 2015pre-order now
![Page 4: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/4.jpg)
Agenda• Introducing GCP & GAE
• Starting a project with gcloud and Cloud Console
• Understanding request handlers and instances
• Using Python libraries and frameworks
• Using Flask
• Testing
• Deploying, monitoring, debugging
![Page 5: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/5.jpg)
![Page 6: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/6.jpg)
Google Cloud Platform
• Computation
• Networking
• Data storage
• Data analysis
• Deployment, monitoring, source control, access control, services…
• Scaling infrastructure
![Page 7: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/7.jpg)
Google Cloud Platform
• Web applications and services
• Gaming and mobile backends
• Scientific computing
• Video and graphics rendering
![Page 8: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/8.jpg)
Google Cloud PlatformComputation and networking
Google Compute Engine (GCE)
Google Container
Engine (GKE)
Google App
Engine (GAE)
![Page 9: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/9.jpg)
Google Cloud PlatformComputation and networking
Managed VMs (beta)
+
![Page 10: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/10.jpg)
Google Cloud PlatformData storage and analysis
Google Cloud
Datastore
Google Cloud SQL
Google Cloud
Storage
![Page 11: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/11.jpg)
![Page 12: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/12.jpg)
Google App Engine• Scalable serving infrastructure optimized for
web apps
• High level of abstraction
• Streamlined, sandboxed, language-specific
• Scalable services
• (Some services specific to App Engine)
![Page 13: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/13.jpg)
Google App Engine
• Easy to deploy, easy to manage
• Pay only for what you use
• Free tier, daily allocation
• Preview launched in 2008 as Python-only; Java, Go, PHP added later
![Page 14: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/14.jpg)
Setting up a development environment
• Cloud SDK: http://cloud.google.com/sdk/
• The gcloud command
• Python 2.7
• git
• pip, virtualenv
![Page 15: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/15.jpg)
gcloud
• Authentication
• Project set-up
• Component installation and management
• Service-specific tools
![Page 16: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/16.jpg)
gcloud
• App Engine SDK, Python libraries
• App Engine development server
• Development server console
• App Engine deployment
![Page 17: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/17.jpg)
Cloud Console
https://console.developers.google.com/
![Page 18: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/18.jpg)
Demo
![Page 19: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/19.jpg)
gcloud• gcloud auth login
• gcloud init <project>
• creates .gcloud file, clones git repo
• gcloud app run app.yaml
• gcloud app deploy app.yaml(gcloud preview app …)
![Page 20: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/20.jpg)
Instances and Request Handlers
![Page 21: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/21.jpg)
App handlers
Static file handlers
FEcache
AE FE
Datastore Memcache
Task Queue
URL Fetch
Mail XMPP
Blobstore Channel
Services
⋮
![Page 22: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/22.jpg)
Instances andRequest Handlers
• Request handlers are ephemeral: now you see them, now you don’t
• Can’t rely on data persistence between requests
• Use storage services to persist data
![Page 23: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/23.jpg)
Request handler
Request handler
Request handler
Request handler
⋯ ⋯
![Page 24: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/24.jpg)
Instances andRequest Handlers
• In practice, app initialization is expensive
• An app instance is long running, can handle multiple requests in its lifetime
• Environment initialized; instance memory loaded
• Started and stopped as needed
• Can’t rely on a single user’s session to go to the same instance
![Page 25: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/25.jpg)
Instance
Request handler
⋯
![Page 26: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/26.jpg)
Instance
Request handler
⋯
![Page 27: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/27.jpg)
Instance InstanceInstance
Request handler
Request handler
Request handler
⋯
![Page 28: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/28.jpg)
Instance InstanceInstance
Request handler
Request handler
Request handler
⋯Request handler
Request handler
Request handler
threading enabled
![Page 29: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/29.jpg)
Instance InstanceInstance
Request handler
Request handler
Request handler
⋯Request handler
Request handler
Request handler
threading enabled
![Page 30: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/30.jpg)
Instance InstanceInstance
Request handler
Request handler
⋯Request handler
Request handler
threading enabled
![Page 31: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/31.jpg)
The Python Runtime Environment
![Page 32: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/32.jpg)
Runtime Environment• Sandboxing
• Data isolation
• Performance isolation
• Python 2.7: app code must be Python only
• Sandboxing → scalability
![Page 33: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/33.jpg)
Runtime Environment• Limits
• Request timer
• Restricted access to filesystem, sockets
• More performance isolation: RAM, CPU
• Data sizes: requests, responses, API calls, storage objects
• Limits → scalability
![Page 34: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/34.jpg)
Python Libraries
• Runtime includes standard lib and GAE libs automatically
• App code must be “pure Python”
• Can add “pure Python” libs to your app code
• App config can request specific libs be added
![Page 35: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/35.jpg)
Python Libraries
standard libraries GAE libraries
application code
![Page 36: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/36.jpg)
Python Libraries• Python 2.7 standard library
• GAE libraries (google.appengine…)
• Runtime / dev server has GAE libs on path already
• For other scripts, like test runners:
import syssys.path.insert(0, ‘…/google-cloud-sdk/platform/ google_appengine/‘)
![Page 37: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/37.jpg)
Python Libraries
standard libraries GAE libraries
application code
![Page 38: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/38.jpg)
Python Libraries
standard libraries GAE libraries
application code
./lib/ …
![Page 39: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/39.jpg)
Python Libraries% mkdir ./lib % pip install -t lib Flask==0.10
![Page 40: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/40.jpg)
Python Libraries% mkdir ./lib % pip install -t lib Flask==0.10
# appengine_config.py from google.appengine.ext import vendor vendor.add(‘lib’)
![Page 41: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/41.jpg)
Python Libraries% mkdir ./lib % pip install -t lib Flask==0.10
# appengine_config.py from google.appengine.ext import vendor vendor.add(‘lib’)
# .gitignore lib/*
![Page 42: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/42.jpg)
Python Libraries
standard libraries GAE libraries
application code
./lib/ …
![Page 43: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/43.jpg)
Python Libraries
standard libraries GAE libraries
application code
./lib/ …
app.yaml: “libraries: …”
requested libraries
![Page 44: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/44.jpg)
Python Librariesdjango endpoints jinja2 lxml markupsafe matplotlib MySQLdb numpy PIL
protorpc PyAMF pycrypto setuptools ssl webapp2 webob yaml
![Page 45: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/45.jpg)
Python Libraries# app.yaml
libraries: - name: jinja2 version: “2.6” - name: markupsafe version: “0.15”
![Page 46: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/46.jpg)
Python Libraries
standard libraries GAE libraries
application code
./lib/ …
app.yaml: “libraries: …”
requested libraries
![Page 47: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/47.jpg)
Python LibrariesUse virtualenv
to contain requested librariesin your development environment.
% virtualenv venv % source venv/bin/activate (venv)% pip install jinja2==2.6
![Page 48: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/48.jpg)
Python LibrariesUse pip requirements files
to install and document dependencies.
# requirements_cfg.txt jinja2==2.6 markupsafe==0.15
(venv)% pip install -r requirements_cfg.txt
![Page 49: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/49.jpg)
Python LibrariesUse pip requirements files
to install and document dependencies.
# requirements_vnd.txt Flask==0.10 google-api-python-client
(venv)% pip install -t lib \ —r requirements_vnd.txt
![Page 50: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/50.jpg)
Using Flask
![Page 51: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/51.jpg)
Using Flask• Install Flask in ./lib
• Request jinja2 and markup safe in app.yaml; install in virtualenv
• lib/markupsafe’s C-based speed-up is not usable but app.yaml will override with built-in
• app = Flask()
• No need to call app.run(); GAE dev server will take care of it
![Page 52: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/52.jpg)
Cloud Datastore and ndbfrom google.appengine.ext import ndb
class Entry(ndb.Model): title = ndb.StringProperty(required=True) text = ndb.TextProperty(required=True) last_updated_date = ndb.DateTimeProperty( auto_now=True)
![Page 53: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/53.jpg)
Cloud Datastore and ndbentry = Entry( title=flask.request.form[‘title’], text=flask.request.form[‘text’]) entry.put()
entries = Entry.query().order( -Entry.last_updated_date) for entry in entries: # ...
![Page 54: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/54.jpg)
Google Accountsfrom google.appengine.api import users
user = users.get_current_user() if user: # user.nickname...
login_url = users.create_login_url(‘/‘) logout_url = users.create_logout_url(‘/‘)
![Page 55: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/55.jpg)
Google Accounts# app.yaml
handlers: - url: /add script: main.app login: required
- url: .* script: main.app
![Page 56: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/56.jpg)
Demo
![Page 57: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/57.jpg)
Testing
![Page 58: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/58.jpg)
Testing• It’s just Python, right?
• Activate your virtualenv
• Make sure google_appengine, the app root, and ./lib are in PYTHONPATH
• (also yaml)
• Use your favorite test framework (e.g. unittest)
![Page 59: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/59.jpg)
testbed• Service API stubs for a clean test environment,
including Cloud Datastore
• Provided by the GAE SDK
• google.appengine.ext.testbed
• Datastore: can fuzz test consistency guarantees
• Users: can set signed-in status, address, is-admin status
![Page 60: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/60.jpg)
testbedimport unittest from google.appengine.ext import testbed
import main import models
class MyBlogTestCase(unittest.TestCase): def setUp(self): # ... def tearDown(self): # ...
![Page 61: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/61.jpg)
testbedclass MyBlogTestCase(unittest.TestCase): def setUp(self): self.tb = testbed.Testbed() self.tb.activate() self.tb.init_datastore_v3_stub() self.tb.init_memcache_stub() self.tb.init_user_stub()
main.app.config['TESTING'] = True self.app = main.app.test_client()
![Page 62: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/62.jpg)
testbedclass MyBlogTestCase(unittest.TestCase): def tearDown(self): self.tb.deactivate()
![Page 63: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/63.jpg)
testbedclass TestEntry(MyBlogTestCase): def test_entry(self): m = models.Entry( title='Test', text='Test entry text') m.put() self.assertIsNotNone(m.last_updated_date)
![Page 64: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/64.jpg)
testbedclass MyBlogInteractions(MyBlogTestCase): def test_no_entries(self): rv = self.app.get('/') assert 'No entries here so far' in rv.data
def test_one_entry(self): e = models.Entry(title='Test', text='Test text') e.put()
rv = self.app.get('/') assert 'No entries here so far' not in rv.data
![Page 65: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/65.jpg)
testbedtb.init_all_stubs() tb.init_blobstore_stub() tb.init_datastore_v3_stub() tb.init_memcache_stub() tb.init_images_stub() tb.init_mail_stub() tb.init_taskqueue_stub() tb.init_urlfetch_stub() tb.init_user_stub() tb.init_xmpp_stub()
cloud.google.com/appengine/docs/python/tools/ localunittesting
![Page 66: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/66.jpg)
Flask Testing
flask.pocoo.org/docs/0.10/testing/
![Page 67: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/67.jpg)
Demo
![Page 68: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/68.jpg)
Deploying, Monitoring, Debugging
![Page 69: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/69.jpg)
Demo
![Page 70: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/70.jpg)
Where to go from here…• Modules
• Task Queues
• Advanced Cloud Datastore: ndb, transactions, memcache
• Cloud SQL
• Cloud Storage
• Compute / Container Engine
• Managed VMs (beta)
![Page 71: Building Scalable Web Apps with Python and Google Cloud Platformae-book.appspot.com/static/gcp-preso-20150421.pdf · Building Scalable Web Apps with Python and Google Cloud Platform](https://reader031.vdocuments.us/reader031/viewer/2022020413/5b8c2d2809d3f24a638c8572/html5/thumbnails/71.jpg)
Thank you!
cloud.google.com
ae-book.appspot.com
Programming Google App Engine with Python, … with Java Early Access / pre-order now Fully available June 2015
Dan Sanderson profiles.google.com/ dan.sanderson