2.fastapi基础
-
hellow word
from typing import Optional from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class CityInfo(BaseModel): province: str country: str is_affected: Optional[bool] = None # 与bool的区别是可以不传,默认是null # @app.get('/') # def hello_world(): # return {' hello': 'world '} # # # @app.get('/city/{city}') #city 是路径参数(必填参数),quer_string是查询参数(可选填) # def result(city: str, query_string: Optional[str] = None): # return {'city': city, 'query_string': query_string} # # # 启动命令:uvicorn helloworld : app --reload # reload 模式 # # @app.put( '/city/{city}' ) # def result( city : str, city_info: CityInfo ) : # return {'city': city,'country': city_info.country,'is_affected': city_info.is_affected} #################################################异步模式######################################## @app.get('/') async def hello_world(): return {' hello': 'world '} @app.get('/city/{city}') #city 是路径参数(必填参数),quer_string是查询参数(可选填) async def result(city: str, query_string: Optional[str] = None): return {'city': city, 'query_string': query_string} # 启动命令:uvicorn helloworld : app --reload @app.put( '/city/{city}' ) async def result( city : str, city_info: CityInfo ) : # 也可以是自定义的类型City_info return {'city': city,'country': city_info.country,'is_affected': city_info.is_affected}
-
自带接口说明文档docs和redoc
-
docs 可交互
-
redoc
-