Should I Use Flask or Django for Web App Development?
At some point you will want to write a web application. In python the two most popular web frameworks are Flask and Django. In this post I detail the pros and cons of each and which one should you use. I have used both so I have a pretty good picture of both.
As most python developers I started with Flask. It is called a micro-framework, because it provides the bare minimum and with just 5 lines of code, you have your first app working. If you want extra stuff, you have to add them as third party packages.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!'
From the official documentation: “The “micro” in microframework means Flask aims to keep the core simple but extensible. Flask won’t make many decisions for you, such as what database to use. Those decisions that it does make, such as what templating engine to use, are easy to change. Everything else is up to you, so that Flask can be everything you need and nothing you don’t.”
Third Party maintainance vs inbuilt functionality
Sounds great isn’t it? Lets see. If you want to write a full fledged app, you will want a database. No problem. There is flask-sql-alchemy. Now I need to make a migration. No problem, install flask-db-migrate. And I need forms. Ok no problem flask-wtf. And I need sessions. Lets install flask-session. And I need admin area, so lets install flask-admin, and the list goes on…
What seems like a micro-framework, it will soon be a full app, bloated with all the other third party packages, that you will have to install and update as needed and make sure, that each update does not break any dependencies with all the others.
A lot of the third party apps that you will find and some are very popular, are no longer maintained. So what happens if a new bug is found? Basically you are left on your own. As a word of caution, always look at github to see when it was last updated or if it still maintained.
And now lets move to Django. Django provides in the box all the functionality that in Flask is implemented by add ons: Forms, Sessions, Login, Admin, ORM, Caching etc. It has an LTS version which is supported for 5 years. No need to worry about security on individual packages like in Flask. Everything is included. Of course there are third party packages that you can use, but they are not needed in order to create a full fledged app.
Code Structure - What seems simple is not very simple
Flask does not impose any structure on your code. You can write all your code in one file, like so many bad apps that you will find on the web. Even if you decide to split your code, you can run into problems like circular import. Package A imports B and B imports A. There are ways to get around this, but in my opinion not very elegant. There is also another package called blueprints that helps you organize your code better, but in the end if you do everything it says, you will end up in a structure like django. And where will you put your settings? Again so many bad apps put them in the main file and not in a separate file. So in the end, the things that seem as advantages become disadvantages.
On the other hand Django organizes your project into apps and each app has its own file for models, views, and routes. No need to worry where to put each piece of code and how each one is imported to your main app. And there is a separate file for settings where you store all your settings.
So can you write bad code on Django? Of course you can. Just because it provides a better structure, it does not mean that you cannot write bad code. I have seen apps that make 100s of sql queries in one page, just because the novice programmer does not understand what’s going on when he writes something like author.user.username. that traverses multiple tables. If you decide to use django please make sure to read everything you can about query optimization and use sql queries wherever you can, instead of python. The other thing is that most people test their app on their local machine which is fast and the db has only a few entries and then when they take it to production they wonder why it is so slow.
Obviously Django has a more steep lurning curve. When you start a project there are so many files that are created that you wonder what they do. But if you create 2-3 projects, it all starts to make sense and you are able to follow the logic. At the end if you think that you don’t have to learn only flask, but also the documentation of all the other third party packages, it seems to me that django is easier to learn.
Documentation in Django is far superior. Django has a very extensive documentation that you will not find similar on the packages of flask.
The ORM also provides extensive functionality which I think is superior to flask sql alchemy.
Deployment to production.
Since both they are wsgi applications they need a wsgi server like uwsgi or gunicorn and nginx for static file serving, or you can use AWS S3. I would say the deployment difficulty is the same, if you want to deploy them properly.
REST API functionality
Django has mainly the django rest framework, which is a third party app, which is maintained and well documented. Flask has so many that change all the time, and some of them become obsolete. An author stops developing it, then there is a fork and the same thing goes on. For instace there was a project flask-restplus. The original author stopped developing it and then there was a fork called flask-restx.