"""HTML representation for PyVista objects in Jupyter notebooks. Uses pure HTML/CSS with checkbox-driven expand/collapse. The only JavaScript is a minimal ``navigator.clipboard.writeText`` call for copy-to-clipboard buttons. Works in Jupyter Notebook, JupyterLab, VS Code, Colab, and nbviewer. """ from __future__ import annotations from functools import lru_cache from html import escape from importlib.resources import files as _resources_files from typing import TYPE_CHECKING import uuid if TYPE_CHECKING: from collections.abc import Sequence @lru_cache(None) def _load_css() -> str: """Load the CSS stylesheet (cached).""" return ( _resources_files('pyvista.core.static').joinpath('style.css').read_text(encoding='utf-8') ) @lru_cache(None) def _load_pyvista_logo() -> str: """Load the PyVista brand mark SVG (cached).""" return ( _resources_files('pyvista.core.static') .joinpath('pyvista_logo.svg') .read_text(encoding='utf-8') ) _MESH_TYPE_ICONS: dict[str, str] = { 'PolyData': 'polydata.svg', 'UnstructuredGrid': 'unstructured.svg', 'ImageData': 'imagedata.svg', 'RectilinearGrid': 'rectilinear.svg', 'StructuredGrid': 'structured.svg', 'MultiBlock': 'multiblock.svg', } @lru_cache(None) def _load_mesh_icon(mesh_type: str) -> str | None: """Load a mesh-type SVG icon, or ``None`` if unavailable.""" filename = _MESH_TYPE_ICONS.get(mesh_type) if filename is None: return None icon_dir = _resources_files('pyvista.core.static').joinpath('mesh_icons') return icon_dir.joinpath(filename).read_text(encoding='utf-8') def collapsible_section( header: str, *, inline_details: str = '', details: str = '', n_items: int | None = None, enabled: bool = True, collapsed: bool = False, ) -> str: """Build an expand/collapse section using a hidden checkbox. Parameters ---------- header : str Section label (e.g. ``'Point Data:'``). inline_details : str Content shown when collapsed. details : str Content shown when expanded. n_items : int, optional Item count displayed in parentheses. A value of ``0`` disables the section. enabled : bool Whether the section is expandable. collapsed : bool Whether the section starts collapsed. Returns ------- str HTML fragment for the section. """ data_id = 'section-' + str(uuid.uuid4()) has_items = n_items is not None and n_items > 0 n_items_span = '' if n_items is None else f' ({n_items})' enabled_attr = '' if enabled and has_items else ' disabled' collapsed_attr = '' if collapsed or not has_items else ' checked' tip = " title='Expand/collapse section'" if enabled_attr == '' else '' html = ( f"" f"' f"
{text_fallback}"
"'
'