FastAPl.png
FastAPI framework, high performance, easy to learn, fast to code, ready for production
FastAPI框架,高性能,易于学习,快速编码,准备投入生产。
FastAPI是一个现代的、快速的(高性能的)web框架,用于基于标准Python类型提示用Python 3.6+构建api。
主要特点如下:
快:非常高的性能,与NodeJS和Go(感谢Starlette和Pydantic)。可用的最快的Python框架之一。
快速编码: 提高开发速度约200%到300% 。
减少bug : 减少约40%的人为(开发人员)错误。
直观: 伟大的编辑器的支持,完成无处不在,更少的时间调试。
容易: 设计是为了便于使用和学习。减少阅读文档的时间。
短: 最小化代码重复。每个参数声明的多个特性。更少的错误。
健壮: 获得生产就绪代码。自动交互文档。
基于标准的: 基于(并完全兼容)api的开放标准:OpenAPI(以前称为Swagger)和JSON模式。
Typer, the FastAPI of CLIs
如果您正在构建一个在终端中使用的CLI应用程序,而不是web API,请查看Typer。
开发环境
Python 3.6+版本
FastAPI 由以下两大“大牛”支持者:
安装
pip install fastapi
您还需要一个ASGI服务器,用于生产,如Uvicorn或Hypercorn。
pip install uvicorn
Demo例子
新建文件main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
️如果你的代码使用 async / await, 使用 async de