When Django is too bloated - Specialized Web-Applications with Werkzeug

When Django is too bloated - Specialized Web-Applications with Werkzeug

Did you ever think, Django and all the other “batteries included” frameworks are not flexible enough for your needs? Do you feel like they limit you in your creativity and design? Then this talk is for you!

Werkzeug is a very lightweight HTTP/WSGI utility for Python. You might have actually used it before, since the popular framework Flask is based on it. Werkzeug handles the WSGI communication with the web server and parsing of HTTP packets for you, after that, you are left to do whatever you want. No pre-defined ORM, no request dispatching or template rendering. As a developer you are supported with a live debugger that runs in the browser and a great variety of testing tools making it easy to write fine grained unit tests for your application.

As a developer at MPS - Medical Systems, I work with Werkzeug on a daily basis. One of our products is ChemoCompile, a chemo therapy planning, management and documentation tool used in hospitals in various European countries. It is a single-page web application written in Python (backend) and AngularJS (frontend). When we created it, we first prototyped it using Django, but soon realized, that we did not need most of the functionality that Django provides and many of our needs, like interfacing with hospital information systems, are too much out of the scope of a regular web applications. I will talk about, how we then discovered Werkzeug and built our own very customized stack on top of it and how you can do it too!

E8548a8288d4db41955731bd744350d2?s=128

Niklas Meinzer

July 13, 2017
Tweet

Transcript

  1. When Django is too bloated Specialized Web-Applications with Werkzeug EuroPython

    2017 – Rimini, Italy
  2. Photographer: Patrick Neumann Niklas Meinzer @NiklasMM Gotthard Base Tunnel

  3. Python is amazing for web developers! • Bottle • BlueBream

    • CherryPy • CubicWeb • Grok • Nagare • Pyjs • Pylons • TACTIC • Tornado • TurboGears • web2py • Webware • Zope 2
  4. Why would I want to use less? • Learn how

    stuff works
  5. Why would I want to use less? • Avoid over-engineering

    – Wastes time and resources – Makes updates harder – It’s a security risk.
  6. Why would I want to use less? • You want

    to do something very specific • Plan, manage and document chemotherapy treatments • Built with modern web technology • Used by hospitals in three European countries
  7. Database Patient Data Lab Data Printers Pharmacy System REST HL7

  8. Werkzeug = German for “tool” • Developed by pocoo team

    @ pocoo.org – Flask, Sphinx, Jinja2 • A “WSGI utility” • Very lightweight • No ORM, No templating engine, etc • The basis of Flask and others
  9. Werkzeug Features Overview • WSGI – WSGI 1.0 compatible, WSGI

    Helpers • Wrapping of requests and responses • HTTP Utilities – Header processing, form data parsing, cookies • Unicode support • URL routing system • Testing tools – Testclient, Environment builder • Interactive Debugger in the Browser
  10. A simple Application

  11. A simple Application

  12. URL Routing

  13. Middlewares Part of Application without DB access Part of Application

    with DB access • Separate parts of the Application as wsgi apps • Combine as needed Static files User auth Dispatcher DB conn Request Response
  14. None
  15. HTTP Utilities • Work with HTTP dates • Read and

    dump cookies • Parse form data
  16. Using the test client

  17. Using the test client - pytest fixtures

  18. Using the test client - pytest fixtures

  19. Interactive debugger in the Browser

  20. Endless possibilities • Connect to a database with SQLalchemy •

    Use Jinja2 to render documents • Use Celery to schedule asynchronous tasks • Talk to 3rd party APIs with requests • Make syscalls • Remote control a robot to perform tasks at home
  21. Thank you! NiklasMM @NiklasMM Photographer: Patrick Neumann