FASTAPI vs FLASK

The goal of this blog is to explore the API development in python and try to compare the features on different Parameters.FastAPI and Flask are both such python tools used for creating APIs with their own uniqueness .Django is another famous Web framework widely used in Python Community , but here we are not considering it.Since It is used for monolithic architecture and it is does not support APIs ,however Django-rest is update by Django community for RESTful APIs but still limited use of NoSQL Databases with Django, keeping it out of discussion.
INTRODUCTION
FASTAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is built on top of Starlette, Pydantic, OpenApi and Uvicorn.
- Under the hood, FastAPI uses Pydantic for data validation and Starlette for tooling, making it blazing fast compared to Flask, giving comparable performance to high-speed web APIs in Node or Go.
- Starlette + Uvicorn offers async capability, something that Flask lacks.
- With Pydantic along with type hints, you get a nice editor experience with autocompletion. You also get data validation, serialization and deserialization (for building an API), and automatic documentation (via JSON Schema and OpenAPI).
FLASK
It is a Python micro web framework.Flask is extremely lightweight.It easy to get started with building a basic application to complex back-end APIs.It is used by Netflix, Reddit,and many more.
- Basic foundation API is nicely shaped and coherent.
- Flask documentation is comprehensive, full of examples and well structured. You can even try out some sample application to really get a feel of Flask.
- It is super easy to deploy Flask in production (Flask is 100% WSGI 1.0 compliant”)
GETTING STARTED WITH INSTALLATION
The installation of both the packages are quite similar like any other python package.
FASTAPI
pip install fastapi uvicorn
(Here Uvicorn is ASGI server required, since FASTAPI does not have built-in Server)
FLASK
pip install flask
ASYNC SUPPORT
FLASK
Flask being designed for WSGI servers like Gunicorn doesn’t have native async support.However,Async in flask can be achieved by using threads(concurrency) or multiprocessing (parallelism) or from tools like Celery or RQ.
FASTAPI
The most exciting feature of FastAPI is that it supports asynchronous code out of the box using the async/await Python keywords.It has a native support for python asyncio. Also, it has feature of Background tasks, we can use to define background task to run after returning the response.
PERFORMANCE
FastAPI is really fast! It is not only fast to code, but it also processes requests super fast! In a survey done by TechEmpower benchmark tool to benchmark performance among multiple API frameworks, FastAPI outperformed flask by 300%.
API DOCUMENTATION
FLASK
Unfortunately,Flask does not automatically create API documentation.There are several extensions that handle this like flask-swagger and FLASK RESTX but they require additional setup.
FASTAPI
FastAPI supports OpenAPI along with Swagger and ReDOC by default.
HANDLING TEMPLATES AND STATIC FILES
FLASK
Flask serves up static files from the “static” folder and templates in a “templates” folder.
FASTAPI
We need to explicitly define the “templates” folder. Then for each response, the request context needs to be provided.We need to mount a folder for static files.
TESTING
Both the frameworks has great support for testing.
FLASK
It has built-in support for python unittest framework.
FASTAPI
FastAPI provides a TestClient which is supported by pytest(Python Testing Framework).
DATABASE SUPPORT
Both the frameworks have support for various Databases.
For RDBMS:
Both the frameworks require ORMs like SQLAlchemy to configure with SQL databases like Postgres, MySql ,etc.
For NoSQL:
FLASK:
NoSQL databases are supported through open source libraries or extensions like PyMongo for MongoDB,etc.
FASTAPI:
Fast API supports many NoSQL databases like MongoDb, ElasticSearch, Cassandra, CouchDB.
SERIALISATION and DESERIALIZATION
Python object serialization and deserialization is an important aspect of any non-trivial program. If in Python you save something to a file, if you read a configuration file, or if you respond to an HTTP request, you do object serialization and deserialization.
FLASK
Easiest way to serialize the objects is to use jsonify.
For complex objects, we can use Flask-Marshmallow.
FASTAPI
FastAPI automatically serializes any returned dict.For more complex and structured data, Pydantic is used
HTTP AUTHENTICATION
FLASK
While Flask doesn’t have a native solution, several third-party extensions are available.
FASTAPI
FastAPI natively supports a number of security and authentication tools via the fastapi.security package.
SOME OTHER ASPECTS
- Validations: Both use pydantic for data validations.(However,we need to set up Flask-pydantic with Flask)
- Modularity:Both Frameworks have in-Built support for modularity.
- HTTP Methods,Query Parameters,URL Parameters are used in quite same fashion
- Both Flask and FastAPI provide a number of options for dealing with different configurations for different environments.
CONCLUSION
FastAPI is a fast web framework and supports asynchronous code. In addition, this is coupled with very complete documentation and an integrated validation system that makes it easier to use.
However,Flask is a microframework which can be used for loosely coupled application where developer can add third-party extensions according to their needs.
At present, Flask is tested by various developers and it seems to fulfill their needs.Despite this,having a large set of plugins with Flask is an added advantage over FastAPI now.