[BUG] `xarray.Dataset` containing `xarray.DataArray`s with non-`str` dims fails to render in Jupyter HTML mode
Open
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Steps to reproduce:
- Create a jupyter notebook or jupyter console in HTML mode.
- Define an
xarray.Datasetcontaining axarray.DataArraywith non-strdims:
import xarray
import numpy
xarray.Dataset({
"fsdaf": xarray.DataArray(
numpy.zeros((42, 14, 3)),
dims=(("fsdfsljd", "sdf"), ("fsdaf.w", ), ("fsdaf.c", )),
),
})
- Observe the jupyter cell output: the HTML summary fails to render and falls back to text mode.
...
File <omit>/xarray/core/formatting_html.py:130, in <genexpr>(.0)
128 def summarize_vars(variables) -> str:
129 vars_li = "".join(
--> 130 f"<li class='xr-var-item'>{summarize_variable(k, v)}</li>"
131 for k, v in variables.items()
132 )
134 return f"<ul class='xr-var-list'>{vars_li}</ul>"
File <omit>/xarray/core/formatting_html.py:84, in summarize_variable(name, var, is_index, dtype)
81 variable = var.variable if hasattr(var, "variable") else var
83 cssclass_idx = " class='xr-has-index'" if is_index else ""
---> 84 dims_str = f"({', '.join(escape((dim)) for dim in var.dims)})"
85 name = escape(str(name))
86 dtype = dtype or escape(str(var.dtype))
File <omit>/xarray/core/formatting_html.py:84, in <genexpr>(.0)
81 variable = var.variable if hasattr(var, "variable") else var
83 cssclass_idx = " class='xr-has-index'" if is_index else ""
---> 84 dims_str = f"({', '.join(escape((dim)) for dim in var.dims)})"
85 name = escape(str(name))
86 dtype = dtype or escape(str(var.dtype))
File <omit>/html/__init__.py:19, in escape(s, quote)
12 def escape(s, quote=True):
13 """
14 Replace special characters "&", "<" and ">" to HTML-safe sequences.
15 If the optional flag quote is true (the default), the quotation mark
16 characters, both double quote (") and single quote (') characters are also
17 translated.
18 """
---> 19 s = s.replace("&", "&") # Must be done first!
20 s = s.replace("<", "<")
21 s = s.replace(">", ">")
<xarray.Dataset> Size: 14kB
Dimensions: (('fsdfsljd', 'sdf'): 42, ('fsdaf.w',): 14, ('fsdaf.c',): 3)
Dimensions without coordinates: ('fsdfsljd', 'sdf'), ('fsdaf.w',), ('fsdaf.c',)
Data variables:
fsdaf (('fsdfsljd', 'sdf'), ('fsdaf.w',), ('fsdaf.c',)) float64 14kB 0...
Cause:
The dim names are not converted to strs first:
https://github.com/pydata/xarray/blob/40119bf35c53fc222934894d842cfc07b2e264bf/xarray/core/formatting_html.py#L84C1-L84C65
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in xarray/core/formatting_html.py at summarize_variable, especially the dims_str expression identified in the traceback. Reproduce the Dataset example in a Jupyter notebook or console using HTML mode, then verify that the HTML summary renders successfully when dimension names are non-string values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter, numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100