Skip to content

Remove rich as required for tree()#3778

Open
ianhi wants to merge 3 commits intozarr-developers:mainfrom
ianhi:ian/simple-tree
Open

Remove rich as required for tree()#3778
ianhi wants to merge 3 commits intozarr-developers:mainfrom
ianhi:ian/simple-tree

Conversation

@ianhi
Copy link
Contributor

@ianhi ianhi commented Mar 13, 2026

Description

I often found that when I wanted to use tree() rich wasn't installed. So I started out with this as a fallback but on looking into it I think it can be fully removed in favor a small loop (which we would need anyway for a fallback) to construct the tree and just add in ansi or html bolding codes.

One thing I noticed when looking at this is that there is currently no truncation for extremely large hierarchies. I think that is bad for two reasons:

  1. can massively overflow the users screen
  2. Potentially bad performance doing the IO to get the full hierarchy.

So I've added a simplistic truncation. Note that this can result in nodes that are displayed having missing children.

I also added a plain flag to give the user explicit control on the bolding html/ansi sequences.

Output

Per the tests the output is equivalent, and also looks so to my eyes.

To compare here is a script (runnable with third-wheel)

uvx third-wheel@0.3.0 run compare_trees.py

compare_trees.py
# /// script
# dependencies = [
#   "zarr_old",
#   "rich",
#   "zarr @ git+https://github.com/ianhi/zarr-python@ian/simple-tree",
# ]
# [tool.third-wheel]
# renames = [
#   {original = "zarr", new-name = "zarr_old", extras = ["optional"]},
# ]
# ///

"""Compare the old rich-based tree with the new plain tree."""

import zarr
import zarr_old

def make_group(mod):
    g = mod.group()
    g.create_group("A")
    g.create_group("B")
    g.create_group("B/C")
    g.create_group("B/C/D")
    return g

print("=== Old (rich-based) tree ===")
g_old = make_group(zarr_old)
print(repr(g_old.tree()))

print("=== New tree (styled) ===")
g_new = make_group(zarr)
print(repr(g_new.tree()))

print("=== New tree (plain) ===")
print(repr(g_new.tree(plain=True)))
image

or in jupyter:

image

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.md
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant