Skip to content
Feeding a clanker? Grab this page as raw .md →

WidgetDAG API#

WidgetDAG is a marimo display helper rather than an AnyWidget: it takes a mapping of ids to renderables plus a list of (src, dst) edges, puts each node in a column derived from its edge depth, and draws the connecting arrows. Think mo.hstack, except the columns and the arrows come from the graph. The nodes stay live — a Matrix or Paint embedded as a node is still editable — and WidgetDAG.from_widgets([...]) lets you skip the edge list entirely by reading marimo's own dataflow graph, one node per cell.

See also: GraphWidget for a force-directed graph of data instead of widgets, EdgeDraw for drawing a graph by hand, and ModuleTreeWidget for walking a PyTorch module hierarchy.

Lay widgets out as a DAG and draw the arrows -- like mo.hstack, but columns come from edge depth and the connections are drawn for you.

.. deprecated:: WidgetDAG is deprecated in favour of WidgetFlow, which draws FormulaDAG-style arrows and adds notes, edge labels, ports, captions and a direction switch. WidgetDAG.from_widgets([...]) becomes WidgetFlow.from_widgets([...], direction="right"); for WidgetDAG(nodes, edges) pass the widget objects as a list and the edges as pairs of objects. Constructing a WidgetDAG emits a FutureWarning.

nodes maps an id to any renderable (an input widget, an image, a chart -- anything marimo can show). edges is a list of (src_id, dst_id). layout is (nodes, edges) -> {id: column} (default layered_layout). The widgets stay live and reactive; this only arranges references to them.

This is a marimo-only display helper: the arrow overlay reaches into marimo's rendered DOM, so it is not wired for plain Jupyter.

Instead of spelling out edges by hand, WidgetDAG.from_widgets derives them from marimo's own dataflow graph -- pass the widgets and the arrows follow the notebook's dependency order (see that method).

Example
import marimo as mo
from wigglystuff import Matrix, WidgetDAG

kernel = mo.ui.anywidget(Matrix(rows=3, cols=3))
WidgetDAG(
    nodes={"kernel": kernel, "image": mo.image("cat.png"), "conv": out},
    edges=[("image", "conv"), ("kernel", "conv")],
)
Source code in wigglystuff/widget_dag.py
def __init__(self, nodes, edges, layout=layered_layout):
    warnings.warn(
        "WidgetDAG is deprecated; use WidgetFlow instead "
        "(WidgetFlow.from_widgets for edges inferred from marimo).",
        FutureWarning,
        stacklevel=2,
    )
    self.nodes = dict(nodes)
    self.edges = [list(e) for e in edges]
    self.layout = layout

from_widgets classmethod #

from_widgets(widgets, *, layout=layered_layout)

Build a WidgetDAG from a list of widgets, deriving the arrows from marimo's dataflow graph.

Pass the widget objects (not their names); each node is labelled with the Python variable it is bound to, and an edge is drawn between two widgets whenever the cell defining one depends on the cell defining the other.

WidgetDAG.from_widgets([paint, angle, kernel, conv, kernel2, conv2])

This only works when each widget is a top-level variable defined in its own cell -- marimo's graph is cell-level, so two variables computed in the same cell can't be ordered, and inline expressions aren't variables at all. For those cases use WidgetDAG(nodes, edges) directly. Requires a running marimo kernel.

Source code in wigglystuff/widget_dag.py
@classmethod
def from_widgets(cls, widgets, *, layout=layered_layout):
    """Build a ``WidgetDAG`` from a list of widgets, deriving the arrows from
    marimo's dataflow graph.

    Pass the widget objects (not their names); each node is labelled with the
    Python variable it is bound to, and an edge is drawn between two widgets
    whenever the cell defining one depends on the cell defining the other.

    ```python
    WidgetDAG.from_widgets([paint, angle, kernel, conv, kernel2, conv2])
    ```

    This only works when each widget is a top-level variable defined in its
    own cell -- marimo's graph is cell-level, so two variables computed in the
    same cell can't be ordered, and inline expressions aren't variables at
    all. For those cases use ``WidgetDAG(nodes, edges)`` directly. Requires a
    running marimo kernel.
    """
    edges, names = _infer_edges(widgets, caller="WidgetDAG.from_widgets")
    nodes = {names[id(w)]: w for w in widgets}
    edges = [[names[id(u)], names[id(v)]] for u, v in edges]
    return cls(nodes, edges, layout=layout)

Layout#

A layout is any callable (nodes, edges) -> {id: column}. The default is layered_layout; pass your own to WidgetDAG(..., layout=...) to swap in a different algorithm.

Assign each node a layer (0 = first).

Longest-path layering, then pull every node forward to sit just before its earliest child. That keeps edges between adjacent layers for pipeline/tree shapes, so arrows stay inside the empty gaps between layers and never cross a widget. Longer edges get routed through waypoint lanes.

A layout is just (nodes, edges) -> {id: layer}; pass your own to WidgetDAG(..., layout=...) to swap in a different algorithm.

Source code in wigglystuff/_dag_layout.py
def layered_layout(nodes, edges):
    """Assign each node a layer (0 = first).

    Longest-path layering, then pull every node forward to sit just before
    its earliest child. That keeps edges between adjacent layers for
    pipeline/tree shapes, so arrows stay inside the empty gaps between layers
    and never cross a widget. Longer edges get routed through waypoint lanes.

    A layout is just ``(nodes, edges) -> {id: layer}``; pass your own to
    ``WidgetDAG(..., layout=...)`` to swap in a different algorithm.
    """
    parents = {k: [] for k in nodes}
    children = {k: [] for k in nodes}
    for src, dst in edges:
        parents[dst].append(src)
        children[src].append(dst)
    col = {}

    def longest(k):
        if k not in col:
            col[k] = 0 if not parents[k] else 1 + max(longest(p) for p in parents[k])
        return col[k]

    for k in nodes:
        longest(k)
    changed = True
    while changed:  # pull forward: latest layer still before every child
        changed = False
        for k in nodes:
            if children[k]:
                latest = min(col[c] for c in children[k]) - 1
                if latest > col[k]:
                    col[k] = latest
                    changed = True
    return col

Notes#

WidgetDAG is a marimo-only display helper. Its arrow overlay reaches into marimo's rendered DOM to draw connections in the same coordinate space as the node boxes, so it is not wired for plain Jupyter. The nodes stay live and reactive — embedding a widget (e.g. a Matrix or Paint) as a node keeps it interactive, and editing it re-runs the cell that built the DAG.