FastAPI 01 Create Demo

FastAPI 01 Create Demo

  • [install package](#install package)
  • [创建 hello_world.py](#创建 hello_world.py)
  • [启动命令 `uvicorn hello_world:app --reload`](#启动命令 uvicorn hello_world:app --reload)
  • 可用的URL

install package

pip install fastapi uvicorn

创建 hello_world.py

py 复制代码
from fastapi import FastAPI

# 创建一个 FastAPI 实例
app = FastAPI()


# 定义一个路由
@app.get("/")
def root():
    return {
        "Hello World": "this is FastAPI",
    }


if __name__ == "__main__":
    import uvicorn
    from pathlib import Path

    this_file = Path(__file__).absolute().resolve()
    this_file_stem = this_file.stem

    uvicorn.run(
        f'{this_file_stem}:app',
        host="127.0.0.1",
        port=8000,
        reload=True,
    )

启动命令 uvicorn hello_world:app --reload

cmd 复制代码
launcher 6229 -- -m uvicorn hello_world:app --reload "
INFO:     Will watch for changes in these directories: ['E:\\workspace\\python\\hugh_fastapi']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [23296] using StatReload
INFO:     Started server process [24388]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

可用的URL

FastAPI 的强大之处在于它默认提供了交互式 API 文档。启动服务后,你可以在浏览器中直接访问以下两个内置 URL:

相关推荐
wangchunyu1145 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
2501_944521596 小时前
Immer 跨端三方库仓颉适配全流程
python·eclipse·django
信誓旦旦的程序猿6 小时前
【Python量化实战宝典 #26】止损与回撤控制手册:移动止损、最大回撤(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
2501_944521596 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github
Patrick在香港7 小时前
同一行 strftime,两台机器跑出两种日期
python·datetime·时区·locale·日期处理
2601_967987377 小时前
用Python+OpenCV+YOLO写个游戏脚本:从自动瞄准到自动采集的保姆级教程
python·opencv·yolo·目标检测·游戏自动化
meiying88387 小时前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化
人工智能导论实践课7 小时前
【GUI桌面开发3】PyCharm配置外部工具
ide·python·pycharm
一千柯橘7 小时前
LangChain 的实时输出处理: stream_events 和 Streaming
python·langchain·agent
贾公子7 小时前
图片 AIGC 检测接入实战——URL 与 Base64 的取舍、体积校验与「你送过去的可能不是那张图」
python·aigc