Skip to content

Commit 1878f9f

Browse files
committed
fixup
1 parent f844ce6 commit 1878f9f

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ extra = [
100100
optional = [
101101
'lmdb',
102102
'universal-pathlib>=0.0.22',
103+
'rich'
103104
]
104105
tree = [
105-
"rich",
106+
'rich',
106107
]
107108

108109
[project.urls]

src/zarr/_tree.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
from typing import Any
23

34
from zarr.core.group import AsyncGroup
45

@@ -15,13 +16,19 @@ def __init__(self, tree: rich.tree.Tree) -> None:
1516
self.tree = tree
1617

1718
def __repr__(self) -> str:
18-
console = rich.console.Console(file=io.StringIO())
19+
terminal = rich.get_console()
20+
console = rich.console.Console(file=io.StringIO(), color_system=terminal.color_system)
1921
console.print(self.tree)
2022
return str(console.file.getvalue())
2123

24+
def _repr_mimebundle_(self, **kwargs: dict[str, Any]) -> dict[str, str]:
25+
# For jupyter support.
26+
# We don't depend on jupyter, so we can't do the static types appropriately here.
27+
return self.tree._repr_mimebundle_(**kwargs) # type: ignore[no-any-return]
28+
2229

2330
async def group_tree_async(group: AsyncGroup, max_depth: int | None = None) -> TreeRepr:
24-
tree = rich.tree.Tree(label=f"[b]{group.name}[/b]")
31+
tree = rich.tree.Tree(label=f"[bold]{group.name}[/bold]")
2532
nodes = {"": tree}
2633
members = sorted([x async for x in group.members(max_depth=max_depth)])
2734

@@ -36,9 +43,9 @@ async def group_tree_async(group: AsyncGroup, max_depth: int | None = None) -> T
3643
# /'s and path segments. But node.name includes all that, so we build it here.
3744
name = key.rsplit("/")[-1]
3845
if isinstance(node, AsyncGroup):
39-
label = f"[b]{name}[/b]"
46+
label = f"[bold]{name}[/bold]"
4047
else:
41-
label = f"[b]{name}[/b] {node.shape} {node.dtype}"
48+
label = f"[bold]{name}[/bold] {node.shape} {node.dtype}"
4249
nodes[key] = parent.add(label)
4350

4451
return TreeRepr(tree)

tests/test_tree.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import zarr
77

8+
pytest.importorskip("rich")
9+
810

911
@pytest.mark.parametrize("root_name", [None, "root"])
1012
def test_tree(root_name: Any) -> None:
@@ -23,26 +25,29 @@ def test_tree(root_name: Any) -> None:
2325
result = repr(g.tree())
2426
root = root_name or ""
2527

28+
BOPEN = "\x1b[1m"
29+
BCLOSE = "\x1b[0m"
30+
2631
expected = textwrap.dedent(f"""\
27-
/{root}
28-
├── A
29-
│ ├── x (2,) float64
30-
│ └── y (0,) int8
31-
└── B
32-
├── C
33-
│ ├── C
34-
│ │ └── x (0,) float64
35-
│ └── x (0,) float64
36-
└── x (0,) float64
32+
{BOPEN}/{root}{BCLOSE}
33+
├── {BOPEN}A{BCLOSE}
34+
│ ├── {BOPEN}x{BCLOSE} (2,) float64
35+
│ └── {BOPEN}y{BCLOSE} (0,) int8
36+
└── {BOPEN}B{BCLOSE}
37+
├── {BOPEN}C{BCLOSE}
38+
│ ├── {BOPEN}C{BCLOSE}
39+
│ │ └── {BOPEN}x{BCLOSE} (0,) float64
40+
│ └── {BOPEN}x{BCLOSE} (0,) float64
41+
└── {BOPEN}x{BCLOSE} (0,) float64
3742
""")
3843

3944
assert result == expected
4045

4146
result = repr(g.tree(level=0))
4247
expected = textwrap.dedent(f"""\
43-
/{root}
44-
├── A
45-
└── B
48+
{BOPEN}/{root}{BCLOSE}
49+
├── {BOPEN}A{BCLOSE}
50+
└── {BOPEN}B{BCLOSE}
4651
""")
4752

4853
assert result == expected

0 commit comments

Comments
 (0)