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:

相关推荐
XLYcmy7 小时前
PDF 论文处理器 — 改进方向与建议文档
python·pdf·llm·agent·dify·rag·harness
青柠之夏cc7 小时前
前端拖拽功能原生实现,不引入拖拽库完成业务
开发语言·前端·python
曾晓森7 小时前
OpenAI 兼容 API 接入山猫云:先核对分组和价格,再发最小请求
网络·python
OKkankan7 小时前
Python 高阶语法(一):高阶函数、闭包、lambda 与 functools——从底层真正理解装饰器
开发语言·python
智鸟科技GemeOpen开发者智能设备8 小时前
平安校园 AI 智能实时告警系统 智鸟科技·GemeOpen + 谷华科技·融合创新方案白皮书
java·开发语言·python·物联网·智能家居
Sand(ContextGate)8 小时前
Python Agent 测试实战:测试与评估,让 Agent 像传统软件一样可交付
前端·javascript·python·microsoft·ai
空心木偶☜9 小时前
LangChain 概述
python·ai·langchain·ai编程
lie..9 小时前
30天从零开始学AI应用开发(Day 19):项目二完结:RAG 知识库问答系统,把自己攒的资料变成私人顾问
开发语言·人工智能·python
爱写代码的倒霉蛋9 小时前
FastAPI的接口实现流程讲解
fastapi