docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

#Exploring data in a Jupyter notebook

On this page

Install the Nabi kernel in Jupyter and explore temperature data cell by cell, alongside a single runnable program that does the same work.

In a notebook you work step by step: one cell loads the data, another defines functions, another checks the results. The program below is those cells joined in order from top to bottom. From four weeks of daily highs it computes summary statistics, weekly averages and a text histogram.

#Code

#Walkthrough

  • Top-level lets and fns in a notebook cell persist for the whole session. A cell may let the same name again, as a source file may, so you can re-run a cell with different data (5.1.4 Redeclaration in the same scope).
  • f64 has no Ord (NaN is unordered), so the median sorts by the IEEE total order, sorted_by(f64::total_cmp) (Collections and iterators).
  • temps.min() / temps.max() work on floats (a NaN would win, as in NumPy); ?? f64::NAN covers an empty list (Iterator).
  • temps[week * 7:(week + 1) * 7] takes one range of the list as a slice, and to_list() copies it into the List that mean takes. sum() adds a whole list, and (x - m) ** 2 squares (3.6 Indexing and slicing).
  • Histogram bars come from "#".repeat(c) and the bucket labels are aligned with format specs such as {from:4.1}. An f64 printed with {} uses the shortest digits that read back as the same value, so 26.0 prints as 26; write a precision such as {hi:.1} when you want fixed digits (Strings).

#Running it

First register the Nabi kernel with Jupyter. nabi jupyter install writes the kernelspec (kernels/nabi/kernel.json) into the Jupyter data directory (~/Library/Jupyter on macOS, %APPDATA%\jupyter on Windows, ~/.local/share/jupyter elsewhere) (9.8.3 Installing the kernelspec).

bash
nabi jupyter install
jupyter lab            # pick the "nabi" kernel in a new notebook

To keep a separate kernel per environment, install with nabi jupyter install --env <name>. That creates another kernel shown as nabi (<name>).

In the notebook, the program above runs split into a few cells. The last expression of a cell is its result and is shown with Display; end it with ? to see the Debug form.

text
In [1]:  let temps = [12.5, 14.0, 13.2, 15.8, 17.1, 16.4, 18.9,
                      19.5, 21.0, 20.2, 22.7, 24.1, 23.3, 22.0,
                      20.8, 19.4, 21.6, 23.9, 25.2, 26.0, 24.4,
                      22.1, 20.5, 18.7, 17.9, 19.2, 21.3, 22.8];
         temps.len()
Out[1]:  28

In [2]:  fn mean(xs: List<f64>) -> f64 { xs.sum() / xs.len() as f64 }
         mean(temps)
Out[2]:  20.16071428571428

In [3]:  temps[0:7]?
Out[3]:  [12.5, 14.0, 13.2, 15.8, 17.1, 16.4, 18.9]

In [4]:  mean(temps[0:7].to_list())
Out[4]:  15.414285714285715

let temps and fn mean stay defined in later cells, so cells [3] and [4] use the values and functions created earlier. If you fix a function and re-run its cell, the following cells use the new definition (9.7 JIT cell execution). To use the same kind of cell session in a terminal, run nabi repl (Tools).

Found a mistake? Edit this page on GitHub. Last updated