moonrockz/notebooks/nbformat
Types
Attachments
type Attachments derive(Eq, @moonbitlang/core/debug.Debug)An immutable mapping from attachment filenames to validated MIME bundles. Traversal and names use lexical filename order, independent of input key order.
Attachments::Attachments
fn Attachments::Attachments(value : Json) -> Attachments raise FormatErrorValidate a filename-keyed object of MIME bundles, copying all JSON inputs. Raise InvalidField for a non-object or invalid bundle payload.
Attachments::equal
fn Attachments::equal(Attachments, Attachments) -> BoolAttachments::get
fn Attachments::get(self : Attachments, name : String) -> MimeBundle?Find an immutable MIME bundle by its exact filename, or return None.
Attachments::names
fn Attachments::names(self : Attachments) -> ReadOnlyArray[String]Return attachment filenames in lexical order.
Attachments::not_equal
fn Attachments::not_equal(x : Attachments, y : Attachments) -> BoolAttachments::to_json
fn Attachments::to_json(self : Attachments) -> JsonExport an independent filename-keyed JSON object of MIME bundles.
Attachments::to_repr
fn Attachments::to_repr(Attachments) -> @moonbitlang/core/debug.ReprCell
type Cell derive(Eq, @moonbitlang/core/debug.Debug)An immutable validated Jupyter cell. IDs may be absent in pre-4.5 files; the containing notebook enforces version-specific ID presence and uniqueness.
Cell::Cell
fn Cell::Cell(content : CellContent, id? : CellId?, metadata? : @moonrockz/notebooks.Metadata) -> Cell raise FormatErrorValidate a typed payload and cell metadata. Defaults to no ID and empty metadata; Notebook construction requires IDs for v4.5 and later. Unknown fields must not repeat cell_type, id or metadata. Raises InvalidField for invalid metadata or a conflicting Unknown payload.
Cell::cell_type
fn Cell::cell_type(self : Cell) -> StringReturn the wire-format cell type, retaining unknown type names unchanged.
Cell::content
fn Cell::content(self : Cell) -> CellContentReturn the typed cell payload for exhaustive pattern matching.
Cell::equal
fn Cell::equal(Cell, Cell) -> BoolCell::extensions
fn Cell::extensions(self : Cell) -> JsonObjectReturn extra fields preserved when reading a recognized cell. Additional fields of an unknown cell are stored in its Unknown payload instead.
Cell::has_mime_type
fn Cell::has_mime_type(self : Cell, mime_type : String) -> BoolWhether a rich output or attachment contains this exact MIME key. Source text and metadata are not searched. Empty payloads and JSON null count as present; matching follows MimeBundle::get.
Cell::id
fn Cell::id(self : Cell) -> CellId?Return the validated ID, or None when reading or constructing an older cell.
Cell::kind
fn Cell::kind(self : Cell) -> @moonrockz/notebooks.CellKindClassify this cell without discarding its format-specific payload.
Cell::metadata
fn Cell::metadata(self : Cell) -> @moonrockz/notebooks.MetadataReturn the immutable cell metadata object.
Cell::mime_capability
fn Cell::mime_capability(self : Cell) -> &@moonrockz/notebooks.MimeTyped?Cell::not_equal
fn Cell::not_equal(x : Cell, y : Cell) -> BoolCell::outputs
fn Cell::outputs(self : Cell) -> ReadOnlyArray[Output]Return code outputs in stored order. Return an empty immutable array for Markdown, raw and unknown cells.
Cell::source
fn Cell::source(self : Cell) -> String?Return normalized source for code, Markdown and raw cells. Return None for unknown cells, whose uninterpreted source may remain in their fields.
Cell::to_json
fn Cell::to_json(self : Cell) -> Json raise FormatErrorExport an independent cell object, including its discriminator, optional ID, metadata, typed content and preserved fields. Text is normalized.
Cell::to_repr
fn Cell::to_repr(Cell) -> @moonbitlang/core/debug.Reprimpl CellView for Cell
CellView::kind
fn kind(self : Cell) -> @moonrockz/notebooks.CellKindCellView::metadata
fn metadata(self : Cell) -> @moonrockz/notebooks.Metadata?CellView::mime_capability
fn mime_capability(self : Cell) -> &@moonrockz/notebooks.MimeTyped?Every Jupyter cell supports MIME lookup. Cells without rich output or attachments still return Some, and their lookup predicate returns false.
CellView::source
fn source(self : Cell) -> String?impl MimeTyped for Cell
MimeTyped::has_mime_type
fn has_mime_type(self : Cell, mime_type : String) -> BoolCellContent
pub(all) enum CellContent {
Markdown(source~ : String, attachments~ : Attachments?)
Raw(source~ : String, attachments~ : Attachments?)
Code(source~ : String, execution_count~ : ExecutionCount?, outputs~ : ReadOnlyArray[Output])
Unknown(cell_type~ : String, fields~ : JsonObject)
} derive(Eq, @moonbitlang/core/debug.Debug)The source and format-specific payload of a cell. Only code cells carry outputs and execution counts; only Markdown and raw cells carry attachments. Unknown preserves the original cell type and its additional JSON fields.
CellContent::equal
fn CellContent::equal(CellContent, CellContent) -> BoolCellContent::not_equal
fn CellContent::not_equal(x : CellContent, y : CellContent) -> BoolCellContent::to_repr
fn CellContent::to_repr(CellContent) -> @moonbitlang/core/debug.ReprCellId
type CellId derive(Eq, @moonbitlang/core/debug.Debug)An immutable Jupyter cell identifier containing 1 to 64 ASCII letters, digits, hyphens or underscores. Notebook construction checks uniqueness.
CellId::CellId
fn CellId::CellId(value : String) -> CellId raise FormatErrorValidate a cell identifier. Raise InvalidField for an empty, oversized or otherwise invalid ID. This constructor does not generate an ID.
test "doc: construct a stable cell ID" {
let id = @nbformat.CellId("results-01")
@expect.expect(id.to_string()).to_equal("results-01")
}
CellId::equal
fn CellId::equal(CellId, CellId) -> BoolCellId::not_equal
fn CellId::not_equal(x : CellId, y : CellId) -> BoolCellId::to_repr
fn CellId::to_repr(CellId) -> @moonbitlang/core/debug.ReprCellId::to_string
fn CellId::to_string(self : CellId) -> StringReturn the validated identifier unchanged.
ExecutionCount
type ExecutionCount derive(Eq, @moonbitlang/core/debug.Debug)A nonnegative integral JSON number. Preserve its available JSON representation instead of converting large counts to a machine integer. Use None, rather than an ExecutionCount value, for an unexecuted cell.
ExecutionCount::ExecutionCount
fn ExecutionCount::ExecutionCount(value : Int) -> ExecutionCount raise FormatErrorConstruct a count from a nonnegative 32-bit integer. Raise InvalidField for a negative value. Use from_json for larger JSON numbers.
ExecutionCount::equal
fn ExecutionCount::equal(ExecutionCount, ExecutionCount) -> BoolExecutionCount::from_json
fn ExecutionCount::from_json(value : Json) -> ExecutionCount raise FormatErrorValidate a finite, nonnegative integral JSON number and retain its representation. Raise InvalidField for null, negative or fractional values, non-numbers and non-finite values. Null counts are represented by None.
ExecutionCount::not_equal
fn ExecutionCount::not_equal(x : ExecutionCount, y : ExecutionCount) -> BoolExecutionCount::to_json
fn ExecutionCount::to_json(self : ExecutionCount) -> JsonExport the numeric JSON value without converting it to an integer string.
ExecutionCount::to_repr
fn ExecutionCount::to_repr(ExecutionCount) -> @moonbitlang/core/debug.ReprJsonObject
type JsonObject derive(Eq, @moonbitlang/core/debug.Debug)An immutable JSON object. Construction and JSON exports copy the entire value graph, including nested arrays and objects. Accessors never expose mutable aliases to the stored object.
JsonObject::JsonObject
fn JsonObject::JsonObject(value : Json) -> JsonObject raise FormatErrorRequire an object and make a defensive deep copy. Raise InvalidField for any non-object value. Notebook and cell constructors separately validate the meaning of known metadata fields.
JsonObject::equal
fn JsonObject::equal(JsonObject, JsonObject) -> BoolJsonObject::get
fn JsonObject::get(self : JsonObject, key : String) -> Json?Look up a field, returning a defensive deep copy of its JSON value. Return None when the field is absent.
JsonObject::keys
fn JsonObject::keys(self : JsonObject) -> ReadOnlyArray[String]Return immutable field names. Their order is not a sorting guarantee.
JsonObject::not_equal
fn JsonObject::not_equal(x : JsonObject, y : JsonObject) -> BoolJsonObject::to_json
fn JsonObject::to_json(self : JsonObject) -> JsonExport a defensive deep copy that callers may mutate independently.
JsonObject::to_repr
fn JsonObject::to_repr(JsonObject) -> @moonbitlang/core/debug.ReprMimeBundle
type MimeBundle derive(Eq, @moonbitlang/core/debug.Debug)An immutable mapping of MIME types to data. Textual data is normalized to strings; application/json and +json values retain their JSON structure.
MimeBundle::MimeBundle
fn MimeBundle::MimeBundle(value : Json) -> MimeBundle raise FormatErrorRequire a MIME-keyed object. Join string arrays without separators for textual MIME values, while retaining JSON MIME payloads as-is. Raise InvalidField for a non-object or invalid textual payload. Copy all inputs.
MimeBundle::equal
fn MimeBundle::equal(MimeBundle, MimeBundle) -> BoolMimeBundle::get
fn MimeBundle::get(self : MimeBundle, mime_type : String) -> Json?Look up a MIME representation and return an independent JSON value. Textual representations are strings; JSON MIME values retain their type.
MimeBundle::mime_types
fn MimeBundle::mime_types(self : MimeBundle) -> ReadOnlyArray[String]Return the immutable MIME type keys, without a sorting guarantee.
MimeBundle::not_equal
fn MimeBundle::not_equal(x : MimeBundle, y : MimeBundle) -> BoolMimeBundle::to_json
fn MimeBundle::to_json(self : MimeBundle) -> JsonExport an independent MIME-keyed object with normalized textual values.
MimeBundle::to_repr
fn MimeBundle::to_repr(MimeBundle) -> @moonbitlang/core/debug.ReprNode
pub(all) enum Node {
Notebook(Notebook)
Cell(Cell)
Output(Output)
Attachment(name~ : String, data~ : MimeBundle)
} derive(Eq, @moonbitlang/core/debug.Debug)A concrete Jupyter traversal node. Notebooks contain cells; code cells contain outputs; Markdown and raw cells contain attachments. Output and attachment nodes are leaves. Metadata and MIME JSON use their own accessors.
Node::at
fn Node::at(self : Node, path : @moonrockz/notebooks.NodePath) -> Node?Resolve a shared child-index path relative to this node. Return None for any invalid index. The path does not refer to JSON object fields.
Node::children
fn Node::children(self : Node) -> ReadOnlyArray[Node]Return immutable children. Cells and outputs retain stored order; attachments use lexical filename order. Metadata is not a traversal child.
Node::cursor
fn Node::cursor(self : Node) -> @moonrockz/notebooks.Cursor[Node]Start a new navigation root at this node. Use an existing cursor to retain its original ancestors and paths.
Node::equal
fn Node::equal(Node, Node) -> BoolNode::kind
fn Node::kind(self : Node) -> @moonrockz/notebooks.NodeKindClassify this node for format-independent traversal without converting or discarding the concrete Jupyter payload.
Node::metadata
fn Node::metadata(self : Node) -> @moonrockz/notebooks.Metadata?Return this node's own metadata. Some(empty) retains an empty container; None means this node has none. Ancestor metadata is never inherited.
Node::not_equal
fn Node::not_equal(x : Node, y : Node) -> BoolNode::source
fn Node::source(self : Node) -> String?Return a recognized cell's normalized source. Notebook, output, attachment and unknown-cell nodes return None.
Node::to_repr
fn Node::to_repr(Node) -> @moonbitlang/core/debug.Reprimpl Node for Node
Node::children
fn children(self : Node) -> ReadOnlyArray[Node]Node::kind
fn kind(self : Node) -> @moonrockz/notebooks.NodeKindNode::metadata
fn metadata(self : Node) -> @moonrockz/notebooks.Metadata?Node::source
fn source(self : Node) -> String?Notebook
type Notebook derive(Eq, @moonbitlang/core/debug.Debug)An immutable Jupyter v4 document with metadata, ordered cells and preserved extension fields. The file's minor version is retained.
Notebook::Notebook
fn Notebook::Notebook(cells : ReadOnlyArray[Cell], minor? : Int, metadata? : @moonrockz/notebooks.Metadata) -> Notebook raise FormatErrorConstruct a v4 notebook, defaulting to file format 4.5 and empty metadata. Raise UnsupportedVersion for a negative minor version, InvalidField for invalid metadata or a missing required ID, and DuplicateCellId or DuplicateCellName for repeated identities. v4.5 and later require IDs.
Notebook::cell
fn Notebook::cell(self : Notebook, index : Int) -> Cell?Look up a cell by zero-based document index. Return None for negative indices or an index at or beyond the cell count.
Notebook::cell_by_id
fn Notebook::cell_by_id(self : Notebook, id : CellId) -> Cell?Find a cell by its validated ID. Return None if no cell has that ID.
Notebook::cells
fn Notebook::cells(self : Notebook) -> ReadOnlyArray[Cell]Return immutable cells in document order.
Notebook::cells_by_mime_type
fn Notebook::cells_by_mime_type(self : Notebook, mime_type : String) -> ReadOnlyArray[Cell]Select cells containing an exact MIME key in a rich output or attachment. Return each matching cell once, in document order, in an immutable array. Empty or JSON null payloads count as present. Matching is case-sensitive, without wildcard or parameter normalization, just like MimeBundle::get. Source text, metadata, streams, errors and unknown payloads are not searched. Return an empty array when there are no matches.
Notebook::cursor
fn Notebook::cursor(self : Notebook) -> @moonrockz/notebooks.Cursor[Node]Start a typed Jupyter cursor at this notebook, with depth zero and path $.
Notebook::equal
fn Notebook::equal(Notebook, Notebook) -> BoolNotebook::extensions
fn Notebook::extensions(self : Notebook) -> JsonObjectReturn notebook-level fields outside metadata, cells and version fields.
Notebook::fold
fn[A] Notebook::fold(self : Notebook, init : A, f : (A, @moonrockz/notebooks.Cursor[Node]) -> A) -> AThread an accumulator through this notebook tree in pre-order. Callbacks receive typed Jupyter cursors. Use walk for pruning or early termination.
Notebook::from_json
fn Notebook::from_json(value : Json) -> Notebook raise FormatErrorValidate an existing JSON notebook and copy all mutable value graphs. Accept v4 minor versions, requiring cell IDs from 4.5 onward. Raise FormatError on invalid fields, unsupported major versions or duplicate IDs/names. Later mutation of value cannot change the notebook.
Notebook::metadata
fn Notebook::metadata(self : Notebook) -> @moonrockz/notebooks.MetadataReturn immutable notebook metadata, including custom namespaces.
Notebook::nbformat
fn Notebook::nbformat(_self : Notebook) -> IntReturn the notebook file format's major version, always 4. This is independent of the upstream Python library's release number.
Notebook::nbformat_minor
fn Notebook::nbformat_minor(self : Notebook) -> IntReturn the retained notebook file format minor version.
Notebook::not_equal
fn Notebook::not_equal(x : Notebook, y : Notebook) -> BoolNotebook::root
fn Notebook::root(self : Notebook) -> NodeWrap this notebook as the concrete root accepted by shared traversal.
Notebook::to_json
fn Notebook::to_json(self : Notebook) -> Json raise FormatErrorExport an independent notebook JSON object, retaining version numbers, metadata and extension payloads. Multiline values use normalized strings. Mutating the exported object does not alter this notebook.
Notebook::to_repr
fn Notebook::to_repr(Notebook) -> @moonbitlang/core/debug.ReprNotebook::to_string
fn Notebook::to_string(self : Notebook) -> String raise FormatErrorSerialize notebook JSON with normalized multiline strings. Preserve metadata and extensions, but not original whitespace, key order or line splitting. The result can be passed to parse.
Output
type Output derive(Eq, @moonbitlang/core/debug.Debug)An immutable validated output, including preserved extension fields. Construct with Output(content), or obtain outputs from a parsed code cell.
Output::Output
fn Output::Output(content : OutputContent) -> Output raise FormatErrorValidate a typed output payload using the JSON codec's boundary rules. Unknown must use an unrecognized output_type and must not repeat that key inside fields. Raise InvalidField for invalid payload or metadata.
Output::content
fn Output::content(self : Output) -> OutputContentReturn the typed output payload for exhaustive pattern matching.
Output::equal
fn Output::equal(Output, Output) -> BoolOutput::extensions
fn Output::extensions(self : Output) -> JsonObjectReturn extra JSON fields preserved when reading a recognized output. For an unknown output, additional fields live in its Unknown payload.
Output::not_equal
fn Output::not_equal(x : Output, y : Output) -> BoolOutput::output_type
fn Output::output_type(self : Output) -> StringReturn the wire-format discriminator, such as stream or display_data.
An unknown output retains its original discriminator.
Output::to_json
fn Output::to_json(self : Output) -> Json raise FormatErrorExport an independent output object with its discriminator, typed payload and preserved fields. Text is normalized; JSON MIME data is retained.
Output::to_repr
fn Output::to_repr(Output) -> @moonbitlang/core/debug.ReprOutputContent
pub(all) enum OutputContent {
Stream(name~ : String, text~ : String)
DisplayData(data~ : MimeBundle, metadata~ : @moonrockz/notebooks.Metadata)
ExecuteResult(data~ : MimeBundle, metadata~ : @moonrockz/notebooks.Metadata, execution_count~ : ExecutionCount?)
Error(ename~ : String, evalue~ : String, traceback~ : ReadOnlyArray[String])
Unknown(output_type~ : String, fields~ : JsonObject)
} derive(Eq, @moonbitlang/core/debug.Debug)The typed payload of a Jupyter output. Rich display and execution results carry MIME bundles and metadata; None denotes a null execution count. Unknown preserves an unrecognized output type and its additional fields.
OutputContent::equal
fn OutputContent::equal(OutputContent, OutputContent) -> BoolOutputContent::not_equal
fn OutputContent::not_equal(x : OutputContent, y : OutputContent) -> BoolOutputContent::to_repr
fn OutputContent::to_repr(OutputContent) -> @moonbitlang/core/debug.ReprErrors
FormatError
pub(all) suberror FormatError {
InvalidJson(String)
InvalidField(path~ : String, expected~ : String)
UnsupportedVersion(major~ : Int, minor~ : Int)
DuplicateCellId(String)
DuplicateCellName(String)
} derive(Eq, @moonbitlang/core/debug.Debug)A JSON decoding or notebook validation failure. InvalidField reports a logical JSON field path and the expected value. UnsupportedVersion carries file format numbers, not the upstream Python library release version.
FormatError::equal
fn FormatError::equal(FormatError, FormatError) -> BoolFormatError::not_equal
fn FormatError::not_equal(x : FormatError, y : FormatError) -> BoolFormatError::to_repr
fn FormatError::to_repr(FormatError) -> @moonbitlang/core/debug.ReprType aliases
Control
using @moonrockz/notebooks { type Control }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Cursor
using @moonrockz/notebooks { type Cursor }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Metadata
using @moonrockz/notebooks { type Metadata }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
NodePath
using @moonrockz/notebooks { type NodePath }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Values
parse
fn parse(source : String) -> Notebook raise FormatErrorRead notebook JSON into an immutable v4 document. Normalize multiline strings and preserve unknown fields, cell types and output types. Raise InvalidJson for malformed JSON, UnsupportedVersion for another major version, or a typed FormatError for invalid structure or identities.
walk
fn walk(root : Node, enter : (Node) -> @moonrockz/notebooks.Control, leave? : (Node) -> Unit) -> UnitWalk Jupyter nodes in depth-first pre-order with the shared engine. Use walk_cursor when callbacks need path, parent or sibling context.
walk_cursor
fn walk_cursor(root : Node, enter : (@moonrockz/notebooks.Cursor[Node]) -> @moonrockz/notebooks.Control, leave? : (@moonrockz/notebooks.Cursor[Node]) -> Unit) -> UnitUse the shared engine to walk Jupyter positions from a new root. Callbacks retain typed Node payloads. SkipChildren still receives leave; Stop returns immediately without pending leave callbacks.