來個簡單的Hello world 例子
import uvicorn from fastapi import FastAPI app = FastAPI() @app.get('/') async def root(): return {"msg": "Hello World"} if __name__ == '__main__': uvicorn.run('test02:app')
第一種:通過uvicorn.run()啟動,其中test02為當前py文件,app為FastAPI的實例對象,這樣啟動默認為http://127.0.0.1:8000,當然也可以更改host和port
第二種:通過命令uvicorn test02:app --reload 其中--reload :可以自動重啟