Skip to main content

An anywidget wrapping react-complex-tree

Project description

anycomplextree

An anywidget that wraps react-complex-tree, giving you an interactive tree view inside Jupyter / JupyterLab / VS Code / Marimo notebooks with two-way state sync to Python.

Features

  • Hierarchical tree rendering powered by react-complex-tree
  • Two-way sync for selection, expansion, and focus
  • Python-side validation of tree structure with clear error messages
  • Single bundled ESM asset — no CDN fetches at runtime

Installation

uv add anycomplextree

Quick start

from anycomplextree import ComplexTree

tree = ComplexTree(
    items={
        "root": {
            "isFolder": True,
            "children": ["fruits", "meals"],
            "data": "Root",
        },
        "fruits": {
            "isFolder": True,
            "children": ["apple", "orange", "banana"],
            "data": "Fruits",
        },
        "apple":  {"data": "Apple"},
        "orange": {"data": "Orange"},
        "banana": {"data": "Banana"},
        "meals": {
            "isFolder": True,
            "children": ["pasta", "pizza"],
            "data": "Meals",
        },
        "pasta": {"data": "Pasta"},
        "pizza": {"data": "Pizza"},
    },
    root_item="root",
)
tree

The items model

Each entry in items represents one node. The dict key is the node's identifier — react-complex-tree calls it index. The key, the optional index field on the item, and the entries inside parent children arrays must all be the same string.

field type description
index str Unique identifier. Auto-filled from the dict key if omitted.
data Any Per-item payload. By default used as the display title.
isFolder bool Whether the item can have children.
children list[str] IDs of child items (must exist as keys in items).

Using UUIDs

Since the dict key is the identifier, just use UUID strings as keys:

import uuid
apple_id = str(uuid.uuid4())
fruits_id = str(uuid.uuid4())

tree = ComplexTree(
    items={
        fruits_id: {"isFolder": True, "children": [apple_id], "data": "Fruits"},
        apple_id:  {"data": "Apple"},
        "root":    {"isFolder": True, "children": [fruits_id], "data": "Root"},
    },
)

tree.selected_items  # -> [apple_id] after you click Apple

Traits

All traits are two-way synced between Python and the browser.

trait type description
items dict The tree contents (see above).
root_item str Key of the root node. Defaults to "root".
selected_items list[str] Currently selected item IDs.
expanded_items list[str] Currently expanded folder IDs.
focused_item str | None Currently focused item ID.

Read, write, or observe them like any traitlet:

tree.expanded_items = ["root", "fruits"]
tree.selected_items = ["apple"]
tree.focused_item   = "apple"

tree.observe(lambda change: print("selection:", change["new"]), names="selected_items")

Note: traitlets only fires change events when the trait is reassigned, not when mutated in place. Use tree.selected_items = [...], not tree.selected_items.append(...).

Validation

items and root_item are validated on assignment. You'll get a TraitError with a clear message for:

  • A dict key that doesn't match an explicit index field
  • A children entry that doesn't exist in items
  • A root_item that isn't a key in items

Development

The widget bundle lives in js/ and builds to anycomplextree/static/.

# one-time install
cd js && npm install

# rebuild after JS changes
npm run build

# watch mode during development
npm run dev

On the Python side:

uv sync
uv run jupyter lab

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anycomplextree-0.1.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anycomplextree-0.1.1-py3-none-any.whl (68.0 kB view details)

Uploaded Python 3

File details

Details for the file anycomplextree-0.1.1.tar.gz.

File metadata

  • Download URL: anycomplextree-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for anycomplextree-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6673d81c81341aa7f746d0028da06f95a4fa611881363cab1bcf9ee5bdb47022
MD5 95fd61cdd38622a4cfb191bc3bb9784a
BLAKE2b-256 f8077df972a277adff5ce9bbce7a70a1ef3787daf6e407d6f6ea042acb1d7224

See more details on using hashes here.

Provenance

The following attestation bundles were made for anycomplextree-0.1.1.tar.gz:

Publisher: release.yml on mluttikh/anycomplextree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file anycomplextree-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: anycomplextree-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 68.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for anycomplextree-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5e5e640cdf26198612bcf5fb4434050e43bf47d86818d8f19733410d8e91dd9b
MD5 5f8233aa6fb0536d526ff5eec443bfd7
BLAKE2b-256 a49949a26b1986280a3af9e6ebad1a04096719b82ec7b144e8441530d96c141e

See more details on using hashes here.

Provenance

The following attestation bundles were made for anycomplextree-0.1.1-py3-none-any.whl:

Publisher: release.yml on mluttikh/anycomplextree

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page