실행 컨텍스트 정보
개요
Quarto가 문서의 코드 셀을 실행할 때, QUARTO_EXECUTE_INFO 환경 변수를 통해 실행 컨텍스트 정보를 제공합니다. 이 변수에는 렌더링 중인 문서와 사용 중인 포맷에 대한 메타데이터가 담긴 JSON 파일 경로가 저장됩니다.
실행 정보 가져오기
QUARTO_EXECUTE_INFO에 저장된 파일 경로를 읽으면 현재 실행 컨텍스트와 관련된 메타데이터를 확인할 수 있습니다. 필요한 언어에서 JSON 파일을 로드해 사용하세요.
import os
import json
with open(os.environ["QUARTO_EXECUTE_INFO"]) as f:
info = json.load(f)
# 문서 경로
print(info["document-path"])
# 포맷 정보
print(info["format"]["identifier"]["target-format"])library(jsonlite)
info_file <- Sys.getenv("QUARTO_EXECUTE_INFO")
info <- fromJSON(info_file)
# 문서 경로
info$`document-path`
# 포맷 정보
info$format$identifier$`target-format`using JSON
info = JSON.parsefile(ENV["QUARTO_EXECUTE_INFO"])
# 문서 경로
println(info["document-path"])
# 포맷 정보
println(info["format"]["identifier"]["target-format"])JSON 구조
JSON 객체에는 다음과 같은 최상위 필드가 있습니다.
| 필드 | 타입 | 설명 |
|---|---|---|
document-path |
string | 현재 렌더링 중인 원본 문서의 절대 경로 |
format |
object | 실행, 렌더링, Pandoc 설정 등을 아우르는 포맷 구성 정보 |
format 객체
format 객체는 여러 섹션으로 나뉜 상세 설정을 담고 있습니다.
| 필드 | 타입 | 설명 |
|---|---|---|
identifier |
object | display-name, target-format, base-format 등 포맷 식별자 |
execute |
object | fig-width, fig-height, echo, eval, warning 등의 실행 옵션 |
render |
object | keep-tex, code-fold, fig-align, 출력 확장자 등 렌더링 옵션 |
pandoc |
object | standalone, to, default-image-extension 등의 Pandoc 설정 |
language |
object | UI 텍스트, 섹션 제목, Callout 등 지역화 문자열 |
metadata |
object | 문서 메타데이터(제목, 포맷별 옵션 등) |
format.identifier
identifier 객체는 포맷 분류 정보를 제공합니다.
| 필드 | 타입 | 설명 |
|---|---|---|
display-name |
string | 사람이 읽기 쉬운 포맷 이름(예: “Github (GFM)”, “HTML”) |
target-format |
string | 실제 출력될 타깃 포맷 식별자(예: “gfm”, “html”) |
base-format |
string | 타깃이 상속하는 기본 포맷 |
execute 옵션
execute 객체에는 코드 실행 설정이 포함됩니다. 주요 필드는 다음과 같습니다.
| 필드 | 타입 | 설명 |
|---|---|---|
eval |
boolean | 코드 셀을 실행할지 여부 |
echo |
boolean | 출력에 코드를 포함할지 여부 |
output |
boolean | 출력 결과를 포함할지 여부 |
warning |
boolean | 경고 메시지를 포함할지 여부 |
error |
boolean | 오류 메시지를 포함할지 여부 |
include |
boolean | 셀을 출력에 포함할지 여부 |
cache |
boolean | null | 실행 결과를 캐시할지 여부 |
freeze |
boolean | 실행을 freeze할지 여부 |
fig-width |
number | 기본 그림 폭(인치) |
fig-height |
number | 기본 그림 높이(인치) |
fig-format |
string | 기본 그림 형식(예: “png”, “svg”) |
fig-dpi |
number | 기본 그림 DPI |
render 옵션
render 객체에는 렌더링 설정이 포함됩니다.
| 필드 | 타입 | 설명 |
|---|---|---|
keep-tex |
boolean | 중간 TeX 파일을 유지할지 여부 |
keep-typ |
boolean | 중간 Typst 파일을 유지할지 여부 |
keep-source |
boolean | 소스 파일을 유지할지 여부 |
output-ext |
string | 출력 파일 확장자(예: “md”, “html”) |
fig-align |
string | 기본 그림 정렬 |
pandoc 옵션
pandoc 객체에는 다음과 같은 설정이 포함됩니다.
| 필드 | 타입 | 설명 |
|---|---|---|
to |
string | Pandoc 출력 포맷 |
standalone |
boolean | 독립 실행형 출력을 생성할지 여부 |
default-image-extension |
string | 이미지 기본 확장자 |
활용 예
포맷에 따른 조건부 실행
출력 포맷에 따라 코드 동작을 변경할 수 있습니다.
import os
import json
with open(os.environ["QUARTO_EXECUTE_INFO"]) as f:
info = json.load(f)
base_format = info["format"]["identifier"]["base-format"]
if base_format == "html":
# 대화형 시각화 사용
import plotly.express as px
fig = px.scatter(df, x="x", y="y")
fig.show()
else:
# 정적 그래프 사용
import matplotlib.pyplot as plt
plt.scatter(df["x"], df["y"])
plt.show()library(jsonlite)
info <- fromJSON(Sys.getenv("QUARTO_EXECUTE_INFO"))
target_format <- info$format$identifier$`target-format`
if (target_format == "html") {
# 대화형 시각화
library(plotly)
plot_ly(df, x = ~x, y = ~y, type = "scatter")
} else if (target_format %in% c("pdf", "latex")) {
# 정적 그래프
plot(df$x, df$y)
}포맷 인지형 리소스 경로
문서 경로를 기반으로 리소스 상대 경로를 만들 수 있습니다.
import os
import json
from pathlib import Path
with open(os.environ["QUARTO_EXECUTE_INFO"]) as f:
info = json.load(f)
doc_path = Path(info["document-path"])
doc_dir = doc_path.parent
# 문서 기준 경로에서 데이터 불러오기
data_file = doc_dir / "data" / "input.csv"그림 설정에 따른 출력 조정
실행 설정을 읽어 시각화 매개변수를 조정할 수 있습니다.
import os
import json
import matplotlib.pyplot as plt
with open(os.environ["QUARTO_EXECUTE_INFO"]) as f:
info = json.load(f)
fig_width = info["format"]["execute"]["fig-width"]
fig_height = info["format"]["execute"]["fig-height"]
fig_dpi = info["format"]["execute"]["fig-dpi"]
plt.figure(figsize=(fig_width, fig_height), dpi=fig_dpi)
plt.plot(x, y)
plt.show()