Skip to content
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

Migrate formatting_html.py into xarray core #8930

Merged
merged 14 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
6 changes: 3 additions & 3 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from xarray.core.coordinates import DatasetCoordinates
from xarray.core.dataarray import DataArray
from xarray.core.dataset import Dataset, DataVariables
from xarray.core.formatting_html import (
datatree_repr as datatree_repr_html,
)
from xarray.core.indexes import Index, Indexes
from xarray.core.merge import dataset_update_method
from xarray.core.options import OPTIONS as XR_OPTS
Expand All @@ -33,9 +36,6 @@
from xarray.core.variable import Variable
from xarray.datatree_.datatree.common import TreeAttrAccessMixin
from xarray.datatree_.datatree.formatting import datatree_repr
from xarray.datatree_.datatree.formatting_html import (
datatree_repr as datatree_repr_html,
)
from xarray.datatree_.datatree.mapping import (
TreeIsomorphismError,
check_isomorphic,
Expand Down
132 changes: 132 additions & 0 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import uuid
from collections import OrderedDict
from collections.abc import Mapping
from functools import lru_cache, partial
from html import escape
from importlib.resources import files
from typing import TYPE_CHECKING

from xarray.core.formatting import (
inline_index_repr,
Expand All @@ -18,6 +20,9 @@
("xarray.static.css", "style.css"),
)

if TYPE_CHECKING:
from xarray.core.datatree import DataTree


@lru_cache(None)
def _load_static_files():
Expand Down Expand Up @@ -341,3 +346,130 @@ def dataset_repr(ds) -> str:
]

return _obj_repr(ds, header_components, sections)


def summarize_datatree_children(children: Mapping[str, DataTree]) -> str:
N_CHILDREN = len(children) - 1

# Get result from datatree_node_repr and wrap it
lines_callback = lambda n, c, end: _wrap_datatree_repr(
datatree_node_repr(n, c), end=end
)

children_html = "".join(
(
lines_callback(n, c, end=False) # Long lines
if i < N_CHILDREN
else lines_callback(n, c, end=True)
) # Short lines
for i, (n, c) in enumerate(children.items())
)

return "".join(
[
"<div style='display: inline-grid; grid-template-columns: 100%'>",
children_html,
"</div>",
]
)


children_section = partial(
_mapping_section,
name="Groups",
details_func=summarize_datatree_children,
max_items_collapse=1,
expand_option_name="display_expand_groups",
)


def datatree_node_repr(group_title: str, dt: DataTree) -> str:
header_components = [f"<div class='xr-obj-type'>{escape(group_title)}</div>"]

ds = dt.ds

sections = [
children_section(dt.children),
dim_section(ds),
coord_section(ds.coords),
datavar_section(ds.data_vars),
attr_section(ds.attrs),
]

return _obj_repr(ds, header_components, sections)


def _wrap_datatree_repr(r: str, end: bool = False) -> str:
"""
Wrap HTML representation with a tee to the left of it.

Enclosing HTML tag is a <div> with :code:`display: inline-grid` style.

Turns:
[ title ]
| details |
|_____________|

into (A):
|─ [ title ]
| | details |
| |_____________|

or (B):
└─ [ title ]
| details |
|_____________|

Parameters
----------
r: str
HTML representation to wrap.
end: bool
Specify if the line on the left should continue or end.

Default is True.

Returns
-------
str
Wrapped HTML representation.

Tee color is set to the variable :code:`--xr-border-color`.
"""
# height of line
end = bool(end)
height = "100%" if end is False else "1.2em"
return "".join(
[
"<div style='display: inline-grid;'>",
"<div style='",
"grid-column-start: 1;",
"border-right: 0.2em solid;",
"border-color: var(--xr-border-color);",
f"height: {height};",
"width: 0px;",
"'>",
"</div>",
"<div style='",
"grid-column-start: 2;",
"grid-row-start: 1;",
"height: 1em;",
"width: 20px;",
"border-bottom: 0.2em solid;",
"border-color: var(--xr-border-color);",
"'>",
"</div>",
"<div style='",
"grid-column-start: 3;",
"'>",
"<ul class='xr-sections'>",
r,
"</ul>" "</div>",
"</div>",
]
)


def datatree_repr(dt: DataTree) -> str:
obj_type = f"datatree.{type(dt).__name__}"
return datatree_node_repr(obj_type, dt)
135 changes: 0 additions & 135 deletions xarray/datatree_/datatree/formatting_html.py

This file was deleted.

Loading
Loading