来个简单的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 :可以自动重启