Skip to content

Commit f99c6cc

Browse files
authored
Lazily load resource files (#4297)
* Lazily load resource files * isort
1 parent 9058114 commit f99c6cc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Documentation
4545

4646
Internal Changes
4747
~~~~~~~~~~~~~~~~
48+
- Only load resource files when running inside a Jupyter Notebook
49+
(:issue:`4294`) By `Guido Imperiale <https://github.com/crusaderky>`_
4850

4951

5052
.. _whats-new.0.16.0:

xarray/core/formatting_html.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import uuid
22
from collections import OrderedDict
3-
from functools import partial
3+
from functools import lru_cache, partial
44
from html import escape
55

66
import pkg_resources
77

88
from .formatting import inline_variable_array_repr, short_data_repr
99

10-
CSS_FILE_PATH = "/".join(("static", "css", "style.css"))
11-
CSS_STYLE = pkg_resources.resource_string("xarray", CSS_FILE_PATH).decode("utf8")
10+
STATIC_FILES = ("static/html/icons-svg-inline.html", "static/css/style.css")
1211

1312

14-
ICONS_SVG_PATH = "/".join(("static", "html", "icons-svg-inline.html"))
15-
ICONS_SVG = pkg_resources.resource_string("xarray", ICONS_SVG_PATH).decode("utf8")
13+
@lru_cache(None)
14+
def _load_static_files():
15+
"""Lazily load the resource files into memory the first time they are needed
16+
"""
17+
return [
18+
pkg_resources.resource_string("xarray", fname).decode("utf8")
19+
for fname in STATIC_FILES
20+
]
1621

1722

1823
def short_data_repr_html(array):
@@ -233,9 +238,10 @@ def _obj_repr(obj, header_components, sections):
233238
header = f"<div class='xr-header'>{''.join(h for h in header_components)}</div>"
234239
sections = "".join(f"<li class='xr-section-item'>{s}</li>" for s in sections)
235240

241+
icons_svg, css_style = _load_static_files()
236242
return (
237243
"<div>"
238-
f"{ICONS_SVG}<style>{CSS_STYLE}</style>"
244+
f"{icons_svg}<style>{css_style}</style>"
239245
f"<pre class='xr-text-repr-fallback'>{escape(repr(obj))}</pre>"
240246
"<div class='xr-wrap' hidden>"
241247
f"{header}"

0 commit comments

Comments
 (0)