Comparison of FastAPI and Flask. Simple Explanation!
By: Raoof Naushad
Tags: Comparison, Fastapi, Flask, Software Engineering, Advantages

Whenever a developer take step to machine learning or deep learning, there are some stuffs that are considered as essential. Few of them are

Concepts of machine learning.

Python or R Languages — considered as the most important language in ML

Tensorflow or Pytorch — end to end opensource platform for ML and DL

Flask — To productionize an ML model we usually wrap a model in a REST API (mostly flask) and serve it as a micro service.

Docker — Platform that use OS-level virtualization to deliver software in packages called containers.

Let’s discuss on a better alternative of Flask micro service which is FastAPI .

If you are in a hurry jump to the comparison of FastAPI and Flask which will give you brief idea.

I think its time to learn new things. Let’s do it…

Flask

Flask is currently the primary choice of writing APIs for machine learning frameworks in python. Why so? Flask has many advantages when it is compared to its counterpart which is Django. Even though there are other alternatives flask was compared to Django which has more functionalities but is more complex than Flask.

Flask is a framework based on the current/old standard for Python web frameworks: WSGI.

The main advantage of flask as far as I have concerned is its design which is very lightweight and also modular. It also provides great community support and provides good documentation for developers to start working. Flask comes 100% compliant with WSGI makes it easy to deploy for production. Flask is ORM-agnostic and developer can plug in their favorite ORM. It can also handle HTTP request easily and it is flexible than its compared counterpart which is Django. Last but not the least since most of the data scientist and machine learning developers work with Python and Flask is written in python, resulted Flask to be widely used. But flask also has some cons as well. Few of them are Flask will be time-consuming if we are dealing with a big project. Also flask relies on many dependencies for example for admin site it uses Flask-admin, it doesn’t have a default template engine instead it comes with another dependency freeloaded with flask which is Jinja2.

FastAPI

FastAPI is a framework build on top of Starlette and Uvicorn. Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services. Uvicorn is a lightning-fast ASGI server, built on uvloop and httptools. With all this as the name suggest FastAPI is the fastest micro service framework.

Comparison of Flask and FastAPI

As we have already mentioned, Flask is a framework based on the current/old standard for Python web frameworks WSGI. FastAPI is a framework build on top of Starlette and Uvicorn.

Almost everything you can do in flask can also be done with Starlette which mean with FastAPI too. As FastAPI is based on Starlette, it inherits all its features. Including WebSockets, GraphQL support, templates, etc. Most of all FastAPI learned from Flask and inherits several things including its simplicity.

Why should we choose FastAPI over Flask?

FastAPI is considered as the fastest python web framework .It outperforms flask by more than 100%.

.It outperforms flask by more than 100%. Another important reason you can try switching is Auto scaling . Since Flask is being designed for WSGI services like Gunicorn it doesn’t provide native async support. For auto scaling it is required to asynchronously count queued and in-flight requests.

. Since Flask is being designed for WSGI services like Gunicorn it doesn’t provide native async support. For auto scaling it is required to asynchronously count queued and in-flight requests. FastAPI provides us with powerful dependency injection system that means infinite plug-ins . When you are using FastAPI your API will be compatible with OpenAPI, JSON Schema and OAuth2.0 which will be handled by FastAPI.

. When you are using FastAPI your API will be compatible with OpenAPI, JSON Schema and OAuth2.0 which will be handled by FastAPI. Decreased latency and high throughput is another advantage provided by FastAPI over Flask. This will help in neglecting more hardware dependencies on some cases which helps to reduce cost too.

is another advantage provided by FastAPI over Flask. This will help in neglecting more hardware dependencies on some cases which helps to reduce cost too. It is very easy to transfer from Flask to FastAPI since they provide almost similar kind of code structure.

First of all this doesn’t mean that you should choose FastAPI over Flask. Since it is very handy at ease to switch from Flask to FastAPI, I personally feel it will be worth spending 20–30 minutes of your time in checking this new framework out.