A fork or a spoon: Comparing Flask and FastAPI side by side.


The first framework I came across in my career was Flask. Flask is known for a long time now, and a lot of people absolutely love it. There is a good reason: Flask is a jack-of-all-trades that provides a decent-looking web app in the end. Alongside a relative simplicity, Flask will please you if you enjoy tinkering with HTML files. No web dev team? No JavaScript experience? No problem! With a minimum knowledge of how HTML tags work, you can download a ready HTML template and tailor it to your needs.
Need to mention, that Flask is also friendly with plotly.dash meaning that any python-plotted graph can be easily displayed on your web app.
However, there are some negative aspects of using Flask that I faced working with it. First of all, inconsistent stability. Flask likes to complain about different things, it is very sensitive to the directory from where you attempt to run it. There are certain limitations in file naming, you cannot name any of your files flask.py — which is a minor problem.


Much later, I joined a team building a project using FastAPI. FastAPI is a relatively new framework that is skyrocketing in popularity nowadays. At first glance, I did not like FastAPI — it seemed to be overly complex.
Yet after some time spent with FastAPI documentation, things became clear for me and I started seeing the advantages of using FastAPI.
FastAPI is really fast — at least, works faster than Flask due to the Starlette toolkit under the hood and ASGI. At the same time, FastAPI is just a backend API. It is a tool you would use for one purpose only — like a bottle opener. You cannot call it a multitool, but it does its one job very well. And its job is to reliably capture input data through the ‘GET’ route, do some operations using Python/SQL/Machine Learning, and output it through the ‘POST’ route. More importantly, FastAPI readily serves return data in JSON format which is a grab-n-go for the web development team.

app.include_router(db.router, tags=['Database'])app.include_router(ml.router, tags=['Machine Learning'])app.include_router(viz.router, tags=['Visualization'])

Which one?

The answer is: depends on the scale. For a small project that you want to do yourself, Flask is generally a better option — it’s better to have some user GUI than nothing at all. It is still possible to do the large project with Flask, however, you would have to solve a row of problems such as converting final data to a JSON object, fast response issues — especially if you have a machine learning application that takes time to output results. I am working on another project at the moment that is built on Flask. This project does not have any complicated machine learning system built-in, it is not meant to handle a lot of requests, neither it needs a superb frontend page. For such a case, Flask is more than sufficient.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store