Skip to content

Commit 96141fe

Browse files
committed
Add path checks to _page_generator.
1 parent 1d237e9 commit 96141fe

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

src/py/kaleido/_page_generator.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from urllib.parse import urlparse
23

34
import logistro
45

@@ -10,6 +11,13 @@
1011
KJS_PATH = Path(__file__).resolve().parent / "vendor" / "kaleido_scopes.js"
1112

1213

14+
def _ensure_path(path: Path):
15+
if urlparse(path).scheme: # is url
16+
return
17+
if not path.exists():
18+
raise ValueError(f"{path} does not seem to be a valid path.")
19+
20+
1321
class PageGenerator:
1422
"""
1523
A page generator can set the versions of the js libraries used to render.
@@ -45,13 +53,14 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
4553
Create a PageGenerator.
4654
4755
Args:
48-
plotly: the url to the plotly script to use. The default is the one
49-
plotly.py is using, if not installed, it uses the constant declared.
50-
mathjax: the url to the mathjax script. By default is constant above.
51-
Can be set to false to turn off.
52-
others: a list of other script urls to include. Usually strings, but can be
53-
(str, str) where its (url, encoding).
54-
force_cdn: (default False) Don't use plotly import, use CDN
56+
plotly: The url to the plotly.js to use. Defaults to plotly.js
57+
present in plotly.py, if installed. Otherwise fallback to
58+
global constant.
59+
mathjax: The url to the mathjax script. Defaults to global constant.
60+
Can be set to false to turn off.
61+
others: A list of other script urls to include. Usually strings, but
62+
can be (str, str) where it's (url, encoding).
63+
force_cdn: Set True to force CDN use, defaults to False.
5564
5665
"""
5766
self._scripts = []
@@ -81,14 +90,19 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
8190
_logger.info("Plotly not installed. Using CDN.")
8291
plotly = (DEFAULT_PLOTLY, "utf-8")
8392
elif isinstance(plotly, str):
93+
_ensure_path(plotly)
8494
plotly = (plotly, "utf-8")
8595
_logger.debug(f"Plotly script: {plotly}")
8696
self._scripts.append(plotly)
8797
if mathjax is not False:
8898
if not mathjax:
8999
mathjax = DEFAULT_MATHJAX
100+
else:
101+
_ensure_path(mathjax)
90102
self._scripts.append(mathjax)
91103
if others:
104+
for o in others:
105+
_ensure_path(o)
92106
self._scripts.extend(others)
93107

94108
def generate_index(self, path=None):

0 commit comments

Comments
 (0)