-
Notifications
You must be signed in to change notification settings - Fork 1k
docs: document conditional execution via QUARTO_EXECUTE_INFO #2043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed125c4
f0d74f6
e7f4fb5
38822bc
fd2ad7d
c509ec8
5ec4fcf
97093d8
76858f4
f05ca19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The `.content-visible` and `.content-hidden` classes control whether content is *shown* in the rendered output. They do **not** prevent the code in cells they wrap from executing. If you need to skip execution entirely based on the output format — for example, to avoid loading a package or calling a service that only makes sense for one format — have the cell itself read the `QUARTO_EXECUTE_INFO` environment variable. See [Execution Context Information](/docs/advanced/quarto-execute-info.qmd) for the JSON schema and examples in R, Python, and Julia. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -498,3 +498,11 @@ echo "foo" | |
| ```` | ||
|
|
||
| Note that the Knitr engine also supports ```` ```{python} ```` cells, enabling the combination of R, Python, and Bash in the same document | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing this edit...needs |
||
| ## Conditional Execution | ||
|
|
||
| In some cases you want code execution itself — not just visibility of its output — to depend on the active output format. For example, when rendering to PDF you may want to skip a cell that loads an interactive HTML widget, rather than execute it and hide the result. | ||
|
|
||
| Quarto exposes the current rendering context in the `QUARTO_EXECUTE_INFO` environment variable. Cells can read this variable to detect the active format and branch accordingly. See [Execution Context Information](/docs/advanced/quarto-execute-info.qmd) for the full JSON schema and examples in R, Python, and Julia. | ||
|
|
||
| This complements format-based [conditional content](/docs/authoring/conditional.qmd), which controls visibility of *already-executed* output. `QUARTO_EXECUTE_INFO` lets you skip work, choose a different code path, or load a different library before any output is produced. | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -543,6 +543,25 @@ Please direct questions and requests regarding this functionality to the | |||||||
| [QuartoNotebookRunner](https://github.com/PumasAI/QuartoNotebookRunner.jl) | ||||||||
| repository. | ||||||||
|
|
||||||||
| ## Conditional Execution | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ends up nested under "Using the |
||||||||
|
|
||||||||
| To execute a cell only for certain output formats, read the [`QUARTO_EXECUTE_INFO`](/docs/advanced/quarto-execute-info.qmd) environment variable inside the cell: | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This applies to all the E.g.
Suggested change
|
||||||||
|
|
||||||||
| ```` markdown | ||||||||
| ```{{julia}} | ||||||||
| using JSON | ||||||||
|
|
||||||||
| info = JSON.parsefile(ENV["QUARTO_EXECUTE_INFO"]) | ||||||||
| is_html = info["format"]["identifier"]["base-format"] == "html" | ||||||||
|
|
||||||||
| if is_html | ||||||||
| # HTML-only code, e.g. an interactive widget | ||||||||
| end | ||||||||
| ``` | ||||||||
| ```` | ||||||||
|
|
||||||||
| This pairs well with [conditional content](/docs/authoring/conditional.qmd) when both visibility and execution should depend on format. See [Execution Context Information](/docs/advanced/quarto-execute-info.qmd) for the full JSON schema. | ||||||||
|
|
||||||||
| # Using the `jupyter` engine | ||||||||
|
|
||||||||
| ### Installation {#installation} | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.