notebook

package
v0.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notebook reads the code cells of a Jupyter notebook (nbformat 4), so a model written in a notebook can be loaded where a model file can.

Index

Constants

View Source
const Ext = ".ipynb"

Ext is the file extension a notebook is named with.

View Source
const Language = "sysml"

Language is the kernelspec language a notebook must record to be loaded as a SysML model; a notebook recording none is taken to be one.

View Source
const SkipTag = "skip-load"

SkipTag marks a code cell a load passes over: scratch work, a demonstration, anything the notebook runs that another should not inherit.

Variables

This section is empty.

Functions

func BareExpression added in v0.12.0

func BareExpression(src string) (string, bool)

BareExpression reports whether src reads as an expression the kernel answers rather than as a declaration it submits, and the expression it reads as: text that is no file but parses whole as one expression.

func IsCommand added in v0.12.0

func IsCommand(line string) bool

IsCommand reports whether line is a `%` command line.

func IsNotebook

func IsNotebook(path string) bool

IsNotebook reports whether path names a notebook, by its extension.

func NeedsContinuation added in v0.12.0

func NeedsContinuation(src string) bool

NeedsContinuation reports whether src is an unfinished submission: a brace, parenthesis or bracket it opens is not yet closed, or it ends inside a string, quoted name or comment, so the prompt reads another line before submitting it. Each counter clamps at 0 so a stray closer forces nothing.

Types

type Cell

type Cell struct {
	// Index is the cell's 1-based position among the notebook's code cells,
	// which is how a selection names it and how a diagnostic locates it.
	Index  int
	Source string
	Tags   []string
}

Cell is one code cell of a notebook.

func (Cell) Tagged

func (c Cell) Tagged(tag string) bool

Tagged reports whether the cell carries tag in its metadata.

type FormatError

type FormatError struct {
	Name   string
	Reason string
}

FormatError is a file that is not a notebook a load can read, and why.

func (*FormatError) Error

func (e *FormatError) Error() string

type LanguageError

type LanguageError struct {
	Name     string
	Language string
}

LanguageError is a notebook written for another kernel's language, which a load refuses rather than reading its cells as SysML.

func (*LanguageError) Error

func (e *LanguageError) Error() string

type Notebook

type Notebook struct {
	Name     string
	Language string // kernelspec.language, else language_info.name; "" when neither is recorded
	Cells    []Cell
}

Notebook is what a load reads of a notebook: its code cells in notebook order, markdown and raw cells left out, and the language its kernel records.

func Parse

func Parse(name string, data []byte) (*Notebook, error)

Parse reads data as the nbformat 4 notebook called name. A notebook of another format version, or a file that is not a notebook at all, is a *FormatError; one written for another language is a *LanguageError.

func Read

func Read(path string) (*Notebook, error)

Read reads the notebook at path. The error is a *FormatError for a file that is not an nbformat 4 notebook, or the read error.

type Selection

type Selection struct {
	// contains filtered or unexported fields
}

Selection picks the code cells of a notebook a load reads: by position among the code cells, 1-based, as `1,3-5`, or by a tag they carry, as `tag:model`. The zero Selection picks every cell.

func ParseSelection

func ParseSelection(text string) (Selection, error)

ParseSelection reads a selection as it is written after `--cells`.

func (Selection) Apply

func (s Selection) Apply(nb *Notebook) ([]Cell, error)

Apply picks the notebook's cells the selection names, in notebook order. A position past the notebook's code cells is an error naming how many there are; a tag no cell carries is an error naming the tag.

func (Selection) IsZero

func (s Selection) IsZero() bool

IsZero reports whether the selection picks every cell.

func (Selection) String

func (s Selection) String() string

String is the selection as it was written.

type Skipped added in v0.12.0

type Skipped struct {
	CommandLines    int
	ExpressionLines int
}

Skipped counts the lines of a cell Declarations blanked.

func Declarations added in v0.12.0

func Declarations(cell string) (string, Skipped)

Declarations is the text of a cell as what it declares: its declarations where they stand, every `%` command line and expression line blank, so a position in the result is the same position in the cell.

type Statement added in v0.12.0

type Statement struct {
	// Command is set when Text is a `%` command line.
	Command bool
	Text    string
	// Line is the line of the cell, counted from 1, that Text begins on.
	Line int
}

Statement is one unit of a cell: a `%` command line, or a run of declaration or expression text the kernel submits whole.

func Statements added in v0.12.0

func Statements(cell string) []Statement

Statements splits a cell of input into what the kernel reads from it. A `%` line outside a continuation is a command; an expression that closes its brackets is answered on its own, as the prompt answers one line; declarations run together into one submission up to the next command or expression, so a cell may hold a whole model as a file does, blank lines included.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL