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

Flasky Goodness

Kenneth Reitz
September 05, 2012

Flasky Goodness

Kenneth Reitz

September 05, 2012
Tweet

More Decks by Kenneth Reitz

Other Decks in Programming

Transcript

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

    View Slide

  2. Hi.

    View Slide

  3. @kennethreitz

    View Slide

  4. View Slide

  5. Open Source

    View Slide

  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

    View Slide

  7. Requests
    HTTP for Humans

    View Slide

  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"
    }

    View Slide

  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

    View Slide

  10. Open Source
    All The Things!

    View Slide

  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.

    View Slide

  12. Abstraction

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  16. Anything is possible*.

    View Slide

  17. Django Application

    View Slide

  18. Django Application
    Tools & Utilities Web Process Worker Process

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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.

    View Slide

  23. View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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.

    View Slide

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

    View Slide

  47. CONSTRAINTS
    FOSTER
    CREATIVITY

    View Slide

  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

    View Slide

  49. Keep ‘em Separated

    View Slide

  50. Data Persistence
    Developers End Users
    API Service Frontend

    View Slide

  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.

    View Slide

  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.

    View Slide

  53. Composability

    View Slide

  54. View Slide

  55. End Users

    View Slide

  56. End Users
    Developers

    View Slide

  57. End Users
    API Service
    Developers

    View Slide

  58. API Service
    End Users
    API Service
    Developers

    View Slide

  59. API Service
    End Users
    API Service
    Developers

    View Slide

  60. API Service
    End Users
    API Service
    API Service
    Developers

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  66. Django: It Just Sits There™

    View Slide

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

    View Slide

  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)

    View Slide

  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)

    View Slide

  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)

    View Slide

  71. What’s Left?
    ?

    View Slide

  72. Flask

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  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)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  83. Torn?

    View Slide

  84. Flask vs.Django

    View Slide

  85. Alcohol
    vs.
    Ponies

    View Slide

  86. Why not both?

    View Slide

  87. Services are agnostic.
    Just speak HTTP.

    View Slide

  88. github.com/kennethreitz
    Questions?

    View Slide