Skip to main content

NotebookView and NotebookCell

Call Notebook.view(...) to create a renderable view backed by a NotebookView. Notebook.cells provides stable NotebookCell handles.

Standalone view factories

The standalone factories construct a notebook and return its full renderable view. The view keeps its controller available as view.notebook and owns that notebook session. Calling view.close() closes the view and session.

import observablejs as obs

view = obs.view_from_code("viewof n = Inputs.range([0, 10])")
view_from_code(
code,
*,
mode="ojs",
title="Untitled",
theme="air",
files=None,
base_path=None,
variables=None,
**view_options,
) -> NotebookView

view_from_html(
source,
*,
files=None,
base_path=None,
embed_file_attachments=False,
rewrite_imports=False,
variables=None,
show_pinned_source=False,
**view_options,
) -> NotebookView

view_from_observablehq(
specifier,
*,
variables=None,
files=None,
show_pinned_source=False,
timeout=30,
**view_options,
) -> NotebookView

view_from_observablehq_document(
document,
*,
title=None,
variables=None,
files=None,
show_pinned_source=False,
**view_options,
) -> NotebookView

The imported-source factories accept the same keyword options as their matching Notebook.from_* constructors. view_from_code accepts notebook title, theme, files, base path, and initial variables. The specifier may be a public ObservableHQ URL, slug, id, or document API URL. document accepts an observablejs.types.ObservableDocument mapping.

Each factory accepts the typed Notebook.view() options through **view_options. capture_state matches the option on Notebook.view(). Set it to False when the rendered output is all you need and Python will not read NotebookView.state. The view remains interactive and its state stays at its initial value.

Cell, notebook, file, and network errors come from the matching constructor. Invalid view options raise TypeError before file or network access. See Skip Python state capture for guidance.

NotebookCell

cell = notebook.cells["answer"]
print(cell.key, cell.index, cell.id)
MemberContract
keyPortable public identity, or None for an anonymous cell.
indexZero-based notebook order metadata.
idNotebook Kit serialization metadata.
source, modePrepared cell source and Notebook Kit language.
pinned, hiddenSource visibility and authored output visibility.
output, databaseDeclared output and SQL database, when present.

Use the key or handle as a Notebook.view(...) selector. The id and index do not select cells.

NotebookView

The underlying NotebookView is an anywidget model with one browser run. It evaluates its selected cells and any other cells they need.

view = notebook.view("answer")
view

In a running marimo notebook, Notebook.view() returns a marimo UI element that proxies the underlying NotebookView. Use the returned view directly in cell output and layouts. Other anywidget hosts receive the NotebookView model directly.

MemberContract
notebookThe owning Notebook controller.
cellsSelected NotebookCell handles in notebook order.
stateCurrent immutable ViewState browser snapshot.
data, files, graph, inspection, datasetsExplicit inspection and data access.

await view.ready() checks a settled evaluation after current Python updates. view.raise_for_errors() raises the latest received failure immediately. view.diagnostics carries detailed errors even with preview capture disabled. See Errors and diagnostics for signatures and exception types.

Before a browser mounts the view, state.input_revision and state.settled_revision are None, pending is false, and no results or graph exist. Each accepted browser snapshot replaces state once. Frameworks that listen to traitlets can observe it:

def on_view_state(change):
state = change["new"]
if not state.pending:
print(state.results)

view.observe(on_view_state, names="state")

With capture_state=False, state remains at this initial value. Rendering, Python variable updates, and supported input synchronization continue. Explicit inspection and data reads also remain available.

One NotebookView can have one live writable render at a time. Create another view from the notebook when the same selection must appear in two outputs.

NotebookView.close()

For a view created by Notebook.view(), this closes the view and its browser runtime while the notebook session remains active. A view returned by a standalone factory owns its notebook session, so closing it also closes any other views created from view.notebook. Repeated calls are no-ops. Late browser callbacks cannot publish new state after close.

See View state and graph for the result, error, revision, and dependency records.