Slide 1

Slide 1 text

Flasky Goodness Kenneth Reitz (Or Why Django Sucks?)

Slide 2

Slide 2 text

Hi.

Slide 3

Slide 3 text

@kennethreitz

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Open Source

Slide 6

Slide 6 text

Python-Guide.org • Documented best practices. • Guidebook for newcomers. • Reference for seasoned veterans. • Don’t panic & always carry a towel. The Hitchhiker’s Guide to Python

Slide 7

Slide 7 text

Requests HTTP for Humans

Slide 8

Slide 8 text

Httpbin.org $ curl http://httpbin.org/get?test=1 { "url": "http://httpbin.org/get", "headers": { "Content-Length": "", "Connection": "keep-alive", "Accept": "*/*", "User-Agent": "curl/7.21.4 ...", "Host": "httpbin.org", "Content-Type": "" }, "args": { “test”: “1” }, "origin": "67.163.102.42" }

Slide 9

Slide 9 text

Et Cetera • Legit: Git Work ow for Humans • Envoy: Subprocess for Humans • Tablib: Tabular Data for Humans • Clint: CLI App Toolkit • Autoenv: Magic Shell Environments • OSX-GCC-Installer: Provokes Lawyers 275+ More

Slide 10

Slide 10 text

Open Source All The Things!

Slide 11

Slide 11 text

Build for Open Source • Components become concise & decoupled. • Concerns separate themselves. • Best practices emerge (e.g. no creds in code). • Documentation and tests become crucial. • Code can be released at any time.

Slide 12

Slide 12 text

Abstraction

Slide 13

Slide 13 text

Let’s build something. We’ll use Django, of course.

Slide 14

Slide 14 text

Django Benefits • Makes modular decisions for you. • Makes security decisions for you. • Excellent documentation available. • Installable third-party Django apps. • Tremendous resources & community.

Slide 15

Slide 15 text

Django Handles Things • Admin & Management Interface • Database Schema & Migrations • User Pro les and Authentication • User Sessions and Cookies • Internationalization

Slide 16

Slide 16 text

Anything is possible*.

Slide 17

Slide 17 text

Django Application

Slide 18

Slide 18 text

Django Application Tools & Utilities Web Process Worker Process

Slide 19

Slide 19 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process

Slide 20

Slide 20 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks

Slide 21

Slide 21 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 22

Slide 22 text

Single Codebases are Great • All the bene ts of the Django stack. • Figure out architecture as you go. • Shared modules keep you DRY. • Make broad and sweeping changes quickly. • Only need to deploy once.

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 25

Slide 25 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 26

Slide 26 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 27

Slide 27 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 28

Slide 28 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 29

Slide 29 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 30

Slide 30 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 31

Slide 31 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 32

Slide 32 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 33

Slide 33 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 34

Slide 34 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 35

Slide 35 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 36

Slide 36 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 37

Slide 37 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 38

Slide 38 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 39

Slide 39 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 40

Slide 40 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 41

Slide 41 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 42

Slide 42 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 43

Slide 43 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 44

Slide 44 text

Django Application Management Tools Supporting Services Tools & Utilities Web Process Worker Process Scheduled Tasks Deferred Tasks API Service CRUD Admin Data Persistence User Interface Authentication

Slide 45

Slide 45 text

Single Codebases are Evil! • Components are tightly coupled. • Broad tribal knowledge is required. • Iterative change of components di cult. • Technical debt has a tendency to spread. • Forced to deploy everything at once.

Slide 46

Slide 46 text

Anything is possible*. * But it’ll often be a monolithic app.

Slide 47

Slide 47 text

CONSTRAINTS FOSTER CREATIVITY

Slide 48

Slide 48 text

Constraints are Good • Text Editors vs IDEs • Prime vs Zoom Lenses • Mac OS X vs Desktop Linux • Pen & Paper vs Digital Notes • Monolithic Apps vs Services

Slide 49

Slide 49 text

Keep ‘em Separated

Slide 50

Slide 50 text

Data Persistence Developers End Users API Service Frontend

Slide 51

Slide 51 text

Build for Open Source • Components become concise & decoupled. • Concerns separate themselves. • Best practices emerge (e.g. no creds in code). • Documentation and tests become crucial. • Code can be released at any time.

Slide 52

Slide 52 text

Build for Services • Components become concise & decoupled. • Concerns seperate themselves. • Best practices emerge (e.g. ideal tools). • Documentation and contracts become crucial. • Services can be scaled separately at any time • Dogfood is delicious.

Slide 53

Slide 53 text

Composability

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

End Users

Slide 56

Slide 56 text

End Users Developers

Slide 57

Slide 57 text

End Users API Service Developers

Slide 58

Slide 58 text

API Service End Users API Service Developers

Slide 59

Slide 59 text

API Service End Users API Service Developers

Slide 60

Slide 60 text

API Service End Users API Service API Service Developers

Slide 61

Slide 61 text

API Service End Users API Service API Service Developers ( )

Slide 62

Slide 62 text

API Service End Users API Service API Service Developers ( ) Data Persistence

Slide 63

Slide 63 text

API Service End Users API Service Internal API Service Developers ( ) Data Persistence

Slide 64

Slide 64 text

API Service End Users API Service Internal API Service Developers ( ) Data Persistence Message Queue Workers

Slide 65

Slide 65 text

Let’s try it out. We’ll use Django, of course.

Slide 66

Slide 66 text

Django: It Just Sits There™

Slide 67

Slide 67 text

Two Basic Components API Service & Frontend Client API Service Frontend Client Data Interface Business Logic Authentication User Interface State and Sessions Public Face

Slide 68

Slide 68 text

Django for API Services • Signi cant boilerplate code for simple views. • No need for templates, tags, etc. • API libraries are buggy; could use some love. • if request.method == 'POST' (and nothing else)

Slide 69

Slide 69 text

Django as API Consumer • Keep in mind, database is handled by API. • Makes modular decisions for you. • Deals with the database for you. • Installable third-party Django apps. (and nothing else)

Slide 70

Slide 70 text

Django as API Consumer • Everything is tied to the ORM. • Third-party Django apps do too. • User model requires sessions; isn’t exible. (and nothing else)

Slide 71

Slide 71 text

What’s Left? ?

Slide 72

Slide 72 text

Flask

Slide 73

Slide 73 text

Enter, Flask. • HTTP Web Framework based on Werkzeug. • Excellent for building web services.

Slide 74

Slide 74 text

Elegant & Simple from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()

Slide 75

Slide 75 text

Flask Familiarities • WSGI Application Framework. • Built-in Templating System (Jinja2). • Active community, extensions for everything.

Slide 76

Slide 76 text

Flask Philosphies • Started as an April Fool’s joke. • Very minimal; 800 lines of code. • Heavily tested; 1500 lines of tests. • Exhaustively documented; 200 pages of docs. • Layered API; built on Werkzeug, WSGI.

Slide 77

Slide 77 text

Flask Differences • Explicit & passable app objects. • Simple, elegant API. No boiler plate. • BYOB: Bring Your Own Batteries. • No built-in ORM or form validation. • Context locals. Keeps things looking clean.

Slide 78

Slide 78 text

Flask Improvements • Fewer batteries == greater exibility. • Jinja2 is an incredible template system. • Everything harnesses actual references. • Con guration is a simple dictionary. • It’s hard to build monolithic applications. • Response objects are WSGI applications.

Slide 79

Slide 79 text

Flask Improvements • Werkzueug debugger. • No import-time side e ects. • Signals system outside of ORM. • Tests are simpler with real app objects. • return (content, status)

Slide 80

Slide 80 text

Popular Extensions • Flask-SQLAlchemy: Database Mapper. • Flask-Celery: Delayed Jobs. • Flask-Script: Management Commands. • Flask-WTF: Form Validation.

Slide 81

Slide 81 text

Shameless Plug • Flask-SSLify: App HSTS (SSL) Policies • Flask-GoogleFed: Google Federated Auth • Flask-Heroku: Env Variable Con gurations

Slide 82

Slide 82 text

Flask is a sharp tool for building sharp services. Use the right tool for the job.

Slide 83

Slide 83 text

Torn?

Slide 84

Slide 84 text

Flask vs.Django

Slide 85

Slide 85 text

Alcohol vs. Ponies

Slide 86

Slide 86 text

Why not both?

Slide 87

Slide 87 text

Services are agnostic. Just speak HTTP.

Slide 88

Slide 88 text

github.com/kennethreitz Questions?