Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Breaking changes
restore the prior defaults (:issue:`10657`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- The HTML repr for :py:class:`DataTree` has been tweaked to hide empty
sections, and automatically expand sub-groups (:pull:`10785`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

Deprecations
~~~~~~~~~~~~

Expand Down
30 changes: 19 additions & 11 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ def summarize_datatree_children(children: Mapping[str, DataTree]) -> str:
_mapping_section,
name="Groups",
details_func=summarize_datatree_children,
max_items_collapse=1,
max_option_name="display_max_children",
expand_option_name="display_expand_groups",
)
Expand Down Expand Up @@ -416,20 +415,29 @@ def datatree_node_repr(group_title: str, node: DataTree, show_inherited=False) -
indexes=inherited_vars(node._indexes),
)

sections = [
children_section(node.children),
dim_section(ds),
coord_section(node_coords),
]
sections = []

if node.children:
children_max_items = 1 if ds.data_vars else 6
sections.append(
children_section(node.children, max_items_collapse=children_max_items)
)

if ds.dims:
sections.append(dim_section(ds))

if node_coords:
sections.append(coord_section(node_coords))

# only show inherited coordinates on the root
if show_inherited:
if show_inherited and inherited_coords:
sections.append(inherited_coord_section(inherited_coords))

sections += [
datavar_section(ds.data_vars),
attr_section(ds.attrs),
]
if ds.data_vars:
sections.append(datavar_section(ds.data_vars))

if ds.attrs:
sections.append(attr_section(ds.attrs))

return _obj_repr(ds, header_components, sections)

Expand Down
6 changes: 3 additions & 3 deletions xarray/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ class TestDataTreeInheritance:
def test_inherited_section_present(self) -> None:
dt = xr.DataTree.from_dict(
{
"/": None,
"a": None,
"/": xr.Dataset(coords={"x": [1]}),
"child": None,
}
)
with xr.set_options(display_style="html"):
html = dt._repr_html_().strip()
html = dt["child"]._repr_html_().strip()
# checks that the section appears somewhere
assert "Inherited coordinates" in html

Expand Down
Loading