|
1 | 1 | import uuid |
2 | 2 | from collections import OrderedDict |
3 | | -from functools import partial |
| 3 | +from functools import lru_cache, partial |
4 | 4 | from html import escape |
5 | 5 |
|
6 | 6 | import pkg_resources |
7 | 7 |
|
8 | 8 | from .formatting import inline_variable_array_repr, short_data_repr |
9 | 9 |
|
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") |
12 | 11 |
|
13 | 12 |
|
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 | + ] |
16 | 21 |
|
17 | 22 |
|
18 | 23 | def short_data_repr_html(array): |
@@ -233,9 +238,10 @@ def _obj_repr(obj, header_components, sections): |
233 | 238 | header = f"<div class='xr-header'>{''.join(h for h in header_components)}</div>" |
234 | 239 | sections = "".join(f"<li class='xr-section-item'>{s}</li>" for s in sections) |
235 | 240 |
|
| 241 | + icons_svg, css_style = _load_static_files() |
236 | 242 | return ( |
237 | 243 | "<div>" |
238 | | - f"{ICONS_SVG}<style>{CSS_STYLE}</style>" |
| 244 | + f"{icons_svg}<style>{css_style}</style>" |
239 | 245 | f"<pre class='xr-text-repr-fallback'>{escape(repr(obj))}</pre>" |
240 | 246 | "<div class='xr-wrap' hidden>" |
241 | 247 | f"{header}" |
|
0 commit comments