diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2ad2a426532..4b8f980cade 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -45,6 +45,8 @@ Documentation Internal Changes ~~~~~~~~~~~~~~~~ +- Only load resource files when running inside a Jupyter Notebook + (:issue:`4294`) By `Guido Imperiale `_ .. _whats-new.0.16.0: diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index 400ef61502e..5521b33e2e4 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -1,18 +1,23 @@ import uuid from collections import OrderedDict -from functools import partial +from functools import lru_cache, partial from html import escape import pkg_resources from .formatting import inline_variable_array_repr, short_data_repr -CSS_FILE_PATH = "/".join(("static", "css", "style.css")) -CSS_STYLE = pkg_resources.resource_string("xarray", CSS_FILE_PATH).decode("utf8") +STATIC_FILES = ("static/html/icons-svg-inline.html", "static/css/style.css") -ICONS_SVG_PATH = "/".join(("static", "html", "icons-svg-inline.html")) -ICONS_SVG = pkg_resources.resource_string("xarray", ICONS_SVG_PATH).decode("utf8") +@lru_cache(None) +def _load_static_files(): + """Lazily load the resource files into memory the first time they are needed + """ + return [ + pkg_resources.resource_string("xarray", fname).decode("utf8") + for fname in STATIC_FILES + ] def short_data_repr_html(array): @@ -233,9 +238,10 @@ def _obj_repr(obj, header_components, sections): header = f"
{''.join(h for h in header_components)}
" sections = "".join(f"
  • {s}
  • " for s in sections) + icons_svg, css_style = _load_static_files() return ( "
    " - f"{ICONS_SVG}" + f"{icons_svg}" f"
    {escape(repr(obj))}
    " "