A couple of years ago, my team was responsible for maintaining a medium-sized Flask application that powered our back office. Like many Python developers at the time, I had heard about FlaskAPI and was growing curious about it. After reading through their amazing documentation and experimenting with it on a side project, I realised two things: I never wanted to write endpoints any other way, and FastAPI is not exactly a drop-in replacement for Flask.
First, there are many good reasons to choose FastAPI as a framework. But for me, what stood out the most was its aesthetics: the minimalist way it allows you to do advanced request parsing and response serialization, one type-hint at a time. You can cut down some good portions of your code and let the framework handle this bit more efficiently than you would. There is beauty in its simplicity and I think it's a good enough reason to consider FastAPI. Because behind the frugality of the code lies both a framework enhancement and a better maintainability of your application.
Second, although your first glance at FastAPI might leave you with the idea that it's a clone of Flask on steroids, it’s not. And the differences, while manageable, require a fair amount of adjustments. The most striking instance of that is the thriving eco-system of flask extensions that you have to leave behind. But more subtly, the tracebacks are quite different, making debugging a FastAPI application feels unfamiliar at first. Also, FastAPI's dependency injection contrasts with Flask’s use of global objects for accessing the database for example. That one boils down to taste and habits and is not a dealbreaker by any means. However, when migrating endpoints one at a time, juggling two frameworks can introduce some switching costs and it all needs to be worth your while.
The bad news for me was that, in my context, the migration wasn't worth the effort, leaving me between a rock and a hard place: sticking with writing boilerplate code I had grown to dislike, or pushing a framework migration that didn't make sense. What if I wondered, what if, I could have the best of two worlds—or rather, frameworks and bring FastAPI's aesthetics to my Flask Application without leaving it?
It took me a little bit of experimenting but my first implementation was dead simple — under 100 lines of code. It didn’t come close to covering the full feature set of FastAPI, but it allowed us to start writing endpoints in a FastAPI-like style and for me, that was enough: we could enjoy the frugal aesthetics of FastAPI without migrating a single endpoint, just type-hinting our way through it.
This implementation evolved, slowly adjusting our needs. It eventually allowed us to start serving customer-facing data-intensive features. All the while it remained minimal, the core principle of hacking type-hinting into a data validation tool is pretty easy to obtain. Once that piece of code had proven to be reliable, I decided to give it a go at publishing it into a fully-fledged Python package. This is where the hard work started.
The publishing part isn’t particularly difficult on its own, but while you're at it, you might as well do it right. This required setting up automation tools, beefing up the test suite, and writing comprehensive documentation, which is time-consuming. I also did a complete overhaul of the code for it to be more extendable and accommodate use cases outside our initial context. It involved a deep dive into Flask and FastAPI inner workings and was the most interesting part of the journey.
At the end of the day, I had built my first full-blown open-source project at the crossroads of my two favourite web frameworks and had a powerful tool available one pip install away to bring into any other Flask project I might have to work on.
What about you? As a Flask developer, did FastAPI change your approach to writing endpoints? or do you have another aesthetic that works best for you?
--
In case you want to check out the repo, it lives on github.
The documentation is hosted on readthedocs.
For the reckless, you can jump right it with a pip install flask-jeroboam
and give it a try.
Top comments (0)