This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

When I first got into Python Web, I used the Flask framework to write interface services. Until last year, I saw the FastAPI framework and tried to use it. I felt quite good and used it smoothly. Formally applied in the production environment, feel no big problem. Flask framework: How to use FastAPI: Flask Framework: How to use FastAPI: Flask Framework: How to use FastAPI: Flask Framework: How to use FastAPI

Introduction to the

FastAPI is a high-performance Web framework for building API services. It currently only supports Python 3.6+ and above. It is built on Pydantic and Starlette. Pydantic is a library that defines data validation, serialization, and documentation (using JSON schema) based on Python type hints; Starlette is a lightweight ASGI framework/toolkit ideal for building high-performance asynchronous services. FastAPI also absorbs the advantages of other Web frameworks and stands on the shoulders of giants, so FastAPI has the advantages of high development efficiency, superior performance, and standardized API development.

Simple to use

You can use the following command to install FastAPI.

pip install fastapi
pip install uvicorn
Copy the code

Uvicorn is a high performance ASGI server built on UvLoop and HttpTools to provide server running support for FastAPI applications, and can also be used in conjunction with Gunicorn to have asynchronous multi-process servers.

Create a new main.py file and write the following code:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def hello() :
    return {"message": "Hello FastAPI!"}


@app.get("/name")
async def hello_name(name: str) :
    return {"Hello": name}
Copy the code

If you want to define an asynchronous method, just add the async keyword in front of the function definition. Then run the following command from the command line to start the service:

uvicorn main:app --reload
Copy the code

–reload means hot restart (hot loading). The server restarts automatically after the code changes. By default, the server runs on port 8000.

Open your browser to http://127.0.0.1:8000 and you will see the following JSON response:

{"message":"Hello FastAPI!"}
Copy the code

In addition, FastAPI comes with API interactive documentation, we can view the interface documentation through the following address.

  • Swagger UI style: http://127.0.0.1:8000/docs

  • ReDoc style: http://127.0.0.1:8000/redoc

Using interactive documents can be convenient for interface debugging, interactive experience is also very friendly, which is definitely a big advantage.

conclusion

If you want to build API services quickly, FastAPI is a good choice. However, FastAPI is far from being “high performance on a par with NodeJS and Go,” as many people have been hyped. It’s a great Python Web framework, marketed well enough to catch on. However, there are still many problems in the project. I hope it will get better and better.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!