Python Web Framework: Flask vs FastAPI

Flask and FastAPI are lightweight frameworks for developing a small-scale web application using Python. Since the FastAPI framework is younger than Flask in creation date, its popularity is increasing among developers, especially in data science projects that require showing the results to stakeholders on the go.

On the other hand, the flask is used for handling micro-services workloads with a minimal amount of code. Here in this blog post, I have listed the points that show the difference between these frameworks(Flask vs FastAPI). I hope you select the best-suited framework by going through the below points.

Comparison: Flask Vs FastAPI

VSFlaskFastAPI
Introduction
Flask
is a micro web framework written in Python to support the deployment of web applications with a minimal amount of code.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python-type hints.
CreatorArmin Ronacher
Sebastián Ramírez
Release Date20102018
LicenseBSDMIT
Websitehttps://flask.palletsprojects.comhttps://fastapi.tiangolo.com/
Installationpip install flaskpip install fastapi
PerformanceFlask is slower than FastAPI.FastAPI is well known to be the fastest Python web framework that performs better than Flask in any given situation.
Used byNetflix, Airbnb, Reddit, Lyft, etc.Netflix, Uber, Microsoft, etc. 
Data validationFlask does not support data validation.FastAPI supports built-in data validation through the Python Pydantic library.
API DocumentationFlask does not support documentation of APIs.FastAPI supports built-in documentation provided by Swagger UI and ReDoc.
Async supportAs Flask is developed for WSGI services like Gunicorn, it does not offer native async support.As FastAPI is built on using ASGI(Starlette), it supports asynchronous tasks by pre-appending the async keyword in the python function definition.
TestingIt provides built-in testing features using the python unittest framework.Using Starlette and pytest.
Development costsHigherLower
Dependency injectionFlask has to depend on another third-party library for dependency injection supportFastAPI includes an extremely easy-to-use, but extremely powerful Dependency Injection system.

Examples

A simple example that shows how easy is to implement HTTP GET requests using the Python web framework Flask and FastAPI.

Flask

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home_page():
    return "Hello I'm FastAPI in Flask vs FastAPI"

Above we create a simple GET request that directs to the home page of the website as “/”. By default, a route only answers GET requests. To handle different HTTP methods, use the method’s argument of the route() decorator as:

@app.route('/login', methods=['GET', 'POST'])
...

To run the server, execute the script and open port 5000 in the browser(http://localhost:5000/) as Flask by default serve through port 5000.

FastAPI

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home_page():
    return "Hello I'm FastAPI in Flask vs FastAPI"

FastAPI allows us to specify the route HTTP methods(GET, PUT, DELETE, etc.) at the time of declaring the decorator as above.

To execute these codes,  make sure you have installed uvicorn that allows running the FastAPI server as:

pip install uvicorn[standard]

Then in the terminal run the server as below and check out port 8000 in the browser(http://localhost:8000/).

uvicorn <python_file_name>:app --reload

–reload option allows us to see the changes whenever we make code automatically in the browser without reloading the server.

Leave a Comment