This will install the extension under the _extensions subdirectory.
If you are using version control, you will want to check in this directory.
Usage
Add the filter to your document’s front matter:
filters:- collapse-output
Configuration
extensions:collapse-output:method: luacollapse-all-outputs:falsedefault-open:falseauto-collapse-size:6output-types: stdout, stderr, displaysummary-template:"{type} ({lines} lines)"summaries:stdout:"Console output"stderr:"Warnings and errors"
Examples
Basic Example with Text Output
This example demonstrates collapsing simple text output. The summary-template renders as Standard Output (N lines) because no per-cell output-summary is set.
```{python}#| label: long-outputfor i inrange(1, 11):print(f"Long output line {i}")```
Console output
Long output line 1
Long output line 2
Long output line 3
Long output line 4
Long output line 5
Long output line 6
Long output line 7
Long output line 8
Long output line 9
Long output line 10
Example with Table Output
Display outputs use the Display Output ({lines} lines) label.
```{python}#| label: tableimport polars as pldf = pl.DataFrame({"Group": group,"Weight": weight})df```
Display Output (1 lines)
shape: (20, 2)
Group
Weight
str
f64
"Ctl"
4.17
"Ctl"
5.58
"Ctl"
5.18
"Ctl"
6.11
"Ctl"
4.5
…
…
"Trt"
3.83
"Trt"
6.03
"Trt"
4.89
"Trt"
4.32
"Trt"
4.69
Example with Matplotlib Figure
```{python}#| label: figure-1import matplotlib.pyplot as pltplt.boxplot([ctl, trt], tick_labels=["Ctl", "Trt"])plt```
Figure 1
Example with Plotnine Figure
```{python}#| label: figure-2from plotnine import ggplot, aes, geom_boxplot, labsimport polars as pldf = pl.DataFrame({"Group": group,"Weight": weight})( ggplot(df.to_pandas()) + aes(x="Group", y="Weight") + geom_boxplot() + labs(title="Boxplot of Weight by Group"))```
Figure 2
Use Cases
The collapse output extension is particularly useful for:
Long outputs: Hide lengthy console output, tables, or plots by default.
Tutorial documents: Keep code visible whilst hiding output until needed.
Reports: Show results on demand without cluttering the main document.
Interactive exploration: Allow readers to selectively view outputs of interest.
Notes
Note
The output-fold code cell option only works with HTML output formats.
When using the Lua method (default), the collapse is applied at build time.
When using the JavaScript method, the collapse happens in the browser.
auto-collapse-size forces outputs closed even when default-open (or output-open) is true.
Both output-fold and output-summary are code cell options (use #| prefix).