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 full-size slide

  2. @kennethreitz

    View full-size slide

  3. 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 full-size slide

  4. Requests
    HTTP for Humans

    View full-size slide

  5. 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 full-size slide

  6. 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 full-size slide

  7. Open Source
    All The Things!

    View full-size slide

  8. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. Anything is possible*.

    View full-size slide

  13. Django Application

    View full-size slide

  14. Django Application
    Tools & Utilities Web Process Worker Process

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  18. 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 full-size slide

  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

    View full-size slide

  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

    View full-size 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 full-size slide

  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

    View full-size slide

  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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

  40. 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 full-size slide

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

    View full-size slide

  42. CONSTRAINTS
    FOSTER
    CREATIVITY

    View full-size slide

  43. 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 full-size slide

  44. Keep ‘em Separated

    View full-size slide

  45. Data Persistence
    Developers End Users
    API Service Frontend

    View full-size slide

  46. 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 full-size slide

  47. 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 full-size slide

  48. Composability

    View full-size slide

  49. End Users
    Developers

    View full-size slide

  50. End Users
    API Service
    Developers

    View full-size slide

  51. API Service
    End Users
    API Service
    Developers

    View full-size slide

  52. API Service
    End Users
    API Service
    Developers

    View full-size slide

  53. API Service
    End Users
    API Service
    API Service
    Developers

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  59. Django: It Just Sits There™

    View full-size slide

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

    View full-size slide

  61. 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 full-size slide

  62. 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 full-size slide

  63. 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 full-size slide

  64. What’s Left?
    ?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  68. 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 full-size slide

  69. 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 full-size slide

  70. 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 full-size slide

  71. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  75. Flask vs.Django

    View full-size slide

  76. Alcohol
    vs.
    Ponies

    View full-size slide

  77. Why not both?

    View full-size slide

  78. Services are agnostic.
    Just speak HTTP.

    View full-size slide

  79. github.com/kennethreitz
    Questions?

    View full-size slide