Comparison of python’s three major development frameworks: django, flask and fastapi

Original text: https://fastapi.tiangolo.com/alternatives
Author: tiangolo, creator of FastAPI

This article tells the story of what inspired the creation of FastAPI, how it compares to other alternative frameworks, and the lessons learned from it.
FastAPI would not exist if it were not based on the work of those who came before. Many tools have been created before FastAPI.
I’ve been avoiding creating new frameworks for a few years now. First, I tried to use many different frameworks, plugins and tools to solve all the features covered by FastAPI.

But sometimes, there’s no better way than to create something with all these features, taking the best ideas from previous tools and combining them in the best way, using language features that weren’t even available before (Python 3.6+ type hint).

Framework that inspired FastAPI

Django

Django is the most popular Python framework and is widely trusted. It is used to build systems like Instagram.

It is relatively tightly integrated with relational databases (such as MySQL or PostgreSQL), so it is not an easy task to use a NoSQL database (such as Couchbase, MongoDB, Cassandra, etc.) as the main storage engine for Django.

Django REST Framework

Django REST Framework is a very flexible framework for building web APIs to improve Django’s API functionality.

It is used by many companies including Mozilla, Red Hat and Eventbrite.

Django REST Framework is the first framework to automatically generate API documents. Automatically generating API interface documents is one of the reasons why the FastAPI framework was born.

Notice

The author of the Django REST Framework is Tom Christie, who also created Starlette and Uvicorn. FastAPI is built on top of Starlette and Uvicorn.

Inspired by FastAPI: There is an automated API documentation, and the web UI is available for user testing.

Flask

Flask is a lightweight framework that does not include database integration and does not have many of the bells and whistles that Django provides by default.

This simplicity and flexibility allows the use of NoSQL databases as the primary data store. Although the documentation is technical in some aspects, it is very simple and therefore relatively intuitive to learn.

It is also commonly used in other applications that do not require a database, user management, or pre-built functionality in Django. Although many of these features can be achieved by adding plugins.

The previous decoupling of individual modules into a “micro-framework” that could be extended to provide exactly what was needed was a key feature I wanted to retain.

Given Flask’s simplicity, it seems like a good fit for building APIs. The next thing to find is Flask’s “Django REST Framework”.

Inspired by FastAPI: Becoming a microframework. Easy to mix and match the tools and parts you need. Has a simple and easy to use routing system.

Requests

FastAPI is not actually a replacement for Requests. Their scope of application is very different. In fact, it’s very common to use Requests inside FastAPI applications.

However, FastAPI takes a lot of inspiration from Requests. Requests is a library for interacting with APIs (as a client), while FastAPI is a library for building APIs (as a server). They are more or less at opposite ends and complement each other. Requests has a very simple and intuitive design, is very easy to use, and has sensible defaults. But at the same time, it’s very powerful and customizable.

That’s why, as stated on the official website:

Requests is one of the most downloaded Python packages ever

The way you use it is very simple. For example, to make a GET request, you can write:

response = requests.get("http://example.com/some/url")

The API path operations corresponding to FastAPI are as follows:

@app.get("/some/url")
def read_url():
    return {"message": "Hello World"}

Their usage is similar to requests.get(…) and @app.get(…).

Inspired FastAPI places:

Has a simple and intuitive API.
  
Direct and intuitive use of HTTP method names (actions).
  
Featuring sensible defaults and powerful customization.

Swagger/OpenAPI

The main feature I want from Django REST Framework is automatic API documentation. Then I discovered that there is a standard for API documentation called Swagger, which uses JSON or YAML to describe it.

And the web user interface of the Swagger API has been created. Therefore, being able to generate Swagger documentation for the API will allow automatic use of this web user interface.

At some point, Swagger was granted to the Linux Foundation, which renamed it OpenAPI. That’s why it’s common to say “Swagger” when talking about version 2.0, and “OpenAPI” for version 3+.

Inspired FastAPI places:
Adopt open standards for API specifications rather than using custom schemas. and integrates standards-based user interface tools:
Swagger UI
ReDoc

These two were chosen because they are fairly popular and stable, but with a quick search you can find dozens of other alternative user interfaces for OpenAPI that can be used with FastAPI.

Flask REST frameworks

There are several Flask REST frameworks, but after investigation and trial use, I found that many projects have been discontinued or abandoned, and there are still some long-term problems that make them not suitable for solving the previous problems.

Marshmallow

One of the main functions required by API systems is data serialization, which is the conversion of data from objects in a programming language into objects that can be transmitted over the network, such as converting data in a database into JSON objects. Convert datetime objects in Python to strings, etc.

Another function is data validation to ensure that the parameters passed in are valid. For example, some fields are of int type instead of string, which is very useful in detecting input data.

Without data validation, you would have to write code manually to do all the checks.

These two features are what Marshmallow provides, and it’s a great library that I’ve used a lot in the past.

Before Marshmallow came along, Python didn’t have type hints. Therefore, to define a schema you need to import Marshmallow specific utils and classes.

Inspired FastAPI places:
Use code to define the provided data types and the schema for validation, and the validation is automated.

Framework used by FastAPI

Pydantic

Pydantic is a library that defines data validation, serialization and documentation (using JSON schemas) based on Python type hints. This makes it very intuitive. It’s comparable to Marshmallow. Although it is faster than Marshmallow in benchmark tests. And since it’s based on the same Python type hints, the editor support is fantastic.

FastAPI uses it to handle all data validation, data serialization and automatic model documentation (based on JSON Schema).

FastAPI then takes that JSON Schema data and puts it into OpenAPI, among other things.

Starlette

Starlette is a lightweight ASGI framework/toolkit ideal for building high-performance asyncio services.

It’s very simple and intuitive. It is designed to be easily expandable and has modular components.

it has:

  • Impressive performance.

  • WebSocket support.

  • GraphQL support.

  • Background tasks in progress.

  • Start and shut down events.

  • Test requests based clients.

  • CORS, GZip, static files, streaming responses.

  • Session and cookie support.

  • 100% test coverage.

  • 100% type annotated code base.

  • Zero hard dependencies.

Starlette is currently the fastest tested Python framework. Only Uvicorn surpasses it, Uvicorn is not a framework, but a server.

Starlette provides all basic web microframework functionality. However it does not provide automatic data validation, serialization or API documentation.
This is one of the major things FastAPI adds over the top, all based on Python type hints (using Pydantic). As well as a dependency injection system, security utilities, OpenAPI schema generation, and more.

Technical details: ASGI is a new “standard” developed by members of the Django core team. Although they are doing this, it is still not a “Python Standard” (PEP). However, it is already used as a “standard” by several tools. This greatly improves interoperability, as you can switch Uvicorn to any other ASGI server (such as Daphne or Hypercorn), or add ASGI-compatible tools such as python-socketio.

FastAPI uses it to handle all core web parts. Add functionality on top. The class FastAPI itself directly inherits Starlette. Therefore, anything you can do with Starlette, you can do directly with FastAPI.
Uvicorn

Uvicorn is a lightning-fast ASGI server built on uvloop and httptools. It’s not a web framework, it’s a server. For example, it does not provide tools for routing by path. That’s what a framework like Starlette (or FastAPI) can provide. It is a recommended server for Starlette and FastAPI.

FastAPI recommends it as the main web server to run FastAPI applications. You can combine this with Gunicorn to have an asynchronous multi-process server. See more details in the Deployment section.

Digression

In this era of big data, how can you keep up with scripting without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Essential development tools for Python

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

If there is any infringement, please contact us for deletion