Terbium Labs uses both Flask and Django frameworks for different roles. Flask is used for most services due to its lightweight nature. However, some larger REST APIs became difficult to manage in Flask. Django REST framework provided a solution with features for authentication, serialization, and permissions out of the box. Flask is best for simple apps, while Django REST framework handles more complex needs like a full-featured REST API. The key is choosing the right tool for each job.
1. Baltimore Innovation Week 2015
Python in the Cloud:
Django vs Flask
Alex Viana (@AlexVianaPro)
Terbium Labs (@TerbiumLabs)
and
2. Terbium Labs
❖ Terbium Labs is a Baltimore-based data intelligence
company. Our product, Matchlight, allows customers to
monitor the dark web for stolen data.
❖ Matchlight is primarily written in Python, running on
AWS.
3. Motivation
❖ Django and Flask are the two most popular Python web
frameworks.
❖ We use both frameworks to fill different roles in our
work at Terbium Labs.
❖ It was surprisingly difficult to come to a firm decision
that this was the right direction.
❖ This talk is about what we learned in the process and
how it informed our decision.
4. Flask
❖ Flask is what the majority of our services runs on.
❖ Flask is a bare-bones Python web “microframework”.
❖ It’s great for building lightweight apps or
“microservices”.
❖ How lightweight? How Mirco?
5. Flask is Light
“Flask will never have a database layer. It will not have a form
library or anything else in that direction. Flask itself just bridges
to Werkzeug to implement a proper WSGI application and to
Jinja2 to handle templating. It also binds to a few common
standard library packages such as logging. Everything else is up
for extensions.”
- Flask Documentation
http://flask.pocoo.org/docs/0.10/design/#what-flask-is-what-
flask-is-not
6. Flask’s Hello World
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
7. Your Library as a Flask App
from flask import Flask
from my_module import important_function
app = Flask(__name__)
@app.route("/")
def hello():
results = important_function()
return results
if __name__ == "__main__":
app.run()
8. Flask in a Startup
❖ Don’t have to wrap your head around any new
framework patterns.
❖ Spend most of your time writing business logic not app
logic.
❖ Flask is excellent for the low-overhead rapid
implementation requirements start up development.
9. Issues with Flask
❖ On some of our larger REST API’s things got tricky.
❖ Lots of design considerations with side effects.
❖ Development was slowing down.
❖ Maybe Flask was the wrong choice? So we looked to
Django.
10. Django
❖ Django is far and away the most used Python web
framework.
❖ It’s the “batteries included” option: database ORM,
forms, admin interface, etc.
❖ But this means stopping everything and learning a new
framework. How do we know this is the right choice?
11. Python vs Django
❖ A little Googling on the internet give you something like
this:
❖ Django is for more full-featured applications.
❖ Flask is for more simple apps and rapid prototyping.
❖ Easy! But our product is made up of REST-ful micro
services, shouldn’t Flask be perfect?
12. More Confusion
❖ Flask is deployed at LinkedIn, Pinterest, Twillio, and the
2012 Obama election campaign. That’s big stuff!
❖ Miguel Grinberg’s Flask book builds a blog app with data
migrations, unit tests, authentication, etc. Those are a lot
of features!
❖ On the other hand we have the Django REST Framework.
REST is pretty lean, right?
❖ So who’s right? How do we choose? The clock is ticking…
13. Enter Django REST Framework
❖ So we gave Django REST framework a try.
❖ Django REST seemed like a contradiction at first; Django
just seemed too heavy for an API.
❖ But, we realized there is a -lot- that goes into a REST API:
authentication, serialization, permissions, data model,
queries, testing, pagination, …
❖ Then we had our breakthrough…
14. Flask and Django
❖ Think of Flask as a web framework framework!
❖ Put another way, Flask is not your framework. You use
Flask to build your framework.
❖ Development was slowing down because we weren’t
building our product, we were building our framework.
15. Flask as a Framework Framework
❖ In the “Hello World” case this is trivial, all you need is in
Flask, basically just a WSGI server.
❖ As you scale up you end up having to implement
everything yourself, a-la the Flask book.
❖ Once you deviate from that - you’re on your own.
❖ Which can be great! Unless it’s not.
16. Where Flask is Great
❖ This is why The Flask book is so great; it lays out all the
decisions for you.
❖ And this is why major projects can be written in Flask at
companies with deep resource and experience pools.
❖ And small project are a breeze because there’s literally
nothing else in the framework to think about.
❖ And this why everything in-between is so tough.
17. Building a Framework
❖ Is hard … just an FYI.
❖ Requires significant developer time.
❖ More importantly it requires informed opinions about
how your framework should work.
❖ Even beyond that it requires knowledge about what
decisions have to be made and how they interact.
18. Final Remarks
❖ The driver for improving our technology stack was a
sense we were working against, not with our tools. Don’t
ignore that, it’s a clue something is wrong.
❖ Once we realized the pain points and benefits of both
systems we could confidently select different tools for
different jobs. Don’t underestimate the impact of this.
❖ Both are great projects, we’re indebted to both teams.