Possibility and Probability

Coding Python and making businesses…

25 July 2016

Why flask

by admin

“Why flask? Why not Django?” Recently I was asked this after introducing a new subsystem at work. Originally part of a monolithic Django app, this was new micro-service was one of the first pieces to be split out on its own. I had chosen Flask to be the web framework, and now it was time to explain why…

I’m not anti-Django, but…

Simplicity. Compared to Django, flask is pretty simple. It is intentionally minimalist which leads to less overhead when it comes to both learning flask, and writing flask applications. This minimalism means that when something needs to be done it is usually done via a 3rd party library. And in a lot of cases, there is more than one to choose from. This is a departure from most frameworks, and it does have its drawbacks, such as making sure to choose the right library. But this flexibility does allow a developer to tune their code to meet the requirements, instead of the other way around.

We don’t need the kitchen sink

This particular microservice does not really need to talk to a database. Its purpose is to read some web traffic, make some slight transformations, and then pass it off to a worker. So things we don’t need: templates (or any kind of front end GUI code), database models. Flask does include support for templates, and it is very friendly with database ORM’s. But… these are not strictly required to make an app. To be very clear, these are not a requirement for a Django app either. However, Django is a very UI/CRUD centric framework. It is designed to support those kinds of apps and does a great job of doing that. Flask on the other hand is very much a  ”Bring your own " framework. For this microservice, this fits much nicer: Instead of starting with the models (which is usually a great idea), we were able to start with the API. This focus on our API allows our attention to be overall objective; to have this microservice work with other microservices and communicate along well defined lines.

Heavyweight champion of the world!!!

I personally have not experienced this, but many people feel that Django is resource intensive. Claims of bloat (due to a wide base of support in the framework) are thrown about in conversations about slow performance. Experienced software developers will take that under considerations, but experience also tells us that it isn’t always the root cause of the slowdowns. There is a nugget of truth in the accusation of bloat however: there is a lot of stuff in the framework, and when it is time to debug something it means that there is that much more to “load into your working memory” in order understand what is going on.

So is flask perfect?

Nope! The test support that the Django ecosystem provides is truly enviable. Flask has good testing support, but the tight integrations in Django make testing a lot easier to do in my opinion. If there is one thing python has, it is a lot of web frameworks. And with this wide selection comes a lot of commentary on why one’s framework is the best (this article for example. :) ) Other lightweight frameworks might be better in other situations. Bottle for example is a really great way to sneak a small webapp into another larger project.

This is why flask

Small, configurable, easy to work with. Those are probably the best and most succinct ways to answer the question “why flask”.

tags: