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 the 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

Flask

FastAPI

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.

Creator

Release Date


2010

2018

License

Website

Installation

pip install flask

pip install fastapi

Performance

Flask 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 by

Netflix, Airbnb, Reddit, Lyft, etc.

Netflix, Uber, Microsoft, etc. 

Data validation

Flask does not support data validation.

FastAPI supports built-in data validation through Python Pydantic library.

API documentation

Flask does not support documentation of APIs.

FastAPI supports built-in documentation provided by Swagger UI and ReDoc.

Async support

As 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 async keyword in the python function definition.

Testing

It provides built-in testing feature using python unittest framework.

Using Starlette and pytest.

Development costs

Higher

Lower

Flask has to depend on another third party library for dependency injection support

FastAPI 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 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 to 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.

flask-output-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.) in 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 the 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 in code automatically in the browser without reloading the server.

fastapi-8000

Leave a Reply

Your email address will not be published.