Skip to content

Commit 70f6c31

Browse files
ayjaytemilykl
andauthored
Andrew/372 check paths (#382)
* Fix malformatted html. * Add path checks to _page_generator. * Fix equivelent errors in test. * Cast string to Path * Fix casting to relevant function w/ if-then * Add better debugging. * Check for file scheme. * Rework page generator test for new file:// error * Check for http:// not for lack of file:// Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com> * Improve error specificity. Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com> * Improve docstrings. --------- Co-authored-by: Emily KL <4672118+emilykl@users.noreply.github.com>
1 parent 0668e4d commit 70f6c31

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

src/py/kaleido/_page_generator.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
4+
from urllib.parse import urlparse
25

36
import logistro
47

@@ -10,6 +13,14 @@
1013
KJS_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+
1324
class 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):

src/py/kaleido/vendor/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
<script src="https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js"></script>
1313
<script src="./kaleido_scopes.js"></script>
1414
</head>
15-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
15+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
1616
</html>

src/py/tests/test_page_generator.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
3030
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
3131
</head>
32-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
32+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
3333
</html>
3434
""") # noqa: E501 line too long
3535

@@ -50,7 +50,7 @@
5050
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
5151
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
5252
</head>
53-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
53+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
5454
</html>
5555
""")
5656

@@ -67,11 +67,11 @@
6767
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
6868
</script>
6969
70-
<script src="file:///with_plot" charset="utf-8"></script>
70+
<script src="https://with_plot" charset="utf-8"></script>
7171
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
7272
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
7373
</head>
74-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
74+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
7575
</html>
7676
""")
7777

@@ -88,10 +88,10 @@
8888
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
8989
</script>
9090
91-
<script src="file:///with_plot" charset="utf-8"></script>
91+
<script src="https://with_plot" charset="utf-8"></script>
9292
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
9393
</head>
94-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
94+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
9595
</html>
9696
""")
9797

@@ -108,13 +108,13 @@
108108
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
109109
</script>
110110
111-
<script src="file:///with_plot" charset="utf-8"></script>
112-
<script src="file:///with_mathjax"></script>
113-
<script src="1"></script>
114-
<script src="2"></script>
111+
<script src="https://with_plot" charset="utf-8"></script>
112+
<script src="https://with_mathjax"></script>
113+
<script src="https://1"></script>
114+
<script src="https://2"></script>
115115
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
116116
</head>
117-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"><img></body>
117+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
118118
</html>
119119
""")
120120

@@ -143,19 +143,19 @@ async def test_page_generator():
143143
all_defaults = PageGenerator().generate_index()
144144
assert all_defaults_re.findall(all_defaults)
145145

146-
with_plot = PageGenerator(plotly="file:///with_plot").generate_index()
146+
with_plot = PageGenerator(plotly="https://with_plot").generate_index()
147147
assert with_plot_result_re.findall(with_plot)
148148

149149
without_math = PageGenerator(
150-
plotly="file:///with_plot",
150+
plotly="https://with_plot",
151151
mathjax=False,
152152
).generate_index()
153153
assert without_math_result_re.findall(without_math)
154154

155155
with_others = PageGenerator(
156-
plotly="file:///with_plot",
157-
mathjax="file:///with_mathjax",
158-
others=["1", "2"],
156+
plotly="https://with_plot",
157+
mathjax="https://with_mathjax",
158+
others=["https://1", "https://2"],
159159
).generate_index()
160160
assert with_others_result_re.findall(with_others)
161161

0 commit comments

Comments
 (0)