Spaces:
Sleeping
Sleeping
Download app.py from VeryOnly123/my-team-docs: direct link, hf CLI and curl.
- Browser
- Download file 990 Bytes
-
https://huggingface.co/spaces/VeryOnly123/my-team-docs/resolve/main/app.py
- Command line
-
hf download hf://spaces/VeryOnly123/my-team-docs/app.py
-
curl -L -o app.py https://huggingface.co/spaces/VeryOnly123/my-team-docs/resolve/main/app.py
990 Bytes
| import os | |
| from fastapi import FastAPI, Form | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import PlainTextResponse | |
| import pathlib | |
| app = FastAPI() | |
| # 允许所有来源跨域访问(本地 HTML 调用时需要) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| DATA_DIR = pathlib.Path("/data") | |
| DATA_DIR.mkdir(exist_ok=True) | |
| async def read_page(page_name: str): | |
| """读取页面纯文本,不存在则返回空字符串""" | |
| file_path = DATA_DIR / f"{page_name}.txt" | |
| if file_path.exists(): | |
| return file_path.read_text(encoding="utf-8") | |
| return "" | |
| async def write_page(page_name: str, t: str = Form(...)): | |
| """写入页面内容,表单参数 t""" | |
| file_path = DATA_DIR / f"{page_name}.txt" | |
| file_path.write_text(t, encoding="utf-8") | |
| return {"ok": True} |