diff --git a/pyproject.toml b/pyproject.toml index 330684a..f98e375 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,7 @@ name = "agentseed" version = "0.1.0" requires-python = ">=3.12" dependencies = [ + "fastapi>=0.115,<1", "httpx>=0.27,<1", - "python-dotenv>=1,<2", + "uvicorn[standard]>=0.34,<1", ] diff --git a/tutorial/web_basics/README.md b/tutorial/web_basics/README.md new file mode 100644 index 0000000..ba3f555 --- /dev/null +++ b/tutorial/web_basics/README.md @@ -0,0 +1,35 @@ +# FastAPI + HTML 入门示例 + +这个目录是一个独立的教程示例,不依赖主项目中的 `main.py` 和 `static/` 目录。 + +## 启动服务 + +在项目根目录执行: + +```bash +python -m uvicorn tutorial.web_basics.main:app --reload +``` + +如果当前 Python 不支持带连字符的模块路径,可以先进入本目录再启动: + +```bash +cd tutorial/web-basics +python -m uvicorn main:app --reload +``` + +然后打开浏览器访问: + +```text +http://127.0.0.1:8000 +``` + +`index.html` 保持和教程中的代码一致:点击“发送”后,页面内的 JavaScript 会直接将输入内容转换为大写并更新标题。`main.py` 中的 FastAPI 接口用于配合教程后续讲解 HTTP 请求和前后端分离。 + +## 目录说明 + +```text +web_basics/ +├── README.md +├── index.html # 前端页面:HTML、CSS 和 JavaScript +└── main.py # 后端接口:FastAPI 和 POST / +``` diff --git a/tutorial/web_basics/index.html b/tutorial/web_basics/index.html new file mode 100644 index 0000000..7cf5f12 --- /dev/null +++ b/tutorial/web_basics/index.html @@ -0,0 +1,88 @@ + + + + + + Welcome Page + + + + + + +
+ +

Hello World

+ + + + + +
+ + + + diff --git a/tutorial/web_basics/main.py b/tutorial/web_basics/main.py new file mode 100644 index 0000000..3c79d82 --- /dev/null +++ b/tutorial/web_basics/main.py @@ -0,0 +1,31 @@ +from pathlib import Path + +from fastapi import FastAPI +from fastapi.responses import FileResponse +from pydantic import BaseModel + + +BASE_DIR = Path(__file__).parent +app = FastAPI() + + +# 请求体:定义客户端提交的数据结构 +class InputData(BaseModel): + value: str + + +@app.get("/") +def get_page() -> FileResponse: + """返回前端页面。""" + return FileResponse(BASE_DIR / "index.html") + + +@app.post("/") +def process_data(request: InputData) -> dict[str, str]: + """将输入全部大写,然后返回给客户端。""" + result = request.value.strip().upper() + + # 返回结果:将处理后的数据封装成字典,也称为响应体。 + return { + "result": result, + }