1+ from __future__ import annotations
2+
13from pathlib import Path
4+ from urllib .parse import urlparse
25
36import logistro
47
1013KJS_PATH = Path (__file__ ).resolve ().parent / "vendor" / "kaleido_scopes.js"
1114
1215
16+ def _ensure_path (path : Path | str ):
17+ _logger .debug (f"Ensuring path { path !s} " )
18+ if urlparse (str (path )).scheme .startswith ("http" ): # is url
19+ return
20+ if not Path (path ).exists ():
21+ raise FileNotFoundError (f"{ path !s} does not exist." )
22+
23+
1324class PageGenerator :
1425 """
1526 A page generator can set the versions of the js libraries used to render.
@@ -35,7 +46,7 @@ class PageGenerator:
3546 footer = f"""
3647 <script src="{ KJS_PATH .as_uri ()} "></script>
3748 </head>
38- <body style="{{margin: 0; padding: 0;}}"><img id="kaleido-image"><img></body>
49+ <body style="{{margin: 0; padding: 0;}}"><img id="kaleido-image"></ img></body>
3950</html>
4051"""
4152 """The footer is the HTML that always goes on the bottom. Rarely needs changing."""
@@ -45,13 +56,14 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
4556 Create a PageGenerator.
4657
4758 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
59+ plotly: The url to the plotly.js to use. Defaults to plotly.js
60+ present in plotly.py, if installed. Otherwise fallback to
61+ value of DEFAULT_PLOTLY.
62+ mathjax: The url to the mathjax script. Defaults to values of
63+ DEFAULT_MATHJAX. Can be set to false to disable mathjax.
64+ others: A list of other script urls to include. Usually strings, but
65+ can be (str, str) where it's (url, encoding).
66+ force_cdn: Set True to force CDN use, defaults to False.
5567
5668 """
5769 self ._scripts = []
@@ -81,14 +93,19 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
8193 _logger .info ("Plotly not installed. Using CDN." )
8294 plotly = (DEFAULT_PLOTLY , "utf-8" )
8395 elif isinstance (plotly , str ):
96+ _ensure_path (plotly )
8497 plotly = (plotly , "utf-8" )
8598 _logger .debug (f"Plotly script: { plotly } " )
8699 self ._scripts .append (plotly )
87100 if mathjax is not False :
88101 if not mathjax :
89102 mathjax = DEFAULT_MATHJAX
103+ else :
104+ _ensure_path (mathjax )
90105 self ._scripts .append (mathjax )
91106 if others :
107+ for o in others :
108+ _ensure_path (o )
92109 self ._scripts .extend (others )
93110
94111 def generate_index (self , path = None ):
0 commit comments