Skip to content

Commit

Permalink
Migrate formatting_html.py into xarray core (#8930)
Browse files Browse the repository at this point in the history
* DAS-2066: formatting_html.py merge

* Revert "DAS-2066: formatting_html.py merge"
oopsies
This reverts commit d68ae3c.

* Adding datatree functions to core

* Deleted formatting_html.py and test_formatting_html.py in datatree_

* renamed all of the relevant functions so they represent datatree

* added comment for mypy

* added dict[any, any] as type hint for children

* added dict[Any, Any] as type hint for children

* updated typhint to dict[str, DataTree] and increased pixel min width to 700

* replaced Any with DataTree

* added type checking

* DAS-2066 - Incorporate CSS changes for DataTree display.

* DAS-2066 - Fix tests.

---------

Co-authored-by: Owen Littlejohns <[email protected]>
Co-authored-by: Matt Savoie <[email protected]>
  • Loading branch information
3 people authored Apr 18, 2024
1 parent 9eb180b commit 5f24079
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 336 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Bug fixes

Internal Changes
~~~~~~~~~~~~~~~~
- Migrates ``formatting_html`` functionality for `DataTree` into ``xarray/core`` (:pull: `8930`)
By `Eni Awowale <https://github.com/eni-awowale>`_, `Julia Signell <https://github.com/jsignell>`_
and `Tom Nicholas <https://github.com/TomNicholas>`_.
- Migrates ``datatree_mapping`` functionality into ``xarray/core`` (:pull:`8948`)
By `Matt Savoie <https://github.com/flamingbear>`_ `Owen Littlejohns
<https://github.com/owenlittlejohns>` and `Tom Nicholas <https://github.com/TomNicholas>`_.
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
check_isomorphic,
map_over_subtree,
)
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 @@ -38,9 +41,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.ops import (
DataTreeArithmeticMixin,
MappedDatasetMethodsMixin,
Expand Down
131 changes: 131 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,129 @@ 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%; grid-column: 1 / -1'>",
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; grid-template-columns: 0px 20px auto; width: 100%;'>",
"<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;",
"'>",
r,
"</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

0 comments on commit 5f24079

Please sign in to comment.