快速上手¶
下面的程序用显式样式表渲染 HTML,并写出一个 PNG:
from pathlib import Path
from takumi_py import Renderer
renderer = Renderer()
png = renderer.render_html(
'<section class="card"><h1>Hello</h1></section>',
stylesheets=[
"""
.card {
width: 1200px;
height: 630px;
display: flex;
align-items: center;
justify-content: center;
color: white;
background: #111827;
}
h1 { font-size: 72px; }
"""
], # (1)!
width=1200,
height=630, # (2)!
)
Path("out.png").write_bytes(png)
- 样式表作为文档级渲染输入传入,而不是隐藏在渲染器的全局状态中。
- 需要多次调用得到相同结果时,请固定两个视口维度。
选择输入模型¶
- 已有 HTML:使用
render_html,见 HTML 与 CSS。 - 内容是结构化数据:使用
render_node,见 节点树。 - 同一结构会被反复渲染:调用
compile_node或compile_html,再遵循 已编译渲染约定。 - 模板存放在磁盘上:使用
TemplateRenderer,见 Jinja 模板。