涉及的文件很多,但主要是 PyCharm 自动重构 + 手动微调。 1. 使用 src-layout 结构组织项目,旨在更加规范地管理各个子包,并方便组织代码。 2. 可以使用 uv build 构建项目至 wheel。 3. 可以在 /webui 目录下使用 pnpm run build 来构建 WebUI,构建产物会生成在 /src/njupt_suan_api/static 目录下。同时,uv build 时会一同包含此目录下的 WebUI 构建产物,避免在用户侧执行构建。 4. 使用 typer 实现了命令行入口,代码位于 manage.py;命令行实现了现 main.py 的全部功能。
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""使用 Playwright 截图"""
|
|
|
|
from json import dumps
|
|
from pathlib import Path
|
|
from urllib.parse import urlencode
|
|
from uuid import uuid4
|
|
|
|
from playwright.async_api import ViewportSize
|
|
|
|
from njupt_suan_api.api.baselib import PlayContextManager, logger
|
|
|
|
TEMP_DIR = Path.cwd() / "temp"
|
|
|
|
|
|
class ScreenShot(PlayContextManager):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
|
|
async def goto(self, url: str) -> bool:
|
|
await self.page.set_viewport_size(ViewportSize(width=1200, height=900))
|
|
res = await self.page.goto(url)
|
|
logger.debug(f"截图 | {res.ok=} - {url=:.{50}}...")
|
|
return res.ok
|
|
|
|
async def shot(self, save_path: str) -> None:
|
|
await self.page.mouse.move(0, 0)
|
|
try:
|
|
await self.page.wait_for_load_state(timeout=10000)
|
|
except Exception:
|
|
pass
|
|
finally:
|
|
await self.page.screenshot(path=save_path)
|
|
logger.debug(f"截图 | 截图已经保存在 {save_path=}")
|
|
return
|
|
|
|
|
|
async def generate_img(courses: list[dict], title: str, subtitle: str) -> str:
|
|
"""
|
|
方法将生成课程表图片并保存在临时目录中,返回图片的完整名称。图片位于 temp 工作目录。
|
|
|
|
Returns:
|
|
字符串,表明生成图片的文件名,格式为 `schedule-{uuid4()}.png`
|
|
"""
|
|
t_name = f"schedule-{uuid4()}.png"
|
|
async with ScreenShot() as ss:
|
|
await ss.goto(
|
|
f"127.0.0.1:8000/webui/schedule#/?{
|
|
urlencode({'data': dumps(courses), 'title': title, 'subtitle': subtitle})
|
|
}",
|
|
)
|
|
await ss.shot(str(TEMP_DIR / t_name))
|
|
logger.debug(f"截图 | 生成临时图片 - {t_name}")
|
|
return t_name
|