import uuid
from collections import OrderedDict
from functools import lru_cache, partial
from html import escape
from importlib.resources import read_binary
from .formatting import inline_variable_array_repr, short_data_repr
from .options import _get_boolean_with_default
STATIC_FILES = (
("xarray.static.html", "icons-svg-inline.html"),
("xarray.static.css", "style.css"),
)
@lru_cache(None)
def _load_static_files():
"""Lazily load the resource files into memory the first time they are needed"""
return [
read_binary(package, resource).decode("utf-8")
for package, resource in STATIC_FILES
]
def short_data_repr_html(array):
"""Format "data" for DataArray and Variable."""
internal_data = getattr(array, "variable", array)._data
if hasattr(internal_data, "_repr_html_"):
return internal_data._repr_html_()
text = escape(short_data_repr(array))
return f"
{text}
"
def format_dims(dims, coord_names):
if not dims:
return ""
dim_css_map = {
k: " class='xr-has-index'" if k in coord_names else "" for k, v in dims.items()
}
dims_li = "".join(
f"
"
def _icon(icon_name):
# icon_name should be defined in xarray/static/html/icon-svg-inline.html
return (
"".format(icon_name)
)
def _summarize_coord_multiindex(name, coord):
preview = f"({', '.join(escape(l) for l in coord.level_names)})"
return summarize_variable(
name, coord, is_index=True, dtype="MultiIndex", preview=preview
)
def summarize_coord(name, var):
is_index = name in var.dims
if is_index:
coord = var.variable.to_index_variable()
if coord.level_names is not None:
coords = {name: _summarize_coord_multiindex(name, coord)}
for lname in coord.level_names:
var = coord.get_level_variable(lname)
coords[lname] = summarize_variable(lname, var)
return coords
return {name: summarize_variable(name, var, is_index)}
def summarize_coords(variables):
coords = {}
for k, v in variables.items():
coords.update(**summarize_coord(k, v))
vars_li = "".join(f"
{v}
" for v in coords.values())
return f"
{vars_li}
"
def summarize_variable(name, var, is_index=False, dtype=None, preview=None):
variable = var.variable if hasattr(var, "variable") else var
cssclass_idx = " class='xr-has-index'" if is_index else ""
dims_str = f"({', '.join(escape(dim) for dim in var.dims)})"
name = escape(str(name))
dtype = dtype or escape(str(var.dtype))
# "unique" ids required to expand/collapse subsections
attrs_id = "attrs-" + str(uuid.uuid4())
data_id = "data-" + str(uuid.uuid4())
disabled = "" if len(var.attrs) else "disabled"
preview = preview or escape(inline_variable_array_repr(variable, 35))
attrs_ul = summarize_attrs(var.attrs)
data_repr = short_data_repr_html(variable)
attrs_icon = _icon("icon-file-text2")
data_icon = _icon("icon-database")
return (
f"
"
def collapsible_section(
name, inline_details="", details="", n_items=None, enabled=True, collapsed=False
):
# "unique" id to expand/collapse the section
data_id = "section-" + str(uuid.uuid4())
has_items = n_items is not None and n_items
n_items_span = "" if n_items is None else f" ({n_items})"
enabled = "" if enabled and has_items else "disabled"
collapsed = "" if collapsed or not has_items else "checked"
tip = " title='Expand/collapse section'" if enabled else ""
return (
f""
f""
f"