Skip to main content

NotebookView and NotebookCell

Call Notebook.view(...) to create a NotebookView. Notebook.cell and Notebook.cells return stable NotebookCell handles.

Standalone view factories

The standalone factories construct a notebook and return its full NotebookView. 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,
) -> NotebookView

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

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

view_from_observablehq_document(
document,
*,
title=None,
variables=None,
files=None,
show_pinned_source=False,
) -> 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 function has explicit named inputs, so it can be registered directly by tools that serve AnyWidget factories. Validation, file, and network errors come from the matching cell or notebook constructor.

NotebookCell

cell = notebook.cell("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.

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

NotebookView

A view is an anywidget model with one browser run. It evaluates its selected cells and any other cells they need.

view = notebook.view("answer")
view

marimo can wrap it explicitly:

import marimo as mo

mo.ui.anywidget(view)
MemberContract
notebookThe owning Notebook controller.
cellsSelected NotebookCell handles in notebook order.
stateCurrent immutable ViewState browser snapshot.

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")

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.