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 5 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
9 changes: 9 additions & 0 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ def coords_repr(coords, col_width=None, max_rows=None):
)


def inline_index_repr(index, max_width=None):
if hasattr(index, "_repr_inline_"):
repr_ = index._repr_inline_(max_width=max_width)
else:
repr_ = repr(index)

return repr_


def summarize_index(
name: Hashable, index, col_width: int, max_width: int = None, is_index: bool = False
):
Expand Down
52 changes: 51 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 xr-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=15,
expand_option_name="display_expand_indexes",
)

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


def _get_indexes_dict(indexes):
idx_dict = {}
for idx, index_vars in indexes.group_by_index():
idx_dict[tuple(index_vars)] = idx

return idx_dict


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

Expand Down Expand Up @@ -280,6 +329,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
3 changes: 3 additions & 0 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _copy(self, deep: bool = True, memo: dict[int, Any] | None = None) -> Index:
def __getitem__(self, indexer: Any):
raise NotImplementedError()

def _repr_inline_(self, max_width):
return self.__class__.__name__


def _maybe_cast_to_cftimeindex(index: pd.Index) -> pd.Index:
from ..coding.cftimeindex import CFTimeIndex
Expand Down
18 changes: 14 additions & 4 deletions xarray/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ body.vscode-dark {
grid-column: 4;
}

.xr-index-preview {
grid-column: 2 / 5;
}

.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
Expand All @@ -265,14 +269,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 +288,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 +335,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