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
- func Dir() string
- func Export(path string) error
- func Load(path string) (*tensorfs.Node, error)
- func MakeMenuButton(p *tree.Plan, ts lab.Tabber)
- func Menu(ts lab.Tabber) func(m *core.Scene)
- func NewcellView(parent ...tree.Node) *cellView
- func NewendBar(parent ...tree.Node) *endBar
- func Newseparator(parent ...tree.Node) *separator
- func OpenDialog(ts lab.Tabber)
- func SaveData() error
- func ToMarkdown(cells []*Cell) string
- type Cell
- type CellKinds
- func (i CellKinds) Desc() string
- func (i CellKinds) Int64() int64
- func (i CellKinds) MarshalText() ([]byte, error)
- func (i *CellKinds) SetInt64(in int64)
- func (i *CellKinds) SetString(s string) error
- func (i CellKinds) String() string
- func (i *CellKinds) UnmarshalText(text []byte) error
- func (i CellKinds) Values() []enums.Enum
- type Meta
- type Notebook
- func (nb *Notebook) AddFile(name string) error
- func (nb *Notebook) AddGoal()
- func (nb *Notebook) AddMarkdown()
- func (nb *Notebook) CurrentPage() *Page
- func (nb *Notebook) DataDir() *tensorfs.Node
- func (nb *Notebook) Init()
- func (nb *Notebook) MakeToolbar(p *tree.Plan)
- func (nb *Notebook) Markdown() string
- func (nb *Notebook) Open(filename core.Filename) error
- func (nb *Notebook) PDF() error
- func (nb *Notebook) ResetInterpreter()
- func (nb *Notebook) RunAll() error
- func (nb *Notebook) Save() error
- func (nb *Notebook) SaveAs(filename core.Filename) error
- func (nb *Notebook) SetCells(cells []*Cell) *Notebook
- func (nb *Notebook) ShowOutput() *Output
- type Output
- type Page
- func (pg *Page) AddCell(kind CellKinds) *Cell
- func (pg *Page) DeleteCell(c *Cell)
- func (pg *Page) DragMovesDefault() bool
- func (pg *Page) DuplicateCell(c *Cell)
- func (pg *Page) Init()
- func (pg *Page) InsertCell(idx int, c *Cell)
- func (pg *Page) Markdown() string
- func (pg *Page) Meta() (Meta, error)
- func (pg *Page) MoveCell(c *Cell, to int)
- func (pg *Page) Save() error
- func (pg *Page) SetCells(cells []*Cell) *Page
- func (pg *Page) SetKind(c *Cell, kind CellKinds)
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*CellKinds) SetString ¶
SetString sets the CellKinds value from its string representation, and returns an error if the string is invalid.
func (*CellKinds) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
CurrentPage returns the page of the selected tab.
func (*Notebook) DataDir ¶
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) MakeToolbar ¶
MakeToolbar makes the toolbar of the notebook.
func (*Notebook) Markdown ¶
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 ¶
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 ¶
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 ¶
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) SaveAs ¶
SaveAs saves the page of the selected tab to the given markdown file, which becomes its file.
func (*Notebook) SetCells ¶
SetCells sets the cells of the notebook to the given cells, in one page without a file.
func (*Notebook) ShowOutput ¶
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 ¶
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) MakeToolbar ¶
MakeToolbar makes the toolbar of the output.
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 ¶
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 ¶
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 ¶
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 ¶
DeleteCell deletes the given cell. The last cell is not deleted.
func (*Page) DragMovesDefault ¶
DragMovesDefault returns that a drag of a cell moves it, and copies it with Shift (see core.DragMover).
func (*Page) DuplicateCell ¶
DuplicateCell inserts a copy of the given cell after it.
func (*Page) InsertCell ¶
InsertCell inserts the given cell at the given index (clamped to the cells), and focuses its editor.
func (*Page) Meta ¶
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 ¶
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.