matrace
A minimal nbdev for marimo notebooks. Write the code, its docs and its tests in a marimo notebook. matrace turns the exported cells into a normal Python package and keeps the two in sync.
| matrace | nbdev equivalent | does |
|---|---|---|
matrace-export |
nbdev-export |
notebook cells → modules in lib_path |
matrace-export --update |
nbdev-update |
edits made in modules → back into the notebook cells |
matrace-prepare |
nbdev-prepare |
export, run every notebook's tests in parallel, then clean |
matrace-clean |
nbdev-clean |
normalize notebooks so diffs and merges stay clean |
Run matrace --help for a guide to using it in a notebook and the CLI, and to organizing notebooks.
Install
pip install mo-trace # or: uv add --dev mo-trace (imported as `matrace`)
Add a [tool.matrace] table to your project's pyproject.toml:
[tool.matrace]
lib_path = "mypkg" # default: [project].name, with - replaced by _
nbs_path = "jobs" # default: jobs
Writing notebooks
Directives are #| comments on the first lines of a cell. They work the same as in nbdev.
with app.setup:
#| default_exp core # where `#| export` cells go -> mypkg/core.py
import numpy as np
@app.cell
def _():
#| export # -> mypkg/core.py, listed in __all__
def norm(x): return x / np.linalg.norm(x)
return (norm,)
@app.cell
def _(norm):
#| export metrics.distance # choose the file for this cell -> mypkg/metrics/distance.py
def cosine(a, b): return norm(a) @ norm(b)
return (cosine,)
@app.cell
def _(cosine):
assert cosine(np.ones(3), np.ones(3)) == 1 # an ordinary cell is a test
return
@app.cell
def _():
#| eval: false # skipped by matrace-prepare
slow_benchmark()
return
Supported directives: default_exp, export [module], exporti [module] (exported but left out of __all__), exports, and
eval: false. matrace also follows nbdev for these details:
__all__is built from the public names in exported cells, plus any names listed in_all_.from mypkg.x import ybecomes a relative import in the generated module, and becomes absolute again when synced back.- The module docstring comes from the
> summaryline under the notebook's# Titlemarkdown cell. - Notebooks whose names start with
_or.are skipped.
Differences from nbdev (by design or necessity)
- Imports across modules are generated for you. Marimo forbids defining a name in more than one cell, so a cell exported
to
metrics/distance.pycannot re-importnormthe way an nbdev notebook would. matrace reads marimo's dataflow graph instead. Each module gets an# %% auto #1block that imports what its cells use but don't define: import statements from other cells, such as the setup cell, are copied, and names exported to other modules are imported from those modules. #| export other_modulecan create a new file. nbdev requires the target module to already exist.- Cells are identified by position. Marimo files have no stable cell ids, so the
# %% ../jobs/nb.py #3markers use the cell's position.--updaterefuses to write if that cell is no longer an exported cell of the same module. Runmatrace-exportagain after reordering cells. - Tests run the notebook as a script,
python nb.py, one process per notebook, in the notebook's directory, withIN_TEST=1set. Cells execute in marimo's dependency order. - Clean has less to remove. Marimo files contain no outputs or execution counts.
matrace-cleanrewrites each notebook in marimo's own canonical format and strips trailing whitespace. It keeps__generated_withunchanged, so upgrading marimo doesn't change every notebook.matrace-clean --stdinworks as a git filter. - Not included: docs, README generation,
_modidx.py,llms.txtand Quarto.matrace-prepareis export + test + clean. - Private marimo APIs. matrace uses marimo's internal parser and code generator, so it pins
marimo>=0.25,<0.26.
Example: a tiny language model from one notebook
examples/tinylm is a character-level bigram language model, the classic first language model. It
trains on a list of names, reports perplexity and samples new names. All of it is in jobs/tinylm.py.
Each cell picks its target file, giving tinylm/{data,tokenizer,model,train}.py. The tests sit next to the code they check.
cd examples/tinylm
matrace-prepare # export, test, clean -> Success.
pip install . # or: pip install -e .
tinylm-train # {'train_ppl': 11.14, 'val_ppl': 11.58} + sampled names
marimo edit jobs/tinylm.py # keep developing in the notebook
Training data: notebook → Pi rollout → Tinker (matrace-convert)
pip install "mo-trace[tinker]"
matrace-convert examples/tinylm/jobs/tinylm.py -o traces
python examples/tinker_train.py traces # SFT, then on-policy distillation, on Tinker
matrace-convert replays a notebook as a session of the Pi coding agent
building it. The replay uses Pi 1.5.2's system prompt, its read/bash/edit/write tool schemas and its result
strings. The agent explores the project, writes the first cells, then edits in each exported cell with its tests, in
marimo dependency order. It runs matrace-prepare after every step and finally checks that the package imports. Every tool
call runs for real in a scratch copy of the project, so tool results are real output. A notebook whose tests fail is
rejected. The output files are:
| file | contents |
|---|---|
pi_session.jsonl |
the rollout as a Pi v3 session file, with a custom entry recording its notebook source |
messages.json |
the same conversation in OpenAI / tinker-cookbook format, plus Pi's system prompt and tool specs |
sft.jsonl |
a Tinker Datum (model_input, target_tokens, 0/1 weights) of the whole trajectory, rendered with Inkling 2 Small's renderer. Only assistant tokens carry loss: reasoning, text, tool calls and end-of-turn |
opd_prompts.jsonl |
one starting state per assistant turn for on-policy distillation. Each has prompt_tokens (a token prefix of the trajectory), teacher_prompt_tokens, the reference turn, and the workspace files at that point |
Load the data with matrace.convert.load_sft(). matrace.convert.opd_datum() builds the importance_sampling datum,
with advantage = teacher logprob − student logprob for each sampled token.
Notes:
- Teacher. Inkling 2 Small's tokenizer isn't shared with any other Tinker model, Inkling v1 included. A token-level
teacher therefore has to be Inkling 2 Small as well.
teacher_prompt_tokensgives it privileged context: the same state, with the finished notebook in the task message. You can also use a stronger fine-tuned checkpoint. - Reasoning. The markdown cells before each step become that turn's
thinkingblock (Pi's and the cookbook's format), which carries loss like the rest of the turn. A notebook with little prose gives little reasoning, so the default thinking effort stays at 0 (--effort). - Ideal path only. Traces don't yet include mistakes and recoveries. Distillation samples one turn per state; a
multi-turn version would run the student's tool calls in the record's
workspacesnapshot.
Development
uv sync && uv run pytest # includes parity tests that run real nbdev on equivalent .ipynb notebooks
Metadata
Release files for mo-trace 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mo_trace-0.1.0.tar.gz | 26.5 kB | Without provenance | Details for mo_trace-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mo_trace-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for mo_trace-0.1.0-py3-none-any.whl |
Total release size: 50.4 kB
Release files / mo_trace-0.1.0.tar.gz
| Download URL | mo_trace-0.1.0.tar.gz |
|---|---|
| Size | 26.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
026bfde8271647f6183488115dbd7f6588a1422f2702294159d9c291a254d33b
|
|
BLAKE2b-256 checksum How to use checksums |
e97a6e44427b2d1f4a77c16dea3b563fcdd3dc25454ff7cd314123078a9bd331
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / mo_trace-0.1.0-py3-none-any.whl
| Download URL | mo_trace-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e113eaeb800b046fec07faa83f1f880c2296e1666241cb75523a1b9ed670fc30
|
|
BLAKE2b-256 checksum How to use checksums |
364f8904f978d05fda7529f35e1c4de1181ae5f2ecd95f34c678307b93f88dc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for mo-trace 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.