.md →
FormulaDAG API#
FormulaDAG lays out a set of LaTeX formulas as a DAG. Name each formula and
write \use{name} inside another one: the widget renders that formula's output
symbol in place, infers the edge, and draws an arrow from the producer straight
onto the spot where it's used. Formulas flow top to bottom by dependency depth,
and each producer gets one color for its symbol, its arrows, and every use.
Hover a formula to highlight what it depends on and what depends on it; click
it to set selected.
Draggable \tangle{name} numbers and \note{name}{...} notes work like in
TangleLatex, shared across the whole DAG. Give a formula an
fn (its argument names pick parameters or other formulas) and its value is
computed live into outputs. While you drag a number, every formula that
depends on it shows its live value in place of its symbol, both where it's
defined and wherever it's used.
Call to_html() for a self-contained, static HTML page of the current state
(for docs, blog posts, or attachments), or save("dag.html") to write it to
a file. KaTeX still loads from a CDN, so the page needs internet access.
See also: TangleLatex for a single formula, and WidgetDAG for arranging live widgets as a DAG.
Bases: AnyWidget
Lay out LaTeX formulas as a DAG, wired by where each output is used.
Each formula has a name. Writing \use{name} inside another formula
renders that formula's output symbol (by default the left-hand side of its
first =) and draws an arrow from the producing formula straight onto
the spot where it is used. The edges are inferred from those markers, so
there is no edge list to maintain; cycles are rejected.
Formulas flow top to bottom: a formula sits one row below the deepest
formula it uses. Each producer gets a color shared by its output symbol,
its arrows, and every place it is used. Hover a formula to highlight what
it depends on and what depends on it; click it to set selected.
Numbers and notes work like TangleLatex, shared across the whole DAG:
\tangle{name} in any formula is a draggable number configured in
parameters (the same name in two formulas is one value), and
\note{name}{tex} attaches the plain-language note notes[name] to
that part of its formula. values follows a drag live;
committed_values keeps the pre-drag values until release.
Examples:
from wigglystuff import FormulaDAG
FormulaDAG(
{
"mu": r"\mu = \frac{1}{n}\sum_i x_i",
"var": r"\sigma^2 = \frac{1}{n}\sum_i (x_i - \use{mu})^2",
"sigma": r"\sigma = \sqrt{\use{var}}",
"z": r"z_i = \frac{x_i - \use{mu}}{\note{scale}{\use{sigma}}}",
"band": r"b = \use{mu} \pm \tangle{k}\,\use{sigma}",
},
parameters={"k": {"value": 2, "min_value": 0, "max_value": 4, "step": 0.5}},
notes={"scale": "measured in standard deviations"},
)
Create a FormulaDAG widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formulas
|
Mapping[str, Any]
|
Mapping from a formula name to its LaTeX, or to a dict
with |
required |
parameters
|
Mapping[str, Mapping[str, Any]] | None
|
Draggable numbers, shared by every formula. Mark one
with |
None
|
notes
|
Mapping[str, Any] | None
|
Plain-language notes, as in |
None
|
legend
|
bool
|
Show a strip under the DAG with one chip per number: its
symbol, its |
True
|
theme
|
str
|
Color theme: |
'auto'
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Source code in wigglystuff/formula_dag.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
downstream #
save #
Write :meth:to_html to path and return it as a Path.
to_html #
Render the DAG as a self-contained HTML page.
The page inlines this widget's JavaScript and CSS, embeds the current
state (including live fn outputs), and renders with
interactive: false — a static artifact needing no widget comm.
KaTeX still loads from the same CDN the widget uses, so the file
needs internet access to display math. Use save() to write it.
Source code in wigglystuff/formula_dag.py
upstream #
Synced traitlets#
| Traitlet | Type | Notes |
|---|---|---|
formulas |
list[dict] |
Normalized formulas: name, lhs, tex, symbol, label, inputs, color, ... |
layers / routes |
list |
Row layout and edge routes (computed in Python). |
parameters |
dict |
Normalized \tangle number configuration. |
notes |
dict |
Normalized \note configuration, each with its home formula. |
values |
dict |
Live number values; follows a drag. |
committed_values |
dict |
Values from before the current drag. |
dragging |
bool |
True while a number is being dragged. |
outputs |
dict |
Live fn results per formula (None on failure). |
committed_outputs |
dict |
fn results for committed_values. |
output_errors |
dict |
Exception messages for formulas whose fn failed. |
hovered |
str |
Note under the pointer. |
selected |
str |
Clicked formula name. |
legend |
bool |
Show the legend strip of number and output chips. |
theme |
str |
"auto", "light", or "dark". |
error |
str |
KaTeX load or render errors. |