Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flasky Goodness

Avatar for Kenneth Reitz Kenneth Reitz
September 05, 2012

Flasky Goodness

Avatar for Kenneth Reitz

Kenneth Reitz

September 05, 2012
Tweet

More Decks by Kenneth Reitz

Other Decks in Programming

Transcript

  1. Hi.

  2. 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
  3. 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" }
  4. 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
  5. 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.
  6. Django Benefits • Makes modular decisions for you. • Makes

    security decisions for you. • Excellent documentation available. • Installable third-party Django apps. • Tremendous resources & community.
  7. Django Handles Things • Admin & Management Interface • Database

    Schema & Migrations • User Pro les and Authentication • User Sessions and Cookies • Internationalization
  8. Django Application Management Tools Supporting Services Tools & Utilities Web

    Process Worker Process Scheduled Tasks Deferred Tasks
  9. 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
  10. 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.
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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.
  33. 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
  34. 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.
  35. 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.
  36. API Service End Users API Service Internal API Service Developers

    ( ) Data Persistence Message Queue Workers
  37. Two Basic Components API Service & Frontend Client API Service

    Frontend Client Data Interface Business Logic Authentication User Interface State and Sessions Public Face
  38. 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)
  39. 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)
  40. 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)
  41. Elegant & Simple from flask import Flask app = Flask(__name__)

    @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
  42. Flask Familiarities • WSGI Application Framework. • Built-in Templating System

    (Jinja2). • Active community, extensions for everything.
  43. 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.
  44. 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.
  45. 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.
  46. 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)
  47. Popular Extensions • Flask-SQLAlchemy: Database Mapper. • Flask-Celery: Delayed Jobs.

    • Flask-Script: Management Commands. • Flask-WTF: Form Validation.
  48. Shameless Plug • Flask-SSLify: App HSTS (SSL) Policies • Flask-GoogleFed:

    Google Federated Auth • Flask-Heroku: Env Variable Con gurations