Skip to content

Commit d0336f8

Browse files
authored
Andrew/mathjax (#393)
* Reorder and rollback mathjax. * Fix self-closing image tag. * Update tests for new page. * Remove old vendored index.html.
1 parent 582127f commit d0336f8

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

src/py/kaleido/_page_generator.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
_logger = logistro.getLogger(__name__)
99

1010
DEFAULT_PLOTLY = "https://cdn.plot.ly/plotly-2.35.2.js"
11-
DEFAULT_MATHJAX = "https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-svg.js"
11+
DEFAULT_MATHJAX = (
12+
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
13+
"?config=TeX-AMS-MML_SVG"
14+
)
1215

1316
KJS_PATH = Path(__file__).resolve().parent / "vendor" / "kaleido_scopes.js"
1417

@@ -46,7 +49,7 @@ class PageGenerator:
4649
footer = f"""
4750
<script src="{KJS_PATH.as_uri()}"></script>
4851
</head>
49-
<body style="{{margin: 0; padding: 0;}}"><img id="kaleido-image"></img></body>
52+
<body style="{{margin: 0; padding: 0;}}"><img id="kaleido-image" /></body>
5053
</html>
5154
"""
5255
"""The footer is the HTML that always goes on the bottom. Rarely needs changing."""
@@ -67,6 +70,12 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
6770
6871
"""
6972
self._scripts = []
73+
if mathjax is not False:
74+
if not mathjax:
75+
mathjax = DEFAULT_MATHJAX
76+
else:
77+
_ensure_path(mathjax)
78+
self._scripts.append(mathjax)
7079
if force_cdn:
7180
plotly = (DEFAULT_PLOTLY, "utf-8")
7281
elif not plotly:
@@ -97,12 +106,6 @@ def __init__(self, *, plotly=None, mathjax=None, others=None, force_cdn=False):
97106
plotly = (plotly, "utf-8")
98107
_logger.debug(f"Plotly script: {plotly}")
99108
self._scripts.append(plotly)
100-
if mathjax is not False:
101-
if not mathjax:
102-
mathjax = DEFAULT_MATHJAX
103-
else:
104-
_ensure_path(mathjax)
105-
self._scripts.append(mathjax)
106109
if others:
107110
for o in others:
108111
_ensure_path(o)

src/py/kaleido/vendor/index.html

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/py/tests/test_page_generator.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
import pytest
77

88
from kaleido import PageGenerator
9+
from kaleido._page_generator import DEFAULT_MATHJAX, DEFAULT_PLOTLY
910

1011
# allows to create a browser pool for tests
1112
pytestmark = pytest.mark.asyncio(loop_scope="function")
1213

1314
_logger = logistro.getLogger(__name__)
1415

15-
no_imports_result_re = re.compile(r"""
16+
_re_default_mathjax = re.escape(DEFAULT_MATHJAX)
17+
_re_default_plotly = re.escape(DEFAULT_PLOTLY)
18+
19+
no_imports_result_raw = (
20+
r'''
1621
<!DOCTYPE html>
1722
<html>
1823
<head>
@@ -25,15 +30,22 @@
2530
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
2631
</script>
2732
28-
<script src="https://cdn\.plot\.ly/plotly-2\.35\.2\.js" charset="utf-8"></script>
29-
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
33+
<script src="'''
34+
+ _re_default_mathjax
35+
+ r'''"></script>
36+
<script src="'''
37+
+ _re_default_plotly
38+
+ r"""" charset="utf\-8"></script>
3039
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
3140
</head>
32-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
41+
<body style="{margin: 0; padding: 0;}"><img id="kaleido\-image" /></body>
3342
</html>
34-
""") # noqa: E501 line too long
43+
"""
44+
)
45+
no_imports_result_re = re.compile(no_imports_result_raw)
3546

36-
all_defaults_re = re.compile(r"""
47+
all_defaults_re = re.compile(
48+
r'''
3749
<!DOCTYPE html>
3850
<html>
3951
<head>
@@ -46,15 +58,19 @@
4658
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
4759
</script>
4860
61+
<script src="'''
62+
+ _re_default_mathjax
63+
+ r""""></script>
4964
<script src="\S[^\n]*/package_data/plotly\.min\.js" charset="utf-8"></script>
50-
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
5165
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
5266
</head>
53-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
67+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image" /></body>
5468
</html>
55-
""")
69+
""",
70+
)
5671

57-
with_plot_result_re = re.compile(r"""
72+
with_plot_result_re = re.compile(
73+
r'''
5874
<!DOCTYPE html>
5975
<html>
6076
<head>
@@ -67,13 +83,16 @@
6783
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
6884
</script>
6985
86+
<script src="'''
87+
+ _re_default_mathjax
88+
+ r""""></script>
7089
<script src="https://with_plot" charset="utf-8"></script>
71-
<script src="https://cdn\.jsdelivr\.net/npm/mathjax@3\.2\.2/es5/tex-svg\.js"></script>
7290
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
7391
</head>
74-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
92+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image" /></body>
7593
</html>
76-
""")
94+
""",
95+
)
7796

7897
without_math_result_re = re.compile(r"""
7998
<!DOCTYPE html>
@@ -91,11 +110,11 @@
91110
<script src="https://with_plot" charset="utf-8"></script>
92111
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
93112
</head>
94-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
113+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image" /></body>
95114
</html>
96115
""")
97116

98-
with_others_result_re = re.compile(r"""
117+
with_others_result_raw = r"""
99118
<!DOCTYPE html>
100119
<html>
101120
<head>
@@ -108,15 +127,16 @@
108127
MathJax\.Hub\.Config\({ "SVG": { blacker: 0 }}\)
109128
</script>
110129
111-
<script src="https://with_plot" charset="utf-8"></script>
112130
<script src="https://with_mathjax"></script>
131+
<script src="https://with_plot" charset="utf-8"></script>
113132
<script src="https://1"></script>
114133
<script src="https://2"></script>
115134
<script src="\S[^\n]*/kaleido_scopes\.js"></script>
116135
</head>
117-
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image"></img></body>
136+
<body style="{margin: 0; padding: 0;}"><img id="kaleido-image" /></body>
118137
</html>
119-
""")
138+
"""
139+
with_others_result_re = re.compile(with_others_result_raw)
120140

121141

122142
@pytest.mark.order(1)
@@ -136,7 +156,11 @@ async def test_page_generator():
136156
"in the main group.",
137157
)
138158
no_imports = PageGenerator().generate_index()
139-
assert no_imports_result_re.findall(no_imports)
159+
assert no_imports_result_re.findall(no_imports), (
160+
f"{len(no_imports_result_raw)}: {no_imports_result_raw}"
161+
"\n"
162+
f"{len(no_imports)}: {no_imports}"
163+
)
140164
sys.path = old_path
141165

142166
# this imports plotly so above test must have already been done
@@ -157,7 +181,11 @@ async def test_page_generator():
157181
mathjax="https://with_mathjax",
158182
others=["https://1", "https://2"],
159183
).generate_index()
160-
assert with_others_result_re.findall(with_others)
184+
assert with_others_result_re.findall(with_others), (
185+
f"{len(with_others_result_raw)}: {with_others_result_raw}"
186+
"\n"
187+
f"{len(with_others)}: {with_others}"
188+
)
161189

162190

163191
# test others

0 commit comments

Comments
 (0)