如何在Google Colab中运行FastAPI / Uvicorn? 已成功安装FastAPI和Uvicorn 示例应用运行尝试

问题描述

我正在尝试使用FastAPI / Uvicorn在Google Colab上运行“本地”网络应用程序,就像我见过的一些Flask应用程序示例代码一样,但是无法正常工作。有人能做到吗?欣赏它。

已成功安装FastAPI和Uvicorn

!pip install FastAPI -q
!pip install uvicorn -q

示例应用

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

运行尝试

#attempt 1
if __name__ == "__main__":
    uvicorn.run("/content/fastapi_002:app",host="127.0.0.1",port=5000,log_level="info")

#attempt 2
#uvicorn main:app --reload
!uvicorn "/content/fastapi_001.ipynb:app" --reload

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)