Skip to content

Commit 4e196f7

Browse files
arabidopsisshoyerdcherian
authored
ensure Variable._repr_html_ works (#3973)
* ensure Variable._repr_html_ works * added PR 3972 to Bug fixes * better attribute access * moved Varible._repr_html_ test to better location Co-authored-by: Stephan Hoyer <[email protected]> Co-authored-by: Deepak Cherian <[email protected]>
1 parent 33a66d6 commit 4e196f7

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ Bug fixes
7979
By `Tom Nicholas <https://github.com/TomNicholas>`_.
8080
- Fix ``RasterioDeprecationWarning`` when using a ``vrt`` in ``open_rasterio``. (:issue:`3964`)
8181
By `Taher Chegini <https://github.com/cheginit>`_.
82+
- Fix ``AttributeError`` on displaying a :py:class:`Variable`
83+
in a notebook context. (:issue:`3972`, :pull:`3973`)
84+
By `Ian Castleden <https://github.com/arabidopsis>`_.
8285
- Fix bug causing :py:meth:`DataArray.interpolate_na` to always drop attributes,
8386
and added `keep_attrs` argument. (:issue:`3968`)
8487
By `Tom Nicholas <https://github.com/TomNicholas>`_.

xarray/core/formatting_html.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def array_section(obj):
183183
# "unique" id to expand/collapse the section
184184
data_id = "section-" + str(uuid.uuid4())
185185
collapsed = ""
186-
preview = escape(inline_variable_array_repr(obj.variable, max_width=70))
186+
variable = getattr(obj, "variable", obj)
187+
preview = escape(inline_variable_array_repr(variable, max_width=70))
187188
data_repr = short_data_repr_html(obj)
188189
data_icon = _icon("icon-database")
189190

xarray/tests/test_formatting_html.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,15 @@ def test_repr_of_dataset(dataset):
137137
)
138138
assert "&lt;U4" in formatted or "&gt;U4" in formatted
139139
assert "&lt;IA&gt;" in formatted
140+
141+
142+
def test_variable_repr_html():
143+
v = xr.Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
144+
assert hasattr(v, "_repr_html_")
145+
with xr.set_options(display_style="html"):
146+
html = v._repr_html_().strip()
147+
# We don't do a complete string identity since
148+
# html output is probably subject to change, is long and... reasons.
149+
# Just test that something reasonable was produced.
150+
assert html.startswith("<div") and html.endswith("</div>")
151+
assert "xarray.Variable" in html

0 commit comments

Comments
 (0)