ipytree
ipytree is a Python library that provides an interactive Tree Widget for Jupyter environments, built using the Jupyter-widgets protocol and jsTree. It offers an easy-to-use interface to visualize and manipulate hierarchical data structures directly within Jupyter Notebooks and JupyterLab, and can be seamlessly integrated with other `ipywidgets` components.
Common errors
-
Error displaying widget
cause The `ipytree` JupyterLab or Notebook extension is not properly installed or enabled, or there's a compatibility issue with the Jupyter frontend.fixFor JupyterLab 3+ and `ipywidgets >= 7.6.0`, reinstall `ipytree` to trigger automatic extension registration. For older environments, manually install/enable the extensions as described in the warnings. Ensure Python kernel is restarted. -
Uncaught DOMException: Failed to execute 'structuredClone' on 'Window': # could not be cloned.
cause This error specifically occurred with `ipywidgets` version 8.0.5, which introduced a change related to `structuredClone` that affected `ipytree` and other custom widgets.fixUpdate `ipywidgets` to version 8.0.6 or newer, or downgrade it to 8.0.4 or earlier.
Warnings
- breaking Users of `ipywidgets` version 8.0.5 may encounter issues with `ipytree` functionality due to changes in how widgets handle structured cloning (`structuredClone`). This affected default serializers.
- gotcha Older JupyterLab versions (<=2) and classic Jupyter Notebook (version 5.2 or below) required explicit manual installation and enabling of Jupyter extensions for `ipytree` to display correctly.
- gotcha When running `ipytree` in environments like VS Code's Jupyter integration or certain cloud notebooks (e.g., EMR), users may encounter 'Error displaying widget' if the frontend extension isn't correctly loaded or compatible.
Install
-
pip install ipytree -
conda install -c conda-forge ipytree
Imports
- Tree
from ipytree import Tree
- Node
from ipytree import Node
Quickstart
from ipytree import Tree, Node
# Create a root node
root_node = Node('Root')
# Add child nodes
child1 = Node('Child 1')
child2 = Node('Child 2')
root_node.add_node(child1)
root_node.add_node(child2)
# Add a nested node
sub_child = Node('Sub Child')
child1.add_node(sub_child)
# Create the tree widget and add the root node
tree = Tree(nodes=[root_node])
# Display the tree
tree