Select cells
Call Notebook.view(...) to render all cells or a keyed selection.
| Selection | Call |
|---|---|
| Whole notebook | notebook.view() |
| One keyed cell | notebook.view("chart") |
| Several keyed cells | notebook.view("chart", "summary") |
Cells needed by the selection also run, but their output stays hidden. Selected cells render in notebook order, regardless of selector order.
import marimo as mo
import observablejs as obs
numbers = obs.js(
"const numbers = [2, 4, 8, 16];",
key="numbers",
display=False,
)
chart = obs.js(
"""
Plot.barY(numbers, {
x: (_, index) => index + 1,
y: (value) => value * scale,
tip: {
// This example references a theme variable used by mdx-marimo.
// Replace it with a CSS color or remove `fill` in another host.
fill: "var(--marimo-island-background)"
}
}).plot({height: 220, x: {label: "Position"}, y: {grid: true}})
""",
key="chart",
)
summary = obs.js(
"""
html`<p>
${numbers.length} values,
ending at <strong>${numbers.at(-1) * scale}</strong>.
</p>`
""",
key="summary",
)
notebook = obs.Notebook(numbers, chart, summary, variables={"scale": 2})
selected = notebook.view(summary, "chart")
mo.ui.anywidget(selected)
Although summary was passed first, the chart renders first because it appears
earlier in the notebook. numbers evaluates once as a hidden dependency.
Selector contract
view(*selectors) accepts key strings, keyed authored Cell objects, and
NotebookCell handles from the same notebook.
notebook.view("chart")
notebook.view(chart)
notebook.view(notebook.cell("chart"))
notebook.view(chart, notebook.cell("summary"))
Unknown keys raise KeyError. Each authored Cell selector needs a key, and
each NotebookCell selector must belong to the notebook. Duplicate selections
and foreign handles raise ValueError. Invalid selector types raise
TypeError.
NotebookCell.key is public identity. NotebookCell.id is Notebook Kit
serialization metadata, and NotebookCell.index is notebook-order metadata.
Anonymous cells remain available through notebook.cells.
One composite view or separate views
A combined view evaluates selected outputs together and reports one state snapshot. Separate views evaluate independently but receive the same notebook values. Use a combined view for outputs that belong together and separate views for different locations.
See Split one notebook into several views for a complete layout.