bootstack.TreeNode#

class bootstack.TreeNode(label='', *, icon=None, open_icon=None, closed_icon=None, expanded=False, loader=None, data=None, **extra)#

Bases: object

A single node in a Tree.

Nodes are identity-based handles: hold the object returned by Tree.add() and pass it back to expand(), select(), remove(), and friends. Two nodes are never equal unless they are the same object.

Display attributes (label, icon, open_icon, closed_icon, …) are a non-destructive view over the node. Any keyword not recognized as a display parameter is folded into data, so a handler always gets the user’s domain data back.

children: list[TreeNode]#

The child nodes, in order. Empty for a leaf, and empty for a lazy node until its loader has run on first expand.

closed_icon: str | None#

Icon shown while the node is collapsed, overriding icon when set.

data: dict[str, Any]#

Open-ended data bag for your own attributes. Holds any keyword passed to add() that is not a recognized display parameter, and — for a data-source-backed tree — the node’s source record.

property depth: int#

Distance from the root (root nodes are depth 0).

property expandable: bool#

Whether this node can be expanded (has children or a lazy loader).

expanded: bool#

Whether the node is currently expanded — kept in sync as it expands and collapses.

icon: str | None#

Bootstrap icon name shown before the label, or None for no icon.

property is_leaf: bool#

Whether this node has no children and cannot lazily load any.

label: str#

The node’s display label.

loader: Callable[[TreeNode], Any] | None#

Callable invoked on first expand to fetch children lazily, or None for an eagerly-populated node. Receives the node; returns an iterable of child specs.

open_icon: str | None#

Icon shown while the node is expanded, overriding icon when set.

parent: TreeNode | None#

The parent node, or None for a root node.

add(label='', **kwargs)#

Add a child node under this node and return it.

Parameters:
  • label (str) – The child’s display label.

  • **kwargs (Any) – Forwarded to Tree.add (icon, open_icon, closed_icon, expanded, children, loader, data, and overflow data kwargs).

ancestors()#

Yield this node’s parent, grandparent, … up to the root.

collapse()#

Collapse this node to hide its children.

descendants()#

Yield every node beneath this one, depth-first.

expand()#

Expand this node to reveal its children.

reload_children()#

Refresh a lazy node’s children (drop and re-fetch via its loader).

remove()#

Remove this node (and its descendants) from the tree.