Rapid Development with Django and App Engine. Guido van Rossum May 28, 2008

Similar documents
Django: Views, Templates, and Sessions

MIT AITI Python Software Development Lab DJ1:

Tangent MicroServices Documentation

정재성

Webdev: Building Django Apps. Ryan Fox Andrew Glassman MKE Python

Reminders. Full Django products are due next Thursday! CS370, Günay (Emory) Spring / 6

Django Test Utils Documentation

Chapter 19: Twitter in Twenty Minutes

Tomasz Szumlak WFiIS AGH 23/10/2017, Kraków

Django IPRestrict Documentation

web frameworks design comparison draft - please help me improve it focus on Model-View-Controller frameworks

Quoting Wikipedia, software

Django Deployment & Tips daybreaker

webapp2 Documentation

Django Standalone Apps

Technology modeling. Ralf Lämmel Software Languages Team University of Koblenz-Landau

silk Documentation Release 0.3 Michael Ford

django-scaffold Documentation

Building a Django Twilio Programmable Chat Application

Developing with Google App Engine

Django starting guide

widgets, events, layout loosely similar to Swing test browser, or plugin for testing with real browser on local system

django-dajaxice Documentation

Hons. B.Sc. Degree in Software Engineering/Development. Web and Cloud Development

Django Phantom Theme Documentation

DJOAuth2 Documentation

Django-Style Flask. Cody Lee SCALE12x Feb 22, git clone

Building Scalable Web Apps with Python and Google Cloud Platform. Dan Sanderson, April 2015

Ninja Typers Web Application Design Document By Marvin Farrell C

django-audiofield Documentation

Django urls Django Girls Tutorial

Django MFA Documentation

Accelerating Information Technology Innovation

Graphene Documentation

django-openid Documentation

Accelerating Information Technology Innovation

generates scaffolding/framework for models, views

Diving into Big Data Workshop

The Django Web Framework Part II. Hamid Zarrabi-Zadeh Web Programming Fall 2013

Django-frontend-notification Documentation

Django File Picker Documentation

django-facetools Documentation

Django_template3d Documentation

PROGRAMMING GOOGLE APP ENGINE WITH PYTHON: BUILD AND RUN SCALABLE PYTHON APPS ON GOOGLE'S INFRASTRUCTURE BY DAN SANDERSON

Building Python web app on GAE

Google & the Cloud. GData, Mashup Editor, AppEngine. Gregor Hohpe Software Engineer Google, Inc. All rights reserved,

django-ad-code Documentation

Lightweight. Django USING REST, WEBSOCKETS & BACKBONE. Julia Elman & Mark Lavin

Django Part II SPARCS 11 undead. Greatly Inspired by SPARCS 10 hodduc


App Engine Web App Framework

App Engine Web App Framework

Investigating Source Code Reusability for Android and Blackberry Applications

fragapy Documentation

django-cron Documentation

django-messages Documentation

Lotus IT Hub. Module-1: Python Foundation (Mandatory)

CS2021- Week 10 Models and Views. Model, View, Controller. Web Development Model, Views, Controller Templates Databases

django-selenium Documentation

django-xross Documentation

DIGIT.B4 Big Data PoC

Asynchronous WebSockets using Django

Bambu API Documentation

Django PAM Documentation

CS4HS Using Google App Engine. Michael Parker

Google App Engine Using Templates

django simple pagination Documentation

Django CBTools Documentation

Django File Picker Documentation

Aldryn Installer Documentation

nacelle Documentation

Building Production Quality Apps on App Engine. Ken Ashcraft 5/29/2008

Developing ASP.NET MVC Web Applications (486)

Linguistic Architecture

MapEntity Documentation

django-facebook-graph Documentation

Lecture 10(-ish) Web [Application] Frameworks

Technology modeling. Ralf Lämmel Software Languages Team University of Koblenz-Landau

CS Programming Languages: Python

Scaling App Engine Applications. Justin Haugh, Guido van Rossum May 10, 2011

micawber Documentation

App Engine: Datastore Introduction

kaleo Documentation Release 1.5 Eldarion

django-teamwork Documentation

Real Life Web Development. Joseph Paul Cohen

SAURASHTRA UNIVERSITY

Without Django. applying django principles to non django projects

Getting Django Set Up Using a Functional Test

nucleon Documentation

29-27 May 2013 CERN WEB FRAMEWORKS. Adrian Mönnich

django-oauth2-provider Documentation

Creating a Custom TinyWebDB service

cmsplugin-blog Release post 0 Øyvind Saltvik

django-waffle Documentation

open-helpdesk Documentation

django-telegram-login Documentation

django-photologue Documentation

Json Parse Error - No Json Object Could Be Decoded Django Rest Framework

Google App Engine Data Store. Google is BIG. Advanced Stuff.

Installing and Running the Google App Engine On a Macintosh System

Transcription:

Rapid Development with Django and App Engine Guido van Rossum May 28, 2008

Talk Overview This is not a plug for Python, Django or Google App Engine Except implicitly :) Best practices for using Django with App Engine Project setup The "Google App Engine Django Helper" module Using App Engine's db.model instead of Django's Model class Writing unit tests Q & A Anything goes! 3

Google App Engine Recap Does one thing well: run web apps Simple configuration Request handlers Accelerators and shortcuts 4

App Engine Does One Thing Well App Engine handles HTTP requests, nothing else Think RPC: request in, processing, response out Works well for the web and AJAX; also for other services Resources are scaled automatically Requests may go to the same process, serially Requests may go to different processes, in parallel or serially Highly scalable datastore based on Bigtable Not SQL; no joins 5

Configuring an App Engine Application An application is a directory with everything underneath it Symbolic links are followed! Single file app.yaml in app root directory Defines application metadata Maps URL patterns (regular expressions) to request handlers Separates static files from program files Dev server (SDK) emulates deployment environment 6

Request Handlers URL patterns are mapped to request handlers by app.yaml Handler is invoked like a CGI script Environment variables give request parameters E.g. PATH_INFO, QUERY_STRING, HTTP_REFERER Write response to stdout Status (optional), headers, blank line, body 7

Request Handler Accelerators and Shortcuts CGI doesn't mean slow! Define a main() function Module will remain loaded, main() called for each requests Can use module globals for caching Must use "if name == ' main ': main()" boilerplate CGI doesn't mean clumsy! WSGI support layered on top of CGI: util.run_wsgi_app(app) 8

Django Project Setup for App Engine Django is a bit finicky about project structure Code in one or more 'app' (application) subdirectories Cannot have the same name as the project Must have a single settings.py file Found via DJANGO_SETTINGS_MODULE environment variable App Engine vs. Django No SQL; must use App Engine's own database Top-level directory appended at end of sys.path Django 0.96.1 preloaded, which is pretty old Slight differences between dev_appserver and real deployment 9

Boilerplate Files, Standard Project Lay-out Boilerplate files These hardly vary between projects app.yaml: direct all non-static requests to main.py main.py: initialize Django and send it all requests settings.py: change only a few settings from defaults Project lay-out static/*: static files; served directly by App Engine myapp/*.py: app-specific python code urls.py, views.py, models.py, tests.py, and more templates/*.html: templates (or myapp/templates/*.html) 10

Minimal app.yaml application: myapp version: 1 #.appspot.com runtime: python api_version: 1 handlers: - url: /static static_dir: static - url:.* script: main.py 11

Contents and Purpose of main.py Set DJANGO_SETTINGS_MODULE environment variable Must do before importing Django Arrange for your version of Django to be loaded Monkey-patch some parts of Django (optional) Arrange to log all tracebacks using the logging module Define main() to invoke Django to handle one request 12

The Most Minimal main.py import os from google.appengine.ext.webapp import util os.environ['django_settings_module'] = 'settings' from django.core.handlers import wsgi def main(): app = wsgi.wsgihandler() util.run_wsgi_app(app) if name == ' main ': main() 13

Bare Minimal settings.py import os DEBUG = os.environ['server_software'].startswith('dev') INSTALLED_APPS = ( 'myapp', ) MIDDLEWARE_CLASSES = () ROOT_URLCONF = 'myapp.urls' TEMPLATE_CONTEXT_PROCESSORS = () TEMPLATE_DEBUG = DEBUG TEMPLATE_DIRS = ( os.path.join(os.path.dirname( file ), 'templates'), ) TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source', ) TZ = 'UTC' 14

Google App Engine Django Helper Separate open source project By two Googlers, Matt Brown and Andy Smith http://code.google.com/p/google-app-engine-django/ Takes care of monkey-patching Django Loads newer version of Django if available in app root dir Enables using standard Django project management tool For unit tests and fixture loading (development only) Comes with boilerplate app.yaml, main.py, settings.py 15

Getting Started Download and unzip appengine_helper_for_django_rn.zip Rename directory to 'mysite' or whatever you like This becomes your project root; contents: COPYING, KNOWN_ISSUES, Makefile, README init.py (empty) app.yaml (edit application: only) appengine_django/... (helper code lives here) main.py (generic bootstrap) manage.py (Django management script) settings.py (edit settings here) urls.py (edit URL mappings here) 16

Changing the Standard Set-up Edit app.yaml to set your application id (can do this later) Create subdirectory myapp Edit settings.py, adding 'myapp' to INSTALLED_APPS In myapp, create: init.py (empty) views.py (add your view code here) models.py (add your model definitions here) tests.py (add your unit tests here) To use Django HEAD, copy the django package here /usr/local/google_appengine must be your App Engine SDK 17

Your First View Edit urls.py: from django.conf.urls.defaults import * urlpatterns = patterns('myapp.views', (r'^$', 'index'), #...more here later... ) Edit myapp/views.py: from django.http import HttpResponse def index(request): return HttpResponse('Hello world') Run it:./manage.py runserver Point your browser to: http://localhost:8080 18

Using Models Edit models.py: from google.appengine.ext import db from appengine_django.models import BaseModel class Shout(BaseModel): # subclass of db.model title = db.stringproperty(required=true) text = db.textproperty() mtime = db.datetimeproperty(auto_now_add=true) user = db.userproperty() Up next: Write views to use models Use forms derived from your models, with templates 19

Using Forms I tend to put forms in views.py; do as you please from django.shortcuts import render_to_response from google.appengine.ext.db import djangoforms import models class ShoutForm(djangoforms.ModelForm): class Meta: model = models.shout exclude = ['mtime', 'user'] query = models.shout.gql("order BY mtime DESC") def index(request): return render_to_response('index.html', {'shouts': query.run(), 'form': ShoutForm()}) 20

Using Templates Put this in templates/index.html {% for shout in shouts %} <p>{{ shout.user }} : {{ shout.title }} <br> {{ shout.text }} <br> {{ shout.mtime timesince }}<hr> </p> {% endfor %} <form method="post" action="/post/"> <table> {{ form }} <tr><td><input type="submit"></td></tr> </table> </form> 21

The post() Method Add to urls.py: (r'^post/$', 'post'), Add to views.py: from google.appengine.api import users from django.http import HttpResponseRedirect def post(request): form = ShoutForm(request.POST) if not form.is_valid(): return render_to_response('index.html', {'form': form}) shout = form.save(commit=false) shout.user = users.get_current_user() shout.put() return HttpResponseRedirect('/') 22

Writing Tests Python has two test frameworks: doctest.py Tests are interactive sessions captured in doc strings Really easy to get started Some caveats (output must be reproducible) unittest.py Tests are methods in classes derived from unittest.testcase Modeled after JUnit 23

Running Tests Use whichever test framework you like./manage.py test myapp Runs all tests found in myapp Specifically: looks for doctest and unittest tests looks in models.py and tests.py 24

Example Doc Tests Put this in myapp/tests.py r""" >>> from django.test.client import Client >>> from models import * >>> c = Client() >>> r = c.get('/') >>> r.status_code 200 >>> r.content '\n\n<form...</form>\n' >>> s = Shout(title='yeah', text='hello world') >>> key = s.put() >>> r = c.get('/') >>> r.status_code 200 >>> assert 'yeah' in r.content >>> """ 25

Test Fixtures./manage.py: manages the datastore used./manage.py runserver: uses a persistent datastore in /tmp./manage dumpdata: writes datastore contents to stdout Format can be JSON (default) or XML (not fully supported yet)./manage loaddata: loads datastore from a file Fixtures can also be added to TestCase classes: from django.test import TestCase class MyTestCase(TestCase): fixtures = ['test_data.json', 'more_test_data'] def testsomething(self):... 26

Q & A Your turn! 27