# Notebook

> Record the steps of a Jupyter notebook with ormaos.record() in its cells, and read each step as a recording of its own.

In a notebook, put the step of a cell inside `with ormaos.record():`. Each block becomes a recording in `.ormaos/` next to the notebook, read with the same verbs. The example is [notebook](/examples/notebook.md): weekly sales with tax, four cells that record their step, run from top to bottom in a new kernel by `run_notebook.py`.

```python
import ormaos

with ormaos.record():
    rows = load("sales.csv")
```

## Run the notebook

```bash
python run_notebook.py
```

```console
$ python run_notebook.py
week total with tax: 47498.40
[IPKernelApp] WARNING | Kernel is running over TCP without encryption. All communication (including code and outputs) is sent in plain text and is susceptible to eavesdropping. Use IPC transport or launch with kernel manager-provisioned CurveZMQ keys to enable transport encryption.
```

The warning is the kernel's own. Nothing is printed by the blocks: `summary=True` would print the short summary of each block when it ends.

## One recording per block

```console
$ ormaos list
4 recordings in .ormaos, newest first
r4  2026-11-27 12:00:00  complete  record() block  …  1 process, 2 calls, 0 exceptions (0 swallowed)  /tmp/ormaos-docs-generate/venv/bin/python ipykernel_launcher
r3  2026-11-27 12:00:00  complete  record() block  …  1 process, 1 call, 0 exceptions (0 swallowed)  /tmp/ormaos-docs-generate/venv/bin/python ipykernel_launcher
r2  2026-11-27 12:00:00  complete  record() block  …  1 process, 1 call, 0 exceptions (0 swallowed)  /tmp/ormaos-docs-generate/venv/bin/python ipykernel_launcher
r1  2026-11-27 12:00:00  complete  record() block  …  1 process, 1 call, 1 exception (0 swallowed), 1 io  /tmp/ormaos-docs-generate/venv/bin/python ipykernel_launcher
next: ormaos show r4 | ormaos show r3 | ormaos show r2 | ormaos diff r3 r4
```

## What did each step receive and return?

```console
$ ormaos tree r2
r2:c1 drop_key_accounts(rows: list[40 dict]) -> list[34 dict]  /tmp/ipykernel_…/1887773198.py:17  …  task 1
next: ormaos exceptions r2
```

```console
$ ormaos tree r3
r3:c1 add_tax(rows: list[34 dict]) -> list[34 dict]  /tmp/ipykernel_…/1887773198.py:21  …  task 1
next: ormaos exceptions r3
```

```console
$ ormaos tree r4
r4:c1 total(rows: list[34 dict]) -> float  /tmp/ipykernel_…/1887773198.py:25  …  task 1  calls below: 1
  r4:c2 total.<locals>.<genexpr>(.0: list_iterator) -> NoneType  /tmp/ipykernel_…/1887773198.py:26  …  task 1
next: ormaos exceptions r4
```

`drop_key_accounts` received 40 rows and returned 34; `add_tax` received and returned 34; `total` returned a `float`. The rows are dicts, so their shape is `list[34 dict]`; with pandas the tree would show rows, columns and nulls.

## What the first block saw

```console
$ ormaos show r1
r1 record() block in …: 1 Python process, 1 call, 1 exception (0 swallowed, 0 uncaught, 1 StopIteration taken by the interpreter)
command: /tmp/ormaos-docs-generate/venv/bin/python ipykernel_launcher
code: git commit not read (the working directory is not inside a git repository)
io: 1 event (file 1)
not collected: the block as it runs without ormaos: its environment is changed: `with ormaos.record():` sets ORMAOS_DIR, ORMAOS_PATH and ORMAOS_IO in os.environ while the block runs (the Python processes started inside find the recording through them); when the block ends, each is put back to the value the program had, or removed if it had none
not collected: which notebook cell a call is in: the kernel gives every cell a temporary file name (.../ipykernel_<pid>/<number>.py); calls and exceptions are recorded under that name, not under the cell's number, and the file is gone when the kernel stops
not collected: io: reads of the 1 file opened at 1887773198.py:10: opened for reading, and no `read` call by Python code was seen on a file of that name: if it was read, that was inside C code (a file that is iterated, `for line in f`, `csv.reader`)
never recorded: values, and 20 more kinds of things (`ormaos show --full` lists them with the reason)
next: ormaos tree r1:c1 | ormaos io r1
```

The `StopIteration` is the end of the `csv.DictReader` loop, taken by the interpreter:

```console
$ ormaos exceptions r1:e1
r1:e1 StopIteration: taken by the interpreter
  raised at   /tmp/ormaos-docs-python/cpython-3.14.4-linux-x86_64-gnu/lib/python3.14/csv.py:177 in DictReader.__next__  [r1:c1]  (not your code; first seen at /tmp/ipykernel_…/1887773198.py:13 in load)
  left calls   load [r1:c1]
  handled at  nowhere seen
  message     not recorded; args: tuple[0]
next: ormaos tree r1:c1
```

## Notes for notebooks

- The kernel gives every cell a temporary file name (`/tmp/ipykernel_…/<number>.py`); calls are recorded under that name, not under the cell's number, and the file is gone when the kernel stops. The recording says so.
- A block sets a few variables in `os.environ` while it runs and puts them back when it ends; every recording of a block says so.
- A block records only what runs inside it, in this process and in the Python processes started inside it. Under `ormaos run`, a block is part of that recording.
- `ormaos.record(values=True)` records values for that block, like `--values`. See [ormaos.record()](/python/reference/record.md).

---

Source: https://docs.ormaos.com/python/guides/notebook/ · ormaos 0.0.1 · ormaos-python 968f69776c · ormaos-examples 6e27be5922
