From 8a591c6b5f55572e83bbfd358462e3c20bfba642 Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Wed, 15 Jul 2020 20:26:40 -0600 Subject: [PATCH 1/6] provide set_option `collapse_html` to control HTML repr collapsed state --- xarray/core/formatting_html.py | 3 ++- xarray/core/options.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index 400ef61502e..a3c989026e5 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -2,6 +2,7 @@ from collections import OrderedDict from functools import partial from html import escape +from .options import OPTIONS import pkg_resources @@ -184,7 +185,7 @@ def dim_section(obj): def array_section(obj): # "unique" id to expand/collapse the section data_id = "section-" + str(uuid.uuid4()) - collapsed = "checked" + collapsed = "" if OPTIONS["collapse_html"] else "checked" variable = getattr(obj, "variable", obj) preview = escape(inline_variable_array_repr(variable, max_width=70)) data_repr = short_data_repr_html(obj) diff --git a/xarray/core/options.py b/xarray/core/options.py index 5d81ca40a6e..4626681db38 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -9,6 +9,7 @@ CMAP_DIVERGENT = "cmap_divergent" KEEP_ATTRS = "keep_attrs" DISPLAY_STYLE = "display_style" +COLLAPSE_HTML = "collapse_html" OPTIONS = { @@ -21,6 +22,7 @@ CMAP_DIVERGENT: "RdBu_r", KEEP_ATTRS: "default", DISPLAY_STYLE: "html", + COLLAPSE_HTML: True, } _JOIN_OPTIONS = frozenset(["inner", "outer", "left", "right", "exact"]) @@ -39,6 +41,7 @@ def _positive_integer(value): WARN_FOR_UNCLOSED_FILES: lambda value: isinstance(value, bool), KEEP_ATTRS: lambda choice: choice in [True, False, "default"], DISPLAY_STYLE: _DISPLAY_OPTIONS.__contains__, + COLLAPSE_HTML: lambda value: isinstance(value, bool), } @@ -104,6 +107,8 @@ class set_options: Default: ``'default'``. - ``display_style``: display style to use in jupyter for xarray objects. Default: ``'text'``. Other options are ``'html'``. + - ``collapse_html``: collapse the data part of the html representation. + Default: ``False``. You can use ``set_options`` either as a context manager: From 6a5c475c6cb453d5c403cd68575db20cf2724501 Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Wed, 15 Jul 2020 20:38:03 -0600 Subject: [PATCH 2/6] fix isort --- xarray/core/formatting_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index a3c989026e5..3fcb7750be5 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -2,11 +2,11 @@ from collections import OrderedDict from functools import partial from html import escape -from .options import OPTIONS import pkg_resources from .formatting import inline_variable_array_repr, short_data_repr +from .options import OPTIONS CSS_FILE_PATH = "/".join(("static", "css", "style.css")) CSS_STYLE = pkg_resources.resource_string("xarray", CSS_FILE_PATH).decode("utf8") From cb179a329017a464c32e637085399befa87dcbfc Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Fri, 31 Jul 2020 19:44:06 -0600 Subject: [PATCH 3/6] fix: change default to match existing behavior --- xarray/core/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 4626681db38..d804cd51e44 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -22,7 +22,7 @@ CMAP_DIVERGENT: "RdBu_r", KEEP_ATTRS: "default", DISPLAY_STYLE: "html", - COLLAPSE_HTML: True, + COLLAPSE_HTML: False, } _JOIN_OPTIONS = frozenset(["inner", "outer", "left", "right", "exact"]) From e5b6246922f73981e26b2812e68dfb38fe7d26b3 Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Sat, 1 Aug 2020 01:23:41 -0600 Subject: [PATCH 4/6] fix: fix tests for collapse_html option --- xarray/tests/test_formatting_html.py | 13 +++++++++---- xarray/tests/test_options.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_formatting_html.py b/xarray/tests/test_formatting_html.py index 9a210ad6fa3..f1fcc3c059e 100644 --- a/xarray/tests/test_formatting_html.py +++ b/xarray/tests/test_formatting_html.py @@ -106,10 +106,15 @@ def test_summarize_attrs_with_unsafe_attr_name_and_value(): def test_repr_of_dataarray(dataarray): - formatted = fh.array_repr(dataarray) - assert "dim_0" in formatted - # has an expanded data section - assert formatted.count("class='xr-array-in' type='checkbox' checked>") == 1 + # has an expanded data section for COLLAPSE_HTML = False + with xr.set_options(collapse_html=False): + formatted = fh.array_repr(dataarray) + assert "dim_0" in formatted + assert formatted.count("class='xr-array-in' type='checkbox' checked>") == 1 + with xr.set_options(collapse_html=True): + formatted = fh.array_repr(dataarray) + assert "dim_0" in formatted + 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 assert ( formatted.count("class='xr-section-summary-in' type='checkbox' disabled >") == 2 diff --git a/xarray/tests/test_options.py b/xarray/tests/test_options.py index 19f74476ced..d947f04fadf 100644 --- a/xarray/tests/test_options.py +++ b/xarray/tests/test_options.py @@ -77,6 +77,16 @@ def test_display_style(): assert OPTIONS["display_style"] == original +def test_collapse_html(): + original = False + assert OPTIONS["collapse_html"] == original + with pytest.raises(ValueError): + xarray.set_options(collapse_html="invalid") + with xarray.set_options(collapse_html=True): + assert OPTIONS["collapse_html"] == True + assert OPTIONS["collapse_html"] == original + + def create_test_dataset_attrs(seed=0): ds = create_test_data(seed) ds.attrs = {"attr1": 5, "attr2": "history", "attr3": {"nested": "more_info"}} From 33e40cfb08238d3e174082deb406fe654d46b328 Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Sat, 1 Aug 2020 01:33:11 -0600 Subject: [PATCH 5/6] docs: add "new feature" entry for new collapse_html option --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index d086d4f411d..4b9cb0878db 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -26,6 +26,8 @@ Breaking changes New Features ~~~~~~~~~~~~ +- new boolean option `collapse_html` to control if the HTML display for a DataArray + is collapsed when first shown. Default: False Bug fixes ~~~~~~~~~ From a1082da8ce70e80f0ad2e8dac73639834637b95c Mon Sep 17 00:00:00 2001 From: Michael Aye Date: Sat, 1 Aug 2020 01:35:27 -0600 Subject: [PATCH 6/6] style: fix PEP8 --- xarray/tests/test_options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_options.py b/xarray/tests/test_options.py index d947f04fadf..bf6507dbbbd 100644 --- a/xarray/tests/test_options.py +++ b/xarray/tests/test_options.py @@ -83,7 +83,7 @@ def test_collapse_html(): with pytest.raises(ValueError): xarray.set_options(collapse_html="invalid") with xarray.set_options(collapse_html=True): - assert OPTIONS["collapse_html"] == True + assert OPTIONS["collapse_html"] is True assert OPTIONS["collapse_html"] == original