Skip to content

Commit

Permalink
ensure Variable._repr_html_ works (#3973)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Apr 29, 2020
1 parent 33a66d6 commit 4e196f7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Bug fixes
By `Tom Nicholas <https://github.com/TomNicholas>`_.
- Fix ``RasterioDeprecationWarning`` when using a ``vrt`` in ``open_rasterio``. (:issue:`3964`)
By `Taher Chegini <https://github.com/cheginit>`_.
- Fix ``AttributeError`` on displaying a :py:class:`Variable`
in a notebook context. (:issue:`3972`, :pull:`3973`)
By `Ian Castleden <https://github.com/arabidopsis>`_.
- Fix bug causing :py:meth:`DataArray.interpolate_na` to always drop attributes,
and added `keep_attrs` argument. (:issue:`3968`)
By `Tom Nicholas <https://github.com/TomNicholas>`_.
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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")

Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,15 @@ def test_repr_of_dataset(dataset):
)
assert "&lt;U4" in formatted or "&gt;U4" in formatted
assert "&lt;IA&gt;" 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("<div") and html.endswith("</div>")
assert "xarray.Variable" in html

0 comments on commit 4e196f7

Please sign in to comment.