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

在Quarto中能否引入外部文件或代码片段并渲染为源代码?

Quarto引入文件/代码片段作为源代码的实现方法

引入整个文件

Quarto可以直接把整个文件内容渲染为带语法高亮的代码块,用代码块的include选项就能实现,语法示例:

#| include: src/file.py

系统会自动识别文件扩展名对应的编程语言并启用高亮。如果需要手动指定语言(比如文件无扩展名),可以加上language选项:

#| include: src/my-script
#| language: python

引入指定代码片段

如果只需要文件中的部分代码,有两种常用方式:

  • 指定行范围:通过lines参数提取特定行的内容
#| include: {file: "src/file.py", lines: [5, 20]}
  • 标签标记片段:在源文件中用注释标记片段起始和结束,再在Quarto中引用标签
    比如在src/file.py里标记:
# 无关代码
# %% snippet-hello
def greet(name):
    return f"Hello, {name}!"
# %% snippet-hello
# 其他无关代码

然后在Quarto中引用这个片段:

#| include: {file: "src/file.py", lines: "snippet-hello"}

你提到的{{< include-as-code src/file.py >}}这类短代码语法,Quarto没有内置支持,但官方推荐的上述include代码块选项已经能满足需求,而且更贴合Quarto的原生语法逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 02:06:31