EXPERIENCES MOVING FROM DJANGO TO FLASK DAN O BRIEN, VP OF ENGINEERING CRAIG LANCASTER, CTO Jana Mobile Inc. www.jana.com
WHO WE ARE Jana is a startup company in Boston connecting advertising and marketing companies to consumers in emerging markets. Via connections to cell phone carriers and vendors, Jana has the capability to send money to most of humanity. We consider ourselves primarily a technology company; mostly engineers (~15/30) Continuous delivery! Cassandra! We re hiring! http://www.jana.com/about/careers/ 2
OUR DJANGO EXPERIENCE: CONDUIT LABS AND MUSIC PETS We both worked at Conduit Labs in 2009-10, where (the now defunct) Music Pets was developed using Django 1.0 This was just when Django 1.0 was coming out Supported ~100k daily users with a very high write-to-read ratio on the database (~1:2) Used a MySQL backend with Django s ORM Django has come a long way since then for example, the test framework stuff is new, and looks great. 3
OUR FLASK EXPERIENCE: JANA AND MCENT We both now work at Jana! Consumer-facing web site (~1M page views/day) Consumer-facing Android app (~1M page views/day) Talks to a JSON REST API Backend process for mobile carrier/vendor communication via REST Internal-use web sites NoSQL backend Mixed use of CouchDB, Cassandra, redis, Hadoop, MySQL 4
WHAT FLASK IS Django and Flask are trying to solve different problems Flask is a simple, lightweight solution to a proper subset of Django s functionality: Ability to map URLs to functions (including managing requests, responses, headers, etc) HTML template rendering creating html responses based on variable values If you re just making a REST API, you might not even need the second thing. 5
DJANGO URL mapping Template rendering! Model Architecture / ORM User & admin management Admin site Test framework! Plugin Architecture 6
FLASK URL mapping Template rendering (Jinja2)! Plugin Architecture! Of note: the model architecture is decoupled from the web framework it s an entirely separate tool in the toolbox. 7
STARTING SMALL: THE SIMPLE FLASK APP A Flask app can be just one.py file I don t have to understand the difference between a project and and an app to get started. I don t need a lot of files that I am not yet using. Adding new endpoints means editing one file. 8
STARTING SMALL: THE SIMPLE DJANGO APP Compare that with the simple Django app: Create project Create app Edit views.py in one directory Edit urls.py in another directory While none of us aspire to build Hello, World, there is something to be said for starting small and building out the architecture as needed. 9
A SLIGHTLY LARGER EXAMPLE https://github.com/jana-mobile/nanoblog The model class stands alone; it doesn t import any code from, say, flask. It can use whatever python libraries are appropriate for its functionality in these cases, sqlalchemy and redis. This is a somewhat simple & silly example, but it s a typical pattern for flask apps. If more models were added, we may want to further abstract away the storage mechanism sqlalchemy has some mechanisms for this, and other libraries exist. 10
YOU DON T NEED A PLUGIN Often the functionality you need needn t be coupled to flask in any way. Instead of how do I do this in django?, you re asking, how do you do this in python? Model architecture and ORM s sqlalchemy is popular often simple python wrapper libraries are sufficient Test frameworks unittest, nose, mock, etc work well for us 11
BUT IF YOU DO NEED A PLUGIN There are also lots of Flask plugins available, and it s easy to write your own. Here are some of the pieces of functionality & plugins that we use: blueprints: similar to django apps Flask-Login: session management Flask-OAuth / Flask-OpenID Flask-Assets: merge & compress js/css Flask-Babel: translation & localization management 12
SUMMARY Django and Flask are both great pieces of software! Much of the preference hinges on whether Django s architecture resonates with you and is appropriate for your project.! Feel free to reach out! danob@jana.com craig@jana.com twitter: @janatechteam http://technology.jana.com/ 13