Walk in the weird Flask eco-system
I started the draft of this post a few years ago (2016) and I’m published it now because to my surprise it still relevant in 2021!
I used to code my server-side Web applications with Django. I often verified the Django’ motto « The web framework for perfectionists with deadlines » but you know what? Mature frameworks can be boring! After various toy projects with Flask (eg. https://github.com/stephane/freezer), I choose to use it for something bigger. Here are my notes.
Project layout, it’s up to you!
No startproject command, many ways to organise apps and settings is a good recipe to waste time. Add a pinch of CLI changes between Flask 0.9 and Flask 0.10 to create a perfect nightmare: https://twitter.com/sraimbault/status/735762959171887104.
I was able to define a project layout which fit my needs eventually so I published it in the hope to help others but that sort of boilerplate won’t be used or maintained if it’s not official (as it’s done for Django or VueJS).
The time to render a template can be an issue with Django (600 ms to render a complex Bootstrap form with django-floppyforms is not acceptable) and the engine is very restrictive (by design) so I think Jinja2 is an exciting replacement. The integration of CSS framework such as Bootstrap or Material Design Lite is easier with the powerful Jinja macros and the “Highlighting Active Menu Items” trick is like magic ✨ when you come from Django.
Using Jinja2 was really cool, you can use it with Django too so do it!
The Django ORM has received some love lately but SQLAlchemy still supports more advanced features (clever model inheritance, avoid LEFT OUTER JOIN, subqueries…) especially if you use PostgreSQL (but of course, you do!). You should use the glue Flask-SQLalchemy but the documentation doesn’t really explain you how to organise your app to share the db instance and the SQLAlchemy documentation is bit messy (the concept of session isn’t easy to understand) and you’ll miss the methods for CRUD operations of Django.
Fast DB tests
The awful part comes when you want to write tests w/o creating/deleting the database before/after each test. At the time, I wrote https://github.com/stephane/flask-fast-sql-tests, I encountered serious issues with the app context (it seems now the situation is getting better). The fact that I did not find any documentation/code to write fast database tests with Flask drove me crazy. Really! I also had an issue with FactoryBoy to share the DB session.
What The Forms?
The development of WTForms was almost dead in 2016, my almost one year modest contribution still on the first page of git logs :/. It’s recommended to use flask-wtf glue but it’s hard to be confident on the durability of such code.
Update: the major v3.0.0 seems to be forthcoming.
The usual way to authenticate, with a simple POST of your credentials to obtain a token of session is not provided by Flask. Flask is a very minimal and modular Web framework so you have to rely on external solutions. It’s not a problem when the project umbrella provides tested and recommended solutions but sadly, that’s not the case. When I tried to use Flask-Security (v1.7.5), I found it very complex to use. I’m more willing to trust simple solutions so I finally only used Flask-Login and Flask-Bcrypt to handle authentication.
Several solutions exist to serialize your DATA, when there is no clear winner you have to test each :(
It’s complex to evaluate them and the devil is in the corner cases, at the end, I wasn’t happy with the required work to provide schema from models.
I don’t understand why Flask is so popular. I ran a few benchmarks against various Python Web framework (template and JSON with DB) ; Flask was a bit slower than Django so what’s the point to use that minimal framework?
Without the awesome work of David Lord, the project would be almost abandoned. If you’re looking for something modular (not Django), very convenient to use for an API, I think FastAPI is cleary a better option. Don’t want to install anything? Use Bottle.
Even before the release of FastAPI, Pyramid was a best choice IMHO:
- a good documentation with many cookbooks you can follow step by step.
- bootstrap your app with pcreate tool.
- Pylons project provides solid libs to handle forms (Deform and Colander).
I’m fully agree with the review of current Python ecosystem of FastAPI https://fastapi.tiangolo.com/alternatives/ so it’s still a mystery to me why Flask still a popular choice.
 Bear in mind this text has been written at the end of 2016