Introduction
Recently, due to the growing need for microservice development, I mainly code Python – Backend, so I was allowed to choose a framework to develop new projects for the company, after considering between 3 popular frameworks. At using Python as Django, Flask and FastAPI, I decided to choose FastAPI to develop the project, here is the reason for this.
Main Reason
FastAPI is a fairly new micro framework, just released in 2018. The Github framework of this framework is currently reaching 25.4k stars as of January 2021, but due to the application of many new technologies, FastAPI has a few points. Strong, but I feel quite suitable for use in project development:
- High performance Due to being based on two fairly strong libs at the moment of python, Pydantic and Starlette, FastAPI has the highest performance out of all current Python frameworks, you can refer to the performance comparison between frameworks. at https://www.techempower.com/
- Development Speed With built-in Swagger interface – OpenAPI comes with a fairly simple code, so programmers can release the function very quickly and still have full documentation, this is the most important advantage of FastAPI compared to with other frameworks. Below is a code that prints the healthcheck text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import uvicorn from fastapi import FastAPI from pydantic import BaseModel class ResponseSchemaBase(BaseModel): code: str = '00' message: str = '' app = FastAPI() @app.post("/health-check", response_model=ResponseSchemaBase) async def health_check(): return {"message": "Health check success"} if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000) |
- Asynchronous Currently asynchronous has been supported since Django 3.x but since release, FastAPI has supported Async developer by default, so FastAPI can only be used with python3.6 and above.
Compare Django, Flask and FastAPI
Below is a comparison table between the 3 Frameworks Django, Flask and FastAPI based on my own experience in experiencing all 3 frameworks.
Django | Flask | FastAPI | |
---|---|---|---|
Community | Django’s community is currently quite large and long-standing, so the number of lib support for Django is arguably the most at the moment. | The current Flask community is also large but not as large as Django, so the number of libs is also limited than Django | FastAPI’s community is relatively new, so there are few libs that support it |
Python version | All version: 2x, 3x | All version: 2x, 3x | Only python3.x |
Performance | Due to support for many features, Django’s perfomance is not high | Performance is quite high | The highest performing framework in current python frameworks |
ORM | Django ORM | Flask-sqlachemy | Sqlachemy |
Async | Support from Django 3.x | Not yet supported | Completely compatible |
Builtin Admin UI | Have | Are not | Are not |
Project Frame | Yes, just run python manage startapp yourapp | Are not | Are not |
Python shell | Have | Have | No – this is a limitation of a microframework compared to real frameworks |
OpenAPI document | The most common is to use Django-rest-framework, with Swagger use django-rest-swagger with python2.x, use drf-yasg with python3.x | Use Flask-restplus | Swagger and Redocs are included by default |