Skip to content

[BUG] xarray.Dataset containing xarray.DataArrays with non-str dims fails to render in Jupyter HTML modeΒ #10613

@dogganon

Description

@dogganon

Steps to reproduce:

  1. Create a jupyter notebook or jupyter console in HTML mode.
  2. Define an xarray.Dataset containing a xarray.DataArray with non-str dims:
import xarray
import numpy

xarray.Dataset({
    "fsdaf": xarray.DataArray(
        numpy.zeros((42, 14, 3)),
        dims=(("fsdfsljd", "sdf"), ("fsdaf.w", ), ("fsdaf.c", )),
    ),
})
  1. 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("&", "&amp;") # Must be done first!
     20     s = s.replace("<", "&lt;")
     21     s = s.replace(">", "&gt;")

<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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions