Skip to content

indexes section in the HTML repr #7185

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 16 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ New Features

- Add static typing to plot accessors (:issue:`6949`, :pull:`7052`).
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Display the indexes in a new section of the text and HTML reprs
(:pull:`6795`, :pull:`7183`, :pull:`7185`)
By `Justus Magin <https://github.com/keewis>`_ and `Benoît Bovy <https://github.com/benbovy>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
54 changes: 53 additions & 1 deletion xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from html import escape
from importlib.resources import read_binary

from .formatting import inline_variable_array_repr, short_data_repr
from .formatting import inline_index_repr, inline_variable_array_repr, short_data_repr
from .options import _get_boolean_with_default

STATIC_FILES = (
Expand Down Expand Up @@ -125,6 +125,40 @@ def summarize_vars(variables):
return f"<ul class='xr-var-list'>{vars_li}</ul>"


def short_index_repr_html(index):
if hasattr(index, "_repr_html_"):
return index._repr_html_()

return f"<pre>{escape(repr(index))}</pre>"


def summarize_index(coord_names, index):
name = "<br>".join([escape(str(n)) for n in coord_names])

index_id = f"index-{uuid.uuid4()}"
preview = escape(inline_index_repr(index))
details = short_index_repr_html(index)

data_icon = _icon("icon-database")

return (
f"<div class='xr-index-name'><div>{name}</div></div>"
f"<div class='xr-index-preview'>{preview}</div>"
f"<div></div>"
f"<input id='{index_id}' class='xr-index-data-in' type='checkbox'/>"
f"<label for='{index_id}' title='Show/Hide index repr'>{data_icon}</label>"
f"<div class='xr-index-data'>{details}</div>"
)


def summarize_indexes(indexes):
indexes_li = "".join(
f"<li class='xr-var-item'>{summarize_index(v, i)}</li>"
for v, i in indexes.items()
)
return f"<ul class='xr-var-list'>{indexes_li}</ul>"


def collapsible_section(
name, inline_details="", details="", n_items=None, enabled=True, collapsed=False
):
Expand Down Expand Up @@ -213,6 +247,13 @@ def array_section(obj):
expand_option_name="display_expand_data_vars",
)

index_section = partial(
_mapping_section,
name="Indexes",
details_func=summarize_indexes,
max_items_collapse=0,
expand_option_name="display_expand_indexes",
)

attr_section = partial(
_mapping_section,
Expand All @@ -223,6 +264,12 @@ def array_section(obj):
)


def _get_indexes_dict(indexes):
return {
tuple(index_vars.keys()): idx for idx, index_vars in indexes.group_by_index()
}


def _obj_repr(obj, header_components, sections):
"""Return HTML repr of an xarray object.

Expand Down Expand Up @@ -266,6 +313,10 @@ def array_repr(arr):
if hasattr(arr, "coords"):
sections.append(coord_section(arr.coords))

if hasattr(arr, "xindexes"):
indexes = _get_indexes_dict(arr.xindexes)
sections.append(index_section(indexes))

sections.append(attr_section(arr.attrs))

return _obj_repr(arr, header_components, sections)
Expand All @@ -280,6 +331,7 @@ def dataset_repr(ds):
dim_section(ds),
coord_section(ds.coords),
datavar_section(ds.data_vars),
index_section(_get_indexes_dict(ds.xindexes)),
attr_section(ds.attrs),
]

Expand Down
15 changes: 11 additions & 4 deletions xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,34 +169,41 @@ class set_options:
Colormap to use for nondivergent data plots. If string, must be
matplotlib built-in colormap. Can also be a Colormap object
(e.g. mpl.cm.magma)
display_expand_attrs : {"default", True, False}:
display_expand_attrs : {"default", True, False}
Whether to expand the attributes section for display of
``DataArray`` or ``Dataset`` objects. Can be

* ``True`` : to always expand attrs
* ``False`` : to always collapse attrs
* ``default`` : to expand unless over a pre-defined limit
display_expand_coords : {"default", True, False}:
display_expand_coords : {"default", True, False}
Whether to expand the coordinates section for display of
``DataArray`` or ``Dataset`` objects. Can be

* ``True`` : to always expand coordinates
* ``False`` : to always collapse coordinates
* ``default`` : to expand unless over a pre-defined limit
display_expand_data : {"default", True, False}:
display_expand_data : {"default", True, False}
Whether to expand the data section for display of ``DataArray``
objects. Can be

* ``True`` : to always expand data
* ``False`` : to always collapse data
* ``default`` : to expand unless over a pre-defined limit
display_expand_data_vars : {"default", True, False}:
display_expand_data_vars : {"default", True, False}
Whether to expand the data variables section for display of
``Dataset`` objects. Can be

* ``True`` : to always expand data variables
* ``False`` : to always collapse data variables
* ``default`` : to expand unless over a pre-defined limit
display_expand_indexes : {"default", True, False}
Whether to expand the indexes section for display of
``DataArray`` or ``Dataset``. Can be

* ``True`` : to always expand indexes
* ``False`` : to always collapse indexes
* ``default`` : to expand unless over a pre-defined limit (always collapse for html style)
display_max_rows : int, default: 12
Maximum display rows.
display_values_threshold : int, default: 200
Expand Down
19 changes: 15 additions & 4 deletions xarray/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ body.vscode-dark {
grid-column: 4;
}

.xr-index-preview {
grid-column: 2 / 5;
color: var(--xr-font-color2);
}

.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
Expand All @@ -265,14 +270,16 @@ body.vscode-dark {
}

.xr-var-attrs,
.xr-var-data {
.xr-var-data,
.xr-index-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}

.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data {
.xr-var-data-in:checked ~ .xr-var-data,
.xr-index-data-in:checked ~ .xr-index-data {
display: block;
}

Expand All @@ -282,13 +289,16 @@ body.vscode-dark {

.xr-var-name span,
.xr-var-data,
.xr-index-name div,
.xr-index-data,
.xr-attrs {
padding-left: 25px !important;
}

.xr-attrs,
.xr-var-attrs,
.xr-var-data {
.xr-var-data,
.xr-index-data {
grid-column: 1 / -1;
}

Expand Down Expand Up @@ -326,7 +336,8 @@ dl.xr-attrs {
}

.xr-icon-database,
.xr-icon-file-text2 {
.xr-icon-file-text2,
.xr-no-icon {
display: inline-block;
vertical-align: middle;
width: 1em;
Expand Down
17 changes: 10 additions & 7 deletions xarray/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ def test_repr_of_dataarray(dataarray) -> None:
assert "dim_0" in formatted
# has an expanded data section
assert formatted.count("class='xr-array-in' type='checkbox' checked>") == 1
# coords and attrs don't have an items so they'll be be disabled and collapsed
# coords, indexes and attrs don't have an items so they'll be be disabled and collapsed
assert (
formatted.count("class='xr-section-summary-in' type='checkbox' disabled >") == 2
formatted.count("class='xr-section-summary-in' type='checkbox' disabled >") == 3
)

with xr.set_options(display_expand_data=False):
formatted = fh.array_repr(dataarray)
assert "dim_0" in formatted
# has an expanded data section
# has a collapsed data section
assert formatted.count("class='xr-array-in' type='checkbox' checked>") == 0
# coords and attrs don't have an items so they'll be be disabled and collapsed
# coords, indexes and attrs don't have an items so they'll be be disabled and collapsed
assert (
formatted.count("class='xr-section-summary-in' type='checkbox' disabled >")
== 2
== 3
)


Expand All @@ -130,19 +130,22 @@ def test_repr_of_dataset(dataset) -> None:
assert (
formatted.count("class='xr-section-summary-in' type='checkbox' checked>") == 3
)
# indexes is collapsed
assert formatted.count("class='xr-section-summary-in' type='checkbox' >") == 1
assert "&lt;U4" in formatted or "&gt;U4" in formatted
assert "&lt;IA&gt;" in formatted

with xr.set_options(
display_expand_coords=False,
display_expand_data_vars=False,
display_expand_attrs=False,
display_expand_indexes=True,
):
formatted = fh.dataset_repr(dataset)
# coords, attrs, and data_vars are collapsed
# coords, attrs, and data_vars are collapsed, indexes is expanded
assert (
formatted.count("class='xr-section-summary-in' type='checkbox' checked>")
== 0
== 1
)
assert "&lt;U4" in formatted or "&gt;U4" in formatted
assert "&lt;IA&gt;" in formatted
Expand Down