Skip to main content

Cell helpers

Cell is the immutable authored-cell record. ojs, js, md, and html create the modes used most often from Python.

import observablejs as obs

notebook = obs.Notebook(
obs.md("# Report", key="title"),
obs.ojs("total = rows.length", key="total", display=False),
variables={"rows": [{"x": 1}, {"x": 2}]},
)

Cell

Cell(
source,
mode="ojs",
key=None,
display=True,
raw=False,
id=None,
pinned=False,
output=None,
notebookkit_attrs={},
)
ArgumentContract
sourceCell source string.
modejs, ts, ojs, md, html, tex, dot, sql, node, python, or r.
keyNonempty portable public identity. Serialized as data-pyobservablejs-key.
displayFalse emits the Notebook Kit hidden attribute.
rawTrue preserves indentation and surrounding newlines.
idOptional unique positive Notebook Kit serialization id through JavaScript's safe integer maximum.
pinnedMarks source for display when the notebook enables pinned source.
outputNotebook Kit output metadata.
notebookkit_attrsTyped mode metadata for database, format, or since.

With raw=False, construction dedents the source and strips surrounding newlines. Invalid source or id types raise TypeError. Unsupported modes, empty keys, unsafe ids, and collisions between first-class fields and notebookkit_attrs raise ValueError.

Helper functions

ojs(source, *, key=None, display=True, raw=False, id=None, pinned=False, output=None, notebookkit_attrs=None) -> Cell
js(source, *, key=None, display=True, raw=False, id=None, pinned=False, output=None, notebookkit_attrs=None) -> Cell
md(source, *, key=None, display=True, raw=False, id=None, pinned=False, output=None, notebookkit_attrs=None) -> Cell
html(source, *, key=None, display=True, raw=False, id=None, pinned=False, output=None, notebookkit_attrs=None) -> Cell

The helper fixes the corresponding mode and forwards every other argument to Cell. Observable variable names come from source code. Cell selection uses the public key.