notebook

package
v0.2.29 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 37 Imported by: ? 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 provides a notebook of markdown text and Goal code cells, whose code runs as the code blocks of content pages do (as in the Cogent Lab docs), sharing its variables and data from one cell to the next, with what it makes, such as plots, shown below it. A notebook is saved as markdown content pages, which can be read as content, and made into a PDF.

Index

Constants

View Source
const DataDirName = "data"

DataDirName is the name of the directory of the saved data of the notebook of a directory.

Variables

This section is empty.

Functions

func Dir

func Dir() string

Dir returns the directory of the notebook whose code is running, such as to save the images of its plots in it, or the current directory if no notebook is running.

func Export

func Export(path string) error

Export exports the notebook whose code is running as a self-contained notebook directory, to the directory at the given path, or a .tar.gz file of it if the path ends with .tar.gz, with its data saved in its data directory (see SaveData). For the notebook of a directory, it has all of the files of the directory (see Dir), such as the images of its plots, and otherwise the file of the notebook. If the path is the directory of the notebook, it only saves the data.

func Load

func Load(path string) (*tensorfs.Node, error)

Load loads the saved data of the notebook of the directory at the given path, or of a .tar.gz file of one (see Export), without running it, into the directory of tensorfs named for it, in its root (tensorfs.CurRoot), which it returns: for example, a notebook that compares the results of other notebooks can load their data, and use them as get("/name/...") in Goal math mode. See Run to run them.

func MakeMenuButton

func MakeMenuButton(p *tree.Plan, ts lab.Tabber)

MakeMenuButton adds to the given plan, as of a toolbar, a Notebook button with a menu of New and Open, which make a new notebook in a tab of the given lab tabs: an empty one, or that of a markdown file. It is typically called in the toolbar maker of an app with lab tabs, as MakeMenuButton(p, br.Tabs).

func Menu(ts lab.Tabber) func(m *core.Scene)

Menu returns a menu of New and Open, which make a new notebook in a tab of the given lab tabs: an empty one, or that of a markdown file. See MakeMenuButton for a button with it.

func NewcellView

func NewcellView(parent ...tree.Node) *cellView

NewcellView returns a new [cellView] with the given optional parent: cellView is the view of a Cell in a Page: a separator above it, which is dragged to move the cell and has its menu, the editor of its markdown or Goal code, and, for Goal code, its output below: the widgets that the code makes and what it prints. A drop of a dragged cell on the view puts the cell before it.

func NewendBar

func NewendBar(parent ...tree.Node) *endBar

NewendBar returns a new [endBar] with the given optional parent: endBar is the end of a Page, after its cells, with buttons to add cells at the end, and a separator on which a dragged cell is dropped at the end.

func Newseparator

func Newseparator(parent ...tree.Node) *separator

Newseparator returns a new [separator] with the given optional parent: separator is the line above a cell, labeled with the kind of the cell, which is dragged to move the cell (or copy it, with Shift), and has the menu of the cell, in its context menu and its menu button. Without a cell, it is the separator of the end of the notebook.

func OpenDialog

func OpenDialog(ts lab.Tabber)

OpenDialog prompts for a notebook directory or markdown file, and opens it in a new tab of the given lab tabs, labeled with its name: Open with no file selected opens the notebook of the directory that is shown.

func SaveData

func SaveData() error

SaveData saves the persistent data of the notebook whose code is running (see tensorfs.Node.SetPersist) in the data directory of its directory (see Dir), replacing what was there, so that they are loaded the next time that it runs, anywhere, as the notebook of its directory. It is typically called at the end of the file that makes the data, as one that gets them from simmer.

func ToMarkdown

func ToMarkdown(cells []*Cell) string

ToMarkdown returns the markdown of the given cells, as a content page.

Types

type Cell

type Cell struct {

	// Kind is the kind of the cell, which is markdown text or Goal code.
	Kind CellKinds

	// Source is the markdown text or Goal code of the cell.
	Source string
	// contains filtered or unexported fields
}

Cell is a cell of a Notebook: a block of markdown text or Goal code.

func ParseMarkdown

func ParseMarkdown(md string) []*Cell

ParseMarkdown returns the cells of the given markdown: a markdown cell for each run of text, separated by cell markers, and a Goal cell for each Goal code block. Other code blocks are part of the text.

type CellKinds

type CellKinds int32 //enums:enum

CellKinds are the kinds of cells of a Notebook.

const (
	// Markdown is a cell of text in markdown, which can have math
	// ($...$), links, and the other elements of content pages. Its source
	// is edited in the notebook, and it is rendered in the [Output].
	Markdown CellKinds = iota

	// Goal is a cell of Goal code, which runs in the interpreter of the
	// notebook, so that the variables and data that it makes can be used
	// by the cells after it. What it makes, such as plots, is shown in the
	// frame below its code, which the code knows as b.
	Goal
)
const CellKindsN CellKinds = 2

CellKindsN is the highest valid value for type CellKinds, plus one.

func CellKindsValues

func CellKindsValues() []CellKinds

CellKindsValues returns all possible values for the type CellKinds.

func (CellKinds) Desc

func (i CellKinds) Desc() string

Desc returns the description of the CellKinds value.

func (CellKinds) Int64

func (i CellKinds) Int64() int64

Int64 returns the CellKinds value as an int64.

func (CellKinds) MarshalText

func (i CellKinds) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*CellKinds) SetInt64

func (i *CellKinds) SetInt64(in int64)

SetInt64 sets the CellKinds value from an int64.

func (*CellKinds) SetString

func (i *CellKinds) SetString(s string) error

SetString sets the CellKinds value from its string representation, and returns an error if the string is invalid.

func (CellKinds) String

func (i CellKinds) String() string

String returns the string representation of this CellKinds value.

func (*CellKinds) UnmarshalText

func (i *CellKinds) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (CellKinds) Values

func (i CellKinds) Values() []enums.Enum

Values returns all possible values for the type CellKinds.

type Meta

type Meta struct {

	// Title is the title of the file.
	Title string

	// Requires are the names (or import paths) of the packages of apps
	// that the code of the file needs, such as "simmer": the file runs
	// only if the interpreter has all of them. Otherwise, the data that it
	// makes are those saved in the notebook (see [SaveData]).
	Requires []string

	// After are the files of the notebook that run before this one, if
	// they are in it, by their names (with or without .md): the files run
	// in an order in which each one runs after those of its After, and
	// otherwise in the order of their names. A file that does not run,
	// because of its Requires, does not stop the files after it.
	After []string
}

Meta is the metadata of a markdown file of a Notebook, from the TOML front matter at its start, between +++ lines, as in the pages of content:

+++
Title = "Get the results of the jobs"
Requires = ["simmer"]
After = ["helpers.md"]
+++

The front matter is part of the text of the first markdown cell of its Page, where it is edited, and saved with it.

func ParseMeta

func ParseMeta(md string) (Meta, error)

ParseMeta returns the metadata of the given markdown, from its front matter (see Meta), which is empty if it has none.

type Notebook

type Notebook struct {
	core.Frame

	// Pages are the pages of the notebook, one for each of its markdown
	// files, in the order of their tabs.
	Pages []*Page `set:"-"`

	// Dir is the directory of the files of the notebook.
	Dir string `set:"-"`
	// contains filtered or unexported fields
}

Notebook is a notebook of markdown text and Goal code cells, in one or more markdown files, each of which is a Page in a tab of the notebook, under its toolbar (with no tabs shown for one file). The code of all of its pages runs in one interpreter, so that the variables and data that the cells make can be used in the cells after them, in the order that the pages run (see Meta). The notebook of a directory has all of its markdown files, its data in tensorfs are in a directory named for it (see Notebook.DataDir), and the persistent data that it saves (see SaveData) are in its data directory, from which they are loaded before it runs, so that it is self-contained: it can be run anywhere, and shared as is or as a .tar.gz file of it (see Export). Its Output shows the whole notebook rendered as a content page, which can be saved as a PDF. In the tabs of a lab browser (see NewTab), the data of the cells, in tensorfs, is in its data tree, and the output is in another tab.

func NewNotebook

func NewNotebook(parent ...tree.Node) *Notebook

NewNotebook returns a new Notebook with the given optional parent: Notebook is a notebook of markdown text and Goal code cells, in one or more markdown files, each of which is a Page in a tab of the notebook, under its toolbar (with no tabs shown for one file). The code of all of its pages runs in one interpreter, so that the variables and data that the cells make can be used in the cells after them, in the order that the pages run (see Meta). The notebook of a directory has all of its markdown files, its data in tensorfs are in a directory named for it (see Notebook.DataDir), and the persistent data that it saves (see SaveData) are in its data directory, from which they are loaded before it runs, so that it is self-contained: it can be run anywhere, and shared as is or as a .tar.gz file of it (see Export). Its Output shows the whole notebook rendered as a content page, which can be saved as a PDF. In the tabs of a lab browser (see NewTab), the data of the cells, in tensorfs, is in its data tree, and the output is in another tab.

func NewTab

func NewTab(ts lab.Tabber, label, filename string) *Notebook

NewTab returns a notebook in a tab with the given label of the given lab tabs, as in the lab browser, with its toolbar, recycling the tab if it exists. If the file name is not empty, the notebook of that directory or markdown file is opened in it (see Notebook.Open), unless it already has it.

func OpenTab

func OpenTab(ts lab.Tabber, filename string) *Notebook

OpenTab opens the notebook of the given directory or markdown file (see Notebook.Open) in a tab of the given lab tabs, labeled with its name.

func Run

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

Run runs the notebook of the given directory or markdown file (see Notebook.Open), in a tab of the lab tabs of the notebook whose code is running, if it is in some, and otherwise without a view, returning it. When it returns, the data of the notebook are in its data directory in tensorfs (see Notebook.DataDir), such as for a notebook that compares the results of other notebooks. See Load to only load its saved data.

func (*Notebook) AddFile

func (nb *Notebook) AddFile(name string) error

AddFile adds a new markdown file with the given name to the notebook, in its directory, as a new page after the others, which is saved with the notebook (see Notebook.Save). The notebook is then that of its directory.

func (*Notebook) AddGoal

func (nb *Notebook) AddGoal()

AddGoal adds a new Goal cell after the cell last edited, in the page of the selected tab.

func (*Notebook) AddMarkdown

func (nb *Notebook) AddMarkdown()

AddMarkdown adds a new markdown cell after the cell last edited, in the page of the selected tab.

func (*Notebook) CurrentPage

func (nb *Notebook) CurrentPage() *Page

CurrentPage returns the page of the selected tab.

func (*Notebook) DataDir

func (nb *Notebook) DataDir() *tensorfs.Node

DataDir returns the directory of the data of the notebook in tensorfs, in its root (tensorfs.CurRoot), named as the notebook (its directory, for the notebook of a directory, and otherwise its file name, or Notebook), which is the current directory of each cell when it starts, so that the data that the cells make, as of the # x := ... definitions of Goal math, goes there, as in its subdirectories that the cells make (with mkdir and cd). Notebook.RunAll deletes all of it first.

func (*Notebook) Init

func (nb *Notebook) Init()

func (*Notebook) MakeToolbar

func (nb *Notebook) MakeToolbar(p *tree.Plan)

MakeToolbar makes the toolbar of the notebook.

func (*Notebook) Markdown

func (nb *Notebook) Markdown() string

Markdown returns the markdown of the whole notebook, as a content page: those of its pages, in the order that they run, with the front matter of the first one only.

func (*Notebook) Open

func (nb *Notebook) Open(filename core.Filename) error

Open opens the notebook of the given directory or markdown file, and runs it (see Notebook.RunAll). The notebook of a directory has all of its markdown files, as pages, and a markdown file is opened as the notebook of its directory, with its page selected, if its directory is that of a notebook, with a data directory, or a markdown file with After or Requires in its front matter (see Meta).

func (*Notebook) PDF

func (nb *Notebook) PDF() error

PDF saves the output of the notebook as a PDF, in its directory, and opens it.

func (*Notebook) ResetInterpreter

func (nb *Notebook) ResetInterpreter()

ResetInterpreter makes the next cell that runs use a new interpreter, without the variables of the cells that ran before. Notebook.RunAll does this.

func (*Notebook) RunAll

func (nb *Notebook) RunAll() error

RunAll runs all of the Goal cells of all of the pages, in the order of the pages (see Meta), in a new interpreter, after deleting all of the data of the notebook (see Notebook.DataDir), and loading its saved data (see SaveData). A page whose Requires the interpreter does not have does not run, as shown at its top. It does not run if the After of the pages make a cycle.

func (*Notebook) Save

func (nb *Notebook) Save() error

Save saves all of the pages of the notebook to their files, as markdown.

func (*Notebook) SaveAs

func (nb *Notebook) SaveAs(filename core.Filename) error

SaveAs saves the page of the selected tab to the given markdown file, which becomes its file.

func (*Notebook) SetCells

func (nb *Notebook) SetCells(cells []*Cell) *Notebook

SetCells sets the cells of the notebook to the given cells, in one page without a file.

func (*Notebook) ShowOutput

func (nb *Notebook) ShowOutput() *Output

ShowOutput shows the output of the notebook: in another tab of its lab tabs, if it is in lab tabs, and otherwise in a new window.

type Output

type Output struct {
	core.Frame

	// Notebook is the notebook that is shown.
	Notebook *Notebook
	// contains filtered or unexported fields
}

Output shows a Notebook as a content page, which runs its code as content does (as in the Cogent Lab docs), in a new interpreter, and can be saved as a PDF. It shows the current cells of the notebook when it is shown, as when its tab is selected. Its code runs as that of the notebook (see Notebook.RunAll): from the saved data of the notebook, with each code block running as a cell of the notebook, in its data directory, and the pages that do not run here are shown with why, and their code, which does not run.

func NewOutput

func NewOutput(parent ...tree.Node) *Output

NewOutput returns a new Output with the given optional parent: Output shows a Notebook as a content page, which runs its code as content does (as in the Cogent Lab docs), in a new interpreter, and can be saved as a PDF. It shows the current cells of the notebook when it is shown, as when its tab is selected.

func (*Output) Init

func (op *Output) Init()

func (*Output) MakeToolbar

func (op *Output) MakeToolbar(p *tree.Plan)

MakeToolbar makes the toolbar of the output.

func (*Output) PDF

func (op *Output) PDF() error

PDF saves the output as a PDF, in the directory of the notebook, and opens it.

func (*Output) Refresh

func (op *Output) Refresh()

Refresh shows the current cells of the notebook, if they changed, and runs their code, as Notebook.RunAll does: from the saved data of the notebook, without the pages that do not run here, whose code is shown, and each code block runs as a cell of the notebook.

func (*Output) SetNotebook

func (t *Output) SetNotebook(v *Notebook) *Output

SetNotebook sets the Output.Notebook: Notebook is the notebook that is shown.

type Page

type Page struct {
	core.Frame

	// Cells are the cells of the page.
	Cells []*Cell `set:"-"`

	// Filename is the markdown file of the page, if it has one.
	Filename core.Filename `set:"-"`
	// contains filtered or unexported fields
}

Page is a page of a Notebook: the markdown text and Goal code cells of one of its markdown files, each edited in its own editor, one after the other, in a tab of the notebook. Command+Enter goes to the next cell, running Goal code, which also runs when its editor loses the focus, if it changed, in the interpreter of the notebook, which all of its pages share, so that the variables and data made by a cell can be used in the cells after it, and in the pages after it, and what it makes, such as plots, is shown below it. Each cell has a separator above it, which can be dragged to move the cell (or copy it, with Shift), and has a menu to insert, duplicate, and delete cells. The insert keys (Control+I and Control+O) insert a cell before or after the cell being edited, prompting for its kind. The front matter of the file (see Meta) is at the start of its first markdown cell.

func NewPage

func NewPage(parent ...tree.Node) *Page

NewPage returns a new Page with the given optional parent: Page is a page of a Notebook: the markdown text and Goal code cells of one of its markdown files, each edited in its own editor, one after the other, in a tab of the notebook. Command+Enter goes to the next cell, running Goal code, which also runs when its editor loses the focus, if it changed, in the interpreter of the notebook, which all of its pages share, so that the variables and data made by a cell can be used in the cells after it, and in the pages after it, and what it makes, such as plots, is shown below it. Each cell has a separator above it, which can be dragged to move the cell (or copy it, with Shift), and has a menu to insert, duplicate, and delete cells. The insert keys (Control+I and Control+O) insert a cell before or after the cell being edited, prompting for its kind. The front matter of the file (see Meta) is at the start of its first markdown cell.

func (*Page) AddCell

func (pg *Page) AddCell(kind CellKinds) *Cell

AddCell adds a new cell of the given kind after the current cell (the one last edited), or at the end, and returns it.

func (*Page) DeleteCell

func (pg *Page) DeleteCell(c *Cell)

DeleteCell deletes the given cell. The last cell is not deleted.

func (*Page) DragMovesDefault

func (pg *Page) DragMovesDefault() bool

DragMovesDefault returns that a drag of a cell moves it, and copies it with Shift (see core.DragMover).

func (*Page) DuplicateCell

func (pg *Page) DuplicateCell(c *Cell)

DuplicateCell inserts a copy of the given cell after it.

func (*Page) Init

func (pg *Page) Init()

func (*Page) InsertCell

func (pg *Page) InsertCell(idx int, c *Cell)

InsertCell inserts the given cell at the given index (clamped to the cells), and focuses its editor.

func (*Page) Markdown

func (pg *Page) Markdown() string

Markdown returns the markdown of the page, as a content page.

func (*Page) Meta

func (pg *Page) Meta() (Meta, error)

Meta returns the metadata of the page, from the front matter of its file at the start of its first cell (see Meta).

func (*Page) MoveCell

func (pg *Page) MoveCell(c *Cell, to int)

MoveCell moves the given cell to the given index in the cells before it is moved: the cell goes before the cell at that index, or at the end if it is the number of cells.

func (*Page) Save

func (pg *Page) Save() error

Save saves the page to its file, as markdown.

func (*Page) SetCells

func (pg *Page) SetCells(cells []*Cell) *Page

SetCells sets the cells of the page.

func (*Page) SetKind

func (pg *Page) SetKind(c *Cell, kind CellKinds)

SetKind changes the given cell to the given kind of cell.

Jump to

Keyboard shortcuts

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