0%

Notebook

A Notebook is a page of markdown text and Goal code cells, like a Jupyter notebook, whose code runs in an interpreter that the cells share, so that the variables and data that one cell makes can be used in the cells after it, and what it makes, such as plots, is shown below it. A notebook is saved as a markdown file, as a page of content like those of these docs, with its Goal cells as Goal code blocks, so that it can be read, compared and edited as text, and made into a PDF.

Go API docs: notebook.Notebook

Using a notebook

Notebooks open in the tabs of a lab Browser, such as that of an app made with lab.NewBasicWindow, or simmer: the Notebook button of its toolbar makes a new notebook or opens a markdown file, and a markdown file in the files opens as a notebook. The notebook command opens the notebook of the markdown file given as its argument, or a new one:

notebook analysis.md

Each cell has an editor of its markdown or Goal code. A Goal cell runs when its editor loses the focus after it was edited, and Command+Enter runs it and goes to the next cell. Control+I and Control+O insert a new cell before or after the cell being edited, and the separator above each cell can be dragged to move it (or copy it with Shift), and has a menu to insert, duplicate and delete cells. Run all runs all of the Goal cells in order, in a new interpreter, and Output shows the whole notebook rendered as a page, which can be saved as a PDF.

In the code of a cell, b is the frame below it, where the widgets that it makes are shown, such as a plot:

plt := lab.NewPlot(b)
plots.NewLine(plt, plot.Data{plot.X: x, plot.Y: y})

Data

The data of a notebook are in a directory of tensorfs named for it (by its file name, or its directory for the notebook of a directory, see below), which is the current directory of each cell when it starts, so that the variables that math mode records are there, along with the directories that the cells make, and they are shown in the data tree of the browser. Run all deletes all of them first, so that the notebook makes its data from scratch.

##
mkdir sine
cd sine
x := linspace(0., 1., 100, true)
y := sin(2 * pi * 3 * x)
##

Notebooks of several files

A notebook can have more than one markdown file: the notebook of a directory has all of its markdown files, each of which is a page in a tab of the notebook, under its toolbar. The code of all of the pages runs in one interpreter, so that a file can define helper functions that the files after it use, and the data that one file makes can be analyzed by the next one. Run all runs the pages in order, after deleting the data of the notebook and loading its saved data (see below).

Each file can have TOML front matter at its start, between +++ lines, as content pages do, which is in its first markdown cell, where it can be edited:

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

    After lists 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 cycle of After (a file that runs after itself) is an error, which names the files of the cycle, and the notebook does not run.

  • • 

    Requires lists 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, and otherwise shows why it did not run at its top. The files after it still run, on the data that it saved. For example, a file that gets the results of simulations from simmer only runs in simmer, and the files that analyze them run anywhere.

A notebook directory is self-contained: it has the files of the notebook, its saved data in its data directory, and anything else that it makes, such as the images of its plots, so that it can be run anywhere, and shared as is, or as a .tar.gz file of it. A directory is opened as a notebook, with all of its files, from the Open of the toolbar or of the Notebook button (where Open with no file selected opens the directory that is shown), and so is a markdown file in a directory that has a data directory, or a markdown file with After or Requires in its front matter, with its page selected. The File button of the toolbar adds a new file to the notebook, and Save saves all of its files.

The data that a notebook saves are those marked as persistent in tensorfs, with the persist command of math mode (see Saving data), which are saved in its data directory by notebook.SaveData(), as plain files of tab-separated values that mirror the structure of the tensorfs directories. They are loaded into the data directory of the notebook before it runs. For example, the last cell of a file that gets the results of simulations:

##
persist sets
persist jobs
##
errors.Log(notebook.SaveData())

The functions of the notebook package can be used in the code of the cells:

  • • 

    notebook.Dir() returns the directory of the notebook, such as to save the images of its plots in it.

  • • 

    notebook.SaveData() saves the persistent data of the notebook in its data directory.

  • • 

    notebook.Export(path) exports the notebook, with its files and saved data, to the directory at the given path, or a .tar.gz file of it if the path ends with .tar.gz.

  • • 

    notebook.Load(path) loads the saved data of the notebook of the directory at the given path (or a .tar.gz file of one) into the directory of tensorfs named for it, without running it.

  • • 

    notebook.Run(path) runs the notebook of the given directory or markdown file, in a tab of the browser, so that its data are in tensorfs when it returns.

A notebook can then compare the results of other notebooks, such as those of different simulations, by loading (or running) them, and using their data in tensorfs by their names:

for _, nb := range []string{"../model_a", "../model_b"} {
	errors.Log1(notebook.Load(filepath.Join(notebook.Dir(), nb)))
}
##
a := get("/model_a/results/Accuracy")
b := get("/model_b/results/Accuracy")
##