Skip to content

Commit

Permalink
Revert "DAS-2066: formatting_html.py merge"
Browse files Browse the repository at this point in the history
oopsies
This reverts commit d68ae3c.
  • Loading branch information
eni-awowale committed Apr 2, 2024
1 parent d68ae3c commit 9e60747
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 325 deletions.
131 changes: 0 additions & 131 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

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 Any

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

from xarray.core.options import OPTIONS

OPTIONS["display_expand_groups"] = "default"


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

return _obj_repr(ds, header_components, sections)


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

# Get result from node_repr and wrap it
lines_callback = lambda n, c, end: _wrap_repr(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_children,
max_items_collapse=1,
expand_option_name="display_expand_groups",
)


def node_repr(group_title: str, dt: Any) -> 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_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: Any) -> str:
obj_type = f"datatree.{type(dt).__name__}"
return node_repr(obj_type, dt)
194 changes: 0 additions & 194 deletions xarray/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import xarray as xr
from xarray.core import formatting_html as fh
from xarray.core.coordinates import Coordinates
from xarray.core.datatree import DataTree


@pytest.fixture
Expand Down Expand Up @@ -197,196 +196,3 @@ def test_nonstr_variable_repr_html() -> None:
html = v._repr_html_().strip()
assert "<dt><span>22 :</span></dt><dd>bar</dd>" in html
assert "<li><span>10</span>: 3</li></ul>" in html


@pytest.fixture(scope="module", params=["some html", "some other html"])
def repr(request):
return request.param


class Test_summarize_children:
"""
Unit tests for summarize_children.
"""

func = staticmethod(fh.summarize_children)

@pytest.fixture(scope="class")
def childfree_tree_factory(self):
"""
Fixture for a child-free DataTree factory.
"""
from random import randint

def _childfree_tree_factory():
return DataTree(
data=xr.Dataset({"z": ("y", [randint(1, 100) for _ in range(3)])})
)

return _childfree_tree_factory

@pytest.fixture(scope="class")
def childfree_tree(self, childfree_tree_factory):
"""
Fixture for a child-free DataTree.
"""
return childfree_tree_factory()

@pytest.fixture(scope="function")
def mock_node_repr(self, monkeypatch):
"""
Apply mocking for node_repr.
"""

def mock(group_title, dt):
"""
Mock with a simple result
"""
return group_title + " " + str(id(dt))

monkeypatch.setattr(fh, "node_repr", mock)

@pytest.fixture(scope="function")
def mock_wrap_repr(self, monkeypatch):
"""
Apply mocking for _wrap_repr.
"""

def mock(r, *, end, **kwargs):
"""
Mock by appending "end" or "not end".
"""
return r + " " + ("end" if end else "not end") + "//"

monkeypatch.setattr(fh, "_wrap_repr", mock)

def test_empty_mapping(self):
"""
Test with an empty mapping of children.
"""
children = {}
assert self.func(children) == (
"<div style='display: inline-grid; grid-template-columns: 100%'>" "</div>"
)

def test_one_child(self, childfree_tree, mock_wrap_repr, mock_node_repr):
"""
Test with one child.
Uses a mock of _wrap_repr and node_repr to essentially mock
the inline lambda function "lines_callback".
"""
# Create mapping of children
children = {"a": childfree_tree}

# Expect first line to be produced from the first child, and
# wrapped as the last child
first_line = f"a {id(children['a'])} end//"

assert self.func(children) == (
"<div style='display: inline-grid; grid-template-columns: 100%'>"
f"{first_line}"
"</div>"
)

def test_two_children(self, childfree_tree_factory, mock_wrap_repr, mock_node_repr):
"""
Test with two level deep children.
Uses a mock of _wrap_repr and node_repr to essentially mock
the inline lambda function "lines_callback".
"""

# Create mapping of children
children = {"a": childfree_tree_factory(), "b": childfree_tree_factory()}

# Expect first line to be produced from the first child, and
# wrapped as _not_ the last child
first_line = f"a {id(children['a'])} not end//"

# Expect second line to be produced from the second child, and
# wrapped as the last child
second_line = f"b {id(children['b'])} end//"

assert self.func(children) == (
"<div style='display: inline-grid; grid-template-columns: 100%'>"
f"{first_line}"
f"{second_line}"
"</div>"
)


class Test__wrap_repr:
"""
Unit tests for _wrap_repr.
"""

func = staticmethod(fh._wrap_repr)

def test_end(self, repr):
"""
Test with end=True.
"""
r = self.func(repr, end=True)
assert r == (
"<div style='display: inline-grid;'>"
"<div style='"
"grid-column-start: 1;"
"border-right: 0.2em solid;"
"border-color: var(--xr-border-color);"
"height: 1.2em;"
"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'>"
f"{repr}"
"</ul>"
"</div>"
"</div>"
)

def test_not_end(self, repr):
"""
Test with end=False.
"""
r = self.func(repr, end=False)
assert r == (
"<div style='display: inline-grid;'>"
"<div style='"
"grid-column-start: 1;"
"border-right: 0.2em solid;"
"border-color: var(--xr-border-color);"
"height: 100%;"
"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'>"
f"{repr}"
"</ul>"
"</div>"
"</div>"
)

0 comments on commit 9e60747

Please sign in to comment.