Collapse Output

Quarto Extension

Installation

quarto add mcanouil/quarto-collapse-output@1.5.0

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: lua
    collapse-all-outputs: false
    default-open: false
    auto-collapse-size: 6
    output-types: stdout, stderr, display
    summary-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: code

group = ["Ctl"] * 10 + ["Trt"] * 10
ctl = [4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14]
trt = [4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69]
weight = ctl + trt

mean_ctl = sum(ctl) / len(ctl)
mean_trt = sum(trt) / len(trt)
diff = mean_trt - mean_ctl

print(f"Mean (Ctl): {mean_ctl:.2f}")
print(f"Mean (Trt): {mean_trt:.2f}")
print(f"Difference: {diff:.2f}")
```
Console output
Mean (Ctl): 5.03
Mean (Trt): 4.66
Difference: -0.37

Cell-Level Summary Override

The explicit output-summary wins over the document template.

```{python}
#| label: explicit-summary

print(f"Range (Ctl): {min(ctl):.2f} to {max(ctl):.2f}")
print(f"Range (Trt): {min(trt):.2f} to {max(trt):.2f}")
```
Click to view summary statistics
Range (Ctl): 4.17 to 6.11
Range (Trt): 3.59 to 6.03

Cell Starting Open

This cell uses output-open: true to opt out of the closed default for one cell only.

```{python}
#| label: open-cell

print("This output starts expanded.")
```
Console output
This output starts expanded.

Auto-Collapse Threshold

This cell is shorter than auto-collapse-size: 6 lines, so it would honour default-open. A longer output (below) is forced closed by the threshold.

```{python}
#| label: short-output

print("Line 1")
print("Line 2")
```
Console output
Line 1
Line 2
```{python}
#| label: long-output

for i in range(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: table

import polars as pl

df = 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-1

import matplotlib.pyplot as plt

plt.boxplot([ctl, trt], tick_labels=["Ctl", "Trt"])
plt
```
Figure 1

Example with Plotnine Figure

```{python}
#| label: figure-2

from plotnine import ggplot, aes, geom_boxplot, labs
import polars as pl

df = 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).