Files
呵呵です 65e7fed32b ♻️ 重构模板截图部分以解决导航导致的报错 (#553)
* ♻️ 把 path 放到数据模型里

* ♻️ 使用通用函数来生成模板图片

* 🎨 同步模板项目结构

* 🐛 修正导入路径
2025-07-27 22:58:41 +08:00

34 lines
913 B
Python

from jinja2 import Environment, FileSystemLoader
from nonebot.compat import PYDANTIC_V2
from ..host import HostPage, get_self_netloc
from ..screenshot import screenshot
from ..templates import TEMPLATES_DIR
from .schemas.base import Base
env = Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
autoescape=False, # noqa: S701
trim_blocks=True,
lstrip_blocks=True,
enable_async=True,
)
async def render(
data: Base,
) -> str:
if PYDANTIC_V2:
return await env.get_template('index.html').render_async(data=data.model_dump_json(by_alias=True))
return await env.get_template('index.html').render_async(data=data.json(by_alias=True))
async def render_image(
data: Base,
) -> bytes:
async with HostPage(page=await render(data)) as page_hash:
return await screenshot(f'http://{get_self_netloc()}/host/{page_hash}.html#/{data.path}')
__all__ = ['render']