FastAPI.1

一、介绍主要特点

  1. 快速
  2. 高效编码
  3. 更少bug
  4. 智能:编辑器的支持,自动补全功能强大,减少调试时间。
  5. 简单:易于学习和使用
  6. 剪短:代码重复最小化,通过不同参数声明实现丰富的功能。
  7. 简装:生产可用级别的代码,还有自动生成的交互式文档。
  8. 标准化:基于(并完全兼容)API的相关开放标准:OpenAPI (以前被称为 Swagger) 和 JSON Schema
  9. FastAPI 是一个用于构建 API 的现代、快速(高性能)的 web 框架,使用 Python 3.6+ 并基于标准的 Python 类型提示。

二、安装

  1. 安装fastapi:pip install fastapi

  2. 安装部署包:pip install uvicorn

三、简单使用

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"hello": "world"}


@app.get("/item/{item_id}/")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, 'q': q}


'''
用装饰器来添加路由
运行:uvicorn main:app --reload
  main:表示app所在的文件
  app:FastAPI实例
  reload:debug模式,可以自动重启
'''

posted on 2023-04-20 17:19  zhangbinn  阅读(11)  评论(0编辑  收藏  举报