You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarto Python批量渲染问题:生成报告图片重复(文件被覆盖)

Quarto批量渲染图表重复的解决方法

问题说明

使用Python脚本批量渲染Quarto模板时,传递不同phase参数生成多个HTML报告,但所有报告显示的图表完全一致。原因是所有渲染任务默认将图片输出到同一目录individual_reference_template_files/figure-html/下,相同文件名的图片被后续渲染任务覆盖。

解决方案

方案1:为每个渲染任务指定独立输出目录

修改批量渲染脚本,通过--output-dir参数为每个报告分配专属的资源文件目录,避免文件冲突。

修改后的Python脚本:

import os

PHASE = [0, 0.1, 0.2]
for f in PHASE:
    output_dir = f"phase_{f}_resources"
    os.system(
        f"quarto render individual_reference_template_copy.qmd --output phase_{f}.html -P phase:{f} --to html --no-cache --output-dir {output_dir}"
    )

方案2:在模板中为图片添加参数化文件名

在Quarto模板的绘图代码块中,通过fig.path选项将参数变量融入图片文件名,确保每个参数生成的图片文件名唯一。

修改模板中的绘图代码块:

#| fig.path: figures/phase_{phase}_
x = np.arange(0,2*np.pi, 0.001)
y = np.sin(x+phase)
plt.scatter(x, y)

生成的图片会以figures/phase_0_cell-5-output-2.png、figures/phase_0.1_cell-5-output-2.png等命名,不会互相覆盖。

方案3:使用Quarto Python API渲染(推荐)

使用官方的quarto Python包替代os.system,更稳定且易于维护,同时支持直接指定输出目录和参数。

  1. 先安装依赖:
pip install quarto
  1. 修改渲染脚本:
from quarto import render

PHASE = [0, 0.1, 0.2]
for f in PHASE:
    render(
        input="individual_reference_template_copy.qmd",
        output_file=f"phase_{f}.html",
        params={"phase": f},
        output_dir=f"phase_{f}_resources",
        to="html",
        no_cache=True
    )

内容的提问来源于stack exchange,提问作者Kresten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:55:20