Pro Yearly is on sale from $80 to $50! »

Flasky Goodness

2eccc4005572c1e2b12a9c00580bc86f?s=47 Kenneth Reitz
September 05, 2012

Flasky Goodness

2eccc4005572c1e2b12a9c00580bc86f?s=128

Kenneth Reitz

September 05, 2012
Tweet

Transcript

  1. Flasky Goodness Kenneth Reitz (Or Why Django Sucks?)

  2. Hi.

  3. @kennethreitz

  4. None
  5. Open Source

  6. 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
  7. Requests HTTP for Humans

  8. 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" }
  9. 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
  10. Open Source All The Things!

  11. 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.
  12. Abstraction

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

  14. Django Benefits • Makes modular decisions for you. • Makes

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

    Schema & Migrations • User Pro les and Authentication • User Sessions and Cookies • Internationalization
  16. Anything is possible*.

  17. Django Application

  18. Django Application Tools & Utilities Web Process Worker Process

  19. Django Application Management Tools Supporting Services Tools & Utilities Web

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

    Process Worker Process Scheduled Tasks Deferred Tasks
  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. 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.
  23. None
  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. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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.
  46. Anything is possible*. * But it’ll often be a monolithic

    app.
  47. CONSTRAINTS FOSTER CREATIVITY

  48. 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
  49. Keep ‘em Separated

  50. Data Persistence Developers End Users API Service Frontend

  51. 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.
  52. 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.
  53. Composability

  54. None
  55. End Users

  56. End Users Developers

  57. End Users API Service Developers

  58. API Service End Users API Service Developers

  59. API Service End Users API Service Developers

  60. API Service End Users API Service API Service Developers

  61. API Service End Users API Service API Service Developers (

    )
  62. API Service End Users API Service API Service Developers (

    ) Data Persistence
  63. API Service End Users API Service Internal API Service Developers

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

    ( ) Data Persistence Message Queue Workers
  65. Let’s try it out. We’ll use Django, of course.

  66. Django: It Just Sits There™

  67. Two Basic Components API Service & Frontend Client API Service

    Frontend Client Data Interface Business Logic Authentication User Interface State and Sessions Public Face
  68. 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)
  69. 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)
  70. 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)
  71. What’s Left? ?

  72. Flask

  73. Enter, Flask. • HTTP Web Framework based on Werkzeug. •

    Excellent for building web services.
  74. Elegant & Simple from flask import Flask app = Flask(__name__)

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

    (Jinja2). • Active community, extensions for everything.
  76. 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.
  77. 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.
  78. 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.
  79. 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)
  80. Popular Extensions • Flask-SQLAlchemy: Database Mapper. • Flask-Celery: Delayed Jobs.

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

    Google Federated Auth • Flask-Heroku: Env Variable Con gurations
  82. Flask is a sharp tool for building sharp services. Use

    the right tool for the job.
  83. Torn?

  84. Flask vs.Django

  85. Alcohol vs. Ponies

  86. Why not both?

  87. Services are agnostic. Just speak HTTP.

  88. github.com/kennethreitz Questions?