10000 Shorten text repr for ``DataTree`` by jsignell · Pull Request #10139 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content

Shorten text repr for DataTree #10139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Shorten text repr for DataTree
* only show max 12 children in text ``DataTree`` repr
* use ``set_options(display_max_rows=12)`` to configure this setting
* always include last item in ``DataTree``
* insert "..." before last item in ``DataTree``
  • Loading branch information
jsignell committed Mar 17, 2025
commit c34530b3b7cb36b70ff0b6c0995c73bf3c14534b
33 changes: 31 additions & 2 deletions xarray/core/datatree_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(
style=None,
childiter: type = list,
maxlevel: int | None = None,
maxchildren: int | None = None,
):
"""
Render tree starting at `node`.
Expand All @@ -88,6 +89,10 @@ def __init__(
Iterables that change the order of children cannot be used
(e.g., `reversed`).
maxlevel: Limit rendering to this depth.
maxchildren: Limit number of children to roughly this number. In practice,
for an arbitrarily large DataTree the number of children returned
will be (maxchildren * maxchildren - 1) / 2. The last child is also
included.
:any:`RenderDataTree` is an iterator, returning a tuple with 3 items:
`pre`
tree prefix.
Expand Down Expand Up @@ -160,6 +165,14 @@ def __init__(
root
├── sub0
└── sub1

# `maxchildren` roughly limits the total number of children

>>> print(RenderDataTree(root, maxchildren=3))
root
├── sub0
│ ├── sub0B
└── sub1
"""
if style is None:
style = ContStyle()
Expand All @@ -169,20 +182,36 @@ def __init__(
self.style = style
self.childiter = childiter
self.maxlevel = maxlevel
self.maxchildren = maxchildren

def __iter__(self) -> Iterator[Row]:
return self.__next(self.node, tuple())

def __next(
self, node: DataTree, continues: tuple[bool, ...], level: int = 0
self,
node: DataTree,
continues: tuple[bool, ...],
level: int = 0,
nchildren: int = 0,
) -> Iterator[Row]:
yield RenderDataTree.__item(node, continues, self.style)
children = node.children.values()
level += 1
if children and (self.maxlevel is None or level < self.maxlevel):
children = self.childiter(children)
for child, is_last in _is_last(children):
yield from self.__next(child, continues + (not is_last,), level=level)
nchildren += 1
if (
self.maxchildren is None
or nchildren < self.maxchildren
or (not any(continues) and is_last)
):
yield from self.__next(
child,
continues + (not is_last,),
level=level,
nchildren=nchildren,
)

@staticmethod
def __item(
Expand Down
14 changes: 12 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,14 +1137,24 @@ def _datatree_node_repr(node: DataTree, show_inherited: bool) -> str:

def datatree_repr(dt: DataTree) -> str:
"""A printable representation of the structure of this entire tree."""
renderer = RenderDataTree(dt)
max_rows = OPTIONS["display_max_rows"]

renderer = RenderDataTree(dt, maxchildren=max_rows)

name_info = "" if dt.name is None else f" {dt.name!r}"
header = f"<xarray.DataTree{name_info}>"

lines = [header]
show_inherited = True
for pre, fill, node in renderer:

rendered_items = list(renderer)
for i, (pre, fill, node) in enumerate(rendered_items):
if len(rendered_items) > max_rows:
if i == max_rows:
lines.append("...")
if i >= max_rows and i != (len(rendered_items) - 1):
continue

node_repr = _datatree_node_repr(node, show_inherited=show_inherited)
show_inherited = False # only show inherited coords on the root

Expand Down
Loading
0