stratify_by_parent
stratify_by_parent(items, parent_ids)Convert flat data to hierarchical tree data via parent-child relationships.
Takes a list of TreeItem objects where parent-child relationships are expressed through a separate list of parent_ids, and returns tree data where the parent-child relationships are expressed through the TreeItem children attribute.
Parameters
items :list[TreeItem]- 
List of TreeItem objects with empty children lists
 parent_ids :list[Optional[str]]- 
List of parent IDs corresponding to each TreeItem. None indicates a root item. Must be the same length as items list.
 
Returns
:list[TreeItem]- 
List of root TreeItem objects with populated children attributes. All original attributes are preserved.
 
Raises
:ValueError- 
If items and parent_ids lists have different lengths. If a parent_id references a non-existent item. If circular references are detected.
 
Examples
from shiny_treeview import TreeItem, stratify_by_parent
# Flat data with parent-child relationships
items = [
    TreeItem(id="root", label="Root"),
    TreeItem(id="child1", label="Child 1"),
    TreeItem(id="child2", label="Child 2"),
    TreeItem(id="grandchild", label="Grandchild")
]
parent_ids = [None, "root", "root", "child1"]
# Convert to hierarchical structure
tree = stratify_by_parent(items, parent_ids)
# Result: root item with child1 and child2 as children,
# and grandchild as a child of child1