Skip to content

Commit a2dac23

Browse files
authored
Remove <pre> from nested HTML repr (#4171)
Using `<pre>` messes up the display of nested HTML reprs, e.g., from dask. Now we only use the `<pre>` tag when displaying text.
1 parent fb5fe79 commit a2dac23

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

xarray/core/formatting_html.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def short_data_repr_html(array):
2020
internal_data = getattr(array, "variable", array)._data
2121
if hasattr(internal_data, "_repr_html_"):
2222
return internal_data._repr_html_()
23-
return escape(short_data_repr(array))
23+
else:
24+
text = escape(short_data_repr(array))
25+
return f"<pre>{text}</pre>"
2426

2527

2628
def format_dims(dims, coord_names):
@@ -123,7 +125,7 @@ def summarize_variable(name, var, is_index=False, dtype=None, preview=None):
123125
f"<label for='{data_id}' title='Show/Hide data repr'>"
124126
f"{data_icon}</label>"
125127
f"<div class='xr-var-attrs'>{attrs_ul}</div>"
126-
f"<pre class='xr-var-data'>{data_repr}</pre>"
128+
f"<div class='xr-var-data'>{data_repr}</div>"
127129
)
128130

129131

@@ -193,7 +195,7 @@ def array_section(obj):
193195
f"<input id='{data_id}' class='xr-array-in' type='checkbox' {collapsed}>"
194196
f"<label for='{data_id}' title='Show/hide data repr'>{data_icon}</label>"
195197
f"<div class='xr-array-preview xr-preview'><span>{preview}</span></div>"
196-
f"<pre class='xr-array-data'>{data_repr}</pre>"
198+
f"<div class='xr-array-data'>{data_repr}</div>"
197199
"</div>"
198200
)
199201

xarray/tests/test_formatting_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def dataset():
4848

4949
def test_short_data_repr_html(dataarray):
5050
data_repr = fh.short_data_repr_html(dataarray)
51-
assert data_repr.startswith("array")
51+
assert data_repr.startswith("<pre>array")
5252

5353

5454
def test_short_data_repr_html_non_str_keys(dataset):

0 commit comments

Comments
 (0)