darkpyonix guide

Notebook files

A notebook is a Python file

Cells are comments and markdown is a function call, so python file.py runs the same code a kernel runs. Outputs never go into the file; they go into the run logs.

An example

train.py
"""Train a small model."""
import darkpyonix                      # preamble: runs once, before any cell


# %% Parameters [argparse]
LR = darkpyonix.params.get("lr", default=0.01, range=(0.001, 0.1, 0.001))
MODEL = darkpyonix.params.get("model", default=0, choices=["small", "large"])


# %% [markdown]
darkpyonix.markdown("""
## Training
The loop below prints one line per epoch.
""", silent=True)


# %% Train [code]
# @width: 1fr
for epoch in range(3):
    print(f"epoch {epoch} lr={LR} model={MODEL}")
epoch

Lines before the first marker are the preamble. The kernel runs it once, before any cell, in every run. Keep imports and the docstring there.

.pynb has the same syntax as .py. The extension only tells an editor to open the file as a notebook instead of as code. The kernel treats both the same.

Cell markers

Cell metadata

Lines of the form # @key: value right after a marker are that cell's metadata. A value that parses as JSON is used as JSON; otherwise it is a string. Unknown keys are kept and ignored.

KeyMeaningExample
widthCell width in a horizontal layout (a CSS grid track)1fr, 320px
layoutLayout starting at this cellhorizontal, vertical, grid
collapsedShown collapsed at firsttrue
auto_runRuns when a client connects to the kerneltrue
idA stable id a client may assign"c-3f2a"

Layout is display only. Cells in a horizontal layout still run one after another.

Cell types

TypeStatusWhat a run does
codeimplementedRuns the body. The value of a last expression becomes the result and is stored in _.
markdownimplementedRuns the darkpyonix.markdown(...) call.
argparseimplementedRuns darkpyonix.params.get(...) calls; clients show a form.
bindingimplementedRe-evaluates definitions decorated with @darkpyonix.binding without access to code cell variables.
shellimplementedRuns darkpyonix.run_command(...) and streams its output.
parallel, concurrentplanned #7Accepted and kept; runs like code for now.
cinterop, cppinterop, rustinteropplanned #5Accepted and kept; the API raises NotImplementedError.
sql, toml, yaml, jsonplanned #5Accepted and kept; runs like code for now.
anything elseRuns like code; the original type is kept.

Parameters

darkpyonix.params.get(name, default=..., choices=..., range=(min, max, step), type=..., help=...) reads a parameter. With choices, default is an index into the list. The value comes from the run request's params first, then from --name value on the command line, then from default. A value outside range or not in choices fails the cell with ValueError.

That is why the same file works both ways: darkpyonix run train.py --param lr=0.02 and uv run train.py --lr 0.02 set the same parameter.

Markdown

darkpyonix.markdown(text, silent=False) emits text/markdown in a kernel. With silent=True the text is shown but not written to the run log. Under python file.py it prints nothing, so the script's output does not change.

Runtime API

CallIn a kernelUnder python file.py
darkpyonix.markdown(text, silent=False)text/markdown outputnothing
darkpyonix.params.get(...)run request params firstcommand-line arguments
@darkpyonix.bindingisolated re-evaluationthe same
darkpyonix.run_command(cmd, check=False)output as a streamstandard output
darkpyonix.display(obj)display_data outputprint(repr(obj))
darkpyonix.uv.add, darkpyonix.uv.removeinstalls into the kernel's interpreterthe same
darkpyonix.run_parallel(*aws)gathers coroutinesthe same

Outputs also come from print, from writes to file descriptors 1 and 2 (C extensions, subprocesses), from _repr_html_-style methods, and from matplotlib figures, which become PNG outputs when matplotlib is installed in the kernel's interpreter.