From 44710e6ca43bd6e49e5e628ba078a748292e4ab4 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Wed, 15 Apr 2020 14:28:07 +0800 Subject: [PATCH 1/4] ensure Variable._repr_html_ works --- xarray/core/formatting_html.py | 3 ++- xarray/tests/test_variable.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index 8678a58b381..cee7a5fd383 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -183,7 +183,8 @@ def array_section(obj): # "unique" id to expand/collapse the section data_id = "section-" + str(uuid.uuid4()) collapsed = "" - preview = escape(inline_variable_array_repr(obj.variable, max_width=70)) + variable = obj.variable if hasattr(obj, "variable") else obj + preview = escape(inline_variable_array_repr(variable, max_width=70)) data_repr = short_data_repr_html(obj) data_icon = _icon("icon-database") diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 78e3848b8fb..7030f7a0c09 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1077,6 +1077,17 @@ def test_repr(self): ).strip() assert expected == repr(v) + def test_repr_html(self): + v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"}) + assert hasattr(v, "_repr_html_") + with set_options(display_style="html"): + html = v._repr_html_().strip() + # We don't do a complete string identity since + # html output is probably subject to change, is long and... reasons. + # Just test that something reasonable was produced. + assert html.startswith("") + assert "xarray.Variable" in html + def test_repr_lazy_data(self): v = Variable("x", LazilyOuterIndexedArray(np.arange(2e5))) assert "200000 values with dtype" in repr(v) From e06b18b6e45293fd404ac10f4e82eea0bb650a5d Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Wed, 15 Apr 2020 19:50:43 +0800 Subject: [PATCH 2/4] added PR 3972 to Bug fixes --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e4c3a4d533f..963811bc25c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -77,6 +77,9 @@ Bug fixes By `Tom Nicholas `_. - Fix ``RasterioDeprecationWarning`` when using a ``vrt`` in ``open_rasterio``. (:issue:`3964`) By `Taher Chegini `_. +- Fix ``AttributeError`` on displaying a :py:class:`Variable` + in a notebook context. (:issue:`3972`, :pull:`3973`) + By `Ian Castleden `_. Documentation ~~~~~~~~~~~~~ From 412227b74c9c04bdf8b1ceb9fc7e1183109492f1 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Fri, 17 Apr 2020 18:52:03 +0800 Subject: [PATCH 3/4] better attribute access --- 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 cee7a5fd383..6e345582ed0 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -183,7 +183,7 @@ def array_section(obj): # "unique" id to expand/collapse the section data_id = "section-" + str(uuid.uuid4()) collapsed = "" - variable = obj.variable if hasattr(obj, "variable") else obj + variable = getattr(obj, "variable", obj) preview = escape(inline_variable_array_repr(variable, max_width=70)) data_repr = short_data_repr_html(obj) data_icon = _icon("icon-database") From c493bd1636a6574ad4f5b4562c9f29e477214940 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sat, 18 Apr 2020 12:12:50 +0800 Subject: [PATCH 4/4] moved Varible._repr_html_ test to better location --- xarray/tests/test_formatting_html.py | 12 ++++++++++++ xarray/tests/test_variable.py | 11 ----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/xarray/tests/test_formatting_html.py b/xarray/tests/test_formatting_html.py index 239f339208d..94653016416 100644 --- a/xarray/tests/test_formatting_html.py +++ b/xarray/tests/test_formatting_html.py @@ -137,3 +137,15 @@ def test_repr_of_dataset(dataset): ) assert "<U4" in formatted or ">U4" in formatted assert "<IA>" in formatted + + +def test_variable_repr_html(): + v = xr.Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"}) + assert hasattr(v, "_repr_html_") + with xr.set_options(display_style="html"): + html = v._repr_html_().strip() + # We don't do a complete string identity since + # html output is probably subject to change, is long and... reasons. + # Just test that something reasonable was produced. + assert html.startswith("") + assert "xarray.Variable" in html diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 7030f7a0c09..78e3848b8fb 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1077,17 +1077,6 @@ def test_repr(self): ).strip() assert expected == repr(v) - def test_repr_html(self): - v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"}) - assert hasattr(v, "_repr_html_") - with set_options(display_style="html"): - html = v._repr_html_().strip() - # We don't do a complete string identity since - # html output is probably subject to change, is long and... reasons. - # Just test that something reasonable was produced. - assert html.startswith("") - assert "xarray.Variable" in html - def test_repr_lazy_data(self): v = Variable("x", LazilyOuterIndexedArray(np.arange(2e5))) assert "200000 values with dtype" in repr(v)