FastApi是什么
顾名思义,FastApi就是一个用于构建高性能api的web框架。
FastApi的特点
- 快速:比肩NodeJs和Go
- 高效:开发效率提升一倍多
- 少BUG:减少开发错误率
- 智能:自动补全
- 简单:易于学习
- 简短:代码简小精悍
- 健壮:生产级别可用
- 文档:自动生成交互式文档
- 标准化:基于OpenApi
FastApi的安装
pip install fastapi[all]
FastApi之hello world
main.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
async def root():
return {'message':'hello world!'}
命令行启动:
uvicorn.exe main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [18784] using statreload
INFO: Started server process [23504]
INFO: Waiting for application startup.
INFO: Application startup complete.
打开http://127.0.0.1:8000查看效果
可以看到,后台程序已经成功返回。
主函数启动
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get('/')
async def root():
return {'message':'hello world!'}
if __name__ == "__main__":
uvicorn.run(app='main:app',host='127.0.0.1',port=8765,reload=True,debug=True)
启动:
python main.py
交互式的API文档
docs
浏览器访问:127.0.0.1:8765/docs
展开看下接口详细信息
点击右侧的Try it
即可实现接口调试!
完美!
redoc
浏览器访问:127.0.0.1:8765/redoc
至此,FastApi的简单介绍结束。
完