biw15: python in the cloud: django and flaks

19
Baltimore Innovation Week 2015 Python in the Cloud: Django vs Flask Alex Viana (@AlexVianaPro) Terbium Labs (@TerbiumLabs) and

Upload: alexcostaviana

Post on 15-Jan-2017

51 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: BIW15: Python in the Cloud: Django and Flaks

Baltimore Innovation Week 2015

Python in the Cloud: Django vs Flask

Alex Viana (@AlexVianaPro)Terbium Labs (@TerbiumLabs)

and

Page 2: BIW15: Python in the Cloud: Django and Flaks

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.

Page 3: BIW15: Python in the Cloud: Django and Flaks

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.

Page 4: BIW15: Python in the Cloud: Django and Flaks

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?

Page 5: BIW15: Python in the Cloud: Django and Flaks

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

Page 6: BIW15: Python in the Cloud: Django and Flaks

Flask’s Hello World

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

Page 7: BIW15: Python in the Cloud: Django and Flaks

Your Library as a Flask Appfrom 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()

Page 8: BIW15: Python in the Cloud: Django and Flaks

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.

Page 9: BIW15: Python in the Cloud: Django and Flaks

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.

Page 10: BIW15: Python in the Cloud: Django and Flaks

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?

Page 11: BIW15: Python in the Cloud: Django and Flaks

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?

Page 12: BIW15: Python in the Cloud: Django and Flaks

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…

Page 13: BIW15: Python in the Cloud: Django and Flaks

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…

Page 14: BIW15: Python in the Cloud: Django and Flaks

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.

Page 15: BIW15: Python in the Cloud: Django and Flaks

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.

Page 16: BIW15: Python in the Cloud: Django and Flaks

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.

Page 17: BIW15: Python in the Cloud: Django and Flaks

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.

Page 18: BIW15: Python in the Cloud: Django and Flaks

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.

Page 19: BIW15: Python in the Cloud: Django and Flaks

Thank you for your attention.

Questions?