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.0.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.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anycomplextree-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 8f94dbb422a7328b8d6b61a0118a8a3c26617eb432ab32985dab3b2786deaa9e
MD5 82b641da65b174c7ce4f7720979a3d22
BLAKE2b-256 34bf6f3f13521173f63ae2c241f7387c79b313fcf224649c7cc1f2dcd1e986d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for anycomplextree-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: anycomplextree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f553087e0f2dac3d6c37a36b30fdc8397c91bac94e1d38b2a4577cfd75338a9
MD5 9c6b5f3d95b1816f31c81069bf0c9b99
BLAKE2b-256 314bcbe438c246d073f608196c2e0d7dfad4abc477a43f3e3e0c82577b3f9dee

See more details on using hashes here.

Provenance

The following attestation bundles were made for anycomplextree-0.1.0-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