Skip to content

Commit feda342

Browse files
authored
Bundle and autodetect MathJax (#75)
* Bundle and autodetect MathJax These changes bundle MathJax. When MathJax is not bundled, the Python package looks for the `mathjax-path` script that is installed by the conda-forge MathJax pacakge.
1 parent 37e6ec4 commit feda342

File tree

7 files changed

+267
-4
lines changed

7 files changed

+267
-4
lines changed

LICENSE.txt

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ of these dependencies.
88

99
The CREDITS.html is also available at https://github.com/plotly/Kaleido/blob/master/repos/CREDITS.html
1010

11+
The Kaleido python PyPI package vendors MathJax, which is released under the terms of the Apache License (3)
12+
1113
(1) ----
1214
The MIT License (MIT)
1315

@@ -59,3 +61,207 @@ THE SOFTWARE.
5961
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6062
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
6163
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64+
65+
(3) ----
66+
Apache License
67+
Version 2.0, January 2004
68+
http://www.apache.org/licenses/
69+
70+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
71+
72+
1. Definitions.
73+
74+
"License" shall mean the terms and conditions for use, reproduction,
75+
and distribution as defined by Sections 1 through 9 of this document.
76+
77+
"Licensor" shall mean the copyright owner or entity authorized by
78+
the copyright owner that is granting the License.
79+
80+
"Legal Entity" shall mean the union of the acting entity and all
81+
other entities that control, are controlled by, or are under common
82+
control with that entity. For the purposes of this definition,
83+
"control" means (i) the power, direct or indirect, to cause the
84+
direction or management of such entity, whether by contract or
85+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
86+
outstanding shares, or (iii) beneficial ownership of such entity.
87+
88+
"You" (or "Your") shall mean an individual or Legal Entity
89+
exercising permissions granted by this License.
90+
91+
"Source" form shall mean the preferred form for making modifications,
92+
including but not limited to software source code, documentation
93+
source, and configuration files.
94+
95+
"Object" form shall mean any form resulting from mechanical
96+
transformation or translation of a Source form, including but
97+
not limited to compiled object code, generated documentation,
98+
and conversions to other media types.
99+
100+
"Work" shall mean the work of authorship, whether in Source or
101+
Object form, made available under the License, as indicated by a
102+
copyright notice that is included in or attached to the work
103+
(an example is provided in the Appendix below).
104+
105+
"Derivative Works" shall mean any work, whether in Source or Object
106+
form, that is based on (or derived from) the Work and for which the
107+
editorial revisions, annotations, elaborations, or other modifications
108+
represent, as a whole, an original work of authorship. For the purposes
109+
of this License, Derivative Works shall not include works that remain
110+
separable from, or merely link (or bind by name) to the interfaces of,
111+
the Work and Derivative Works thereof.
112+
113+
"Contribution" shall mean any work of authorship, including
114+
the original version of the Work and any modifications or additions
115+
to that Work or Derivative Works thereof, that is intentionally
116+
submitted to Licensor for inclusion in the Work by the copyright owner
117+
or by an individual or Legal Entity authorized to submit on behalf of
118+
the copyright owner. For the purposes of this definition, "submitted"
119+
means any form of electronic, verbal, or written communication sent
120+
to the Licensor or its representatives, including but not limited to
121+
communication on electronic mailing lists, source code control systems,
122+
and issue tracking systems that are managed by, or on behalf of, the
123+
Licensor for the purpose of discussing and improving the Work, but
124+
excluding communication that is conspicuously marked or otherwise
125+
designated in writing by the copyright owner as "Not a Contribution."
126+
127+
"Contributor" shall mean Licensor and any individual or Legal Entity
128+
on behalf of whom a Contribution has been received by Licensor and
129+
subsequently incorporated within the Work.
130+
131+
2. Grant of Copyright License. Subject to the terms and conditions of
132+
this License, each Contributor hereby grants to You a perpetual,
133+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
134+
copyright license to reproduce, prepare Derivative Works of,
135+
publicly display, publicly perform, sublicense, and distribute the
136+
Work and such Derivative Works in Source or Object form.
137+
138+
3. Grant of Patent License. Subject to the terms and conditions of
139+
this License, each Contributor hereby grants to You a perpetual,
140+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
141+
(except as stated in this section) patent license to make, have made,
142+
use, offer to sell, sell, import, and otherwise transfer the Work,
143+
where such license applies only to those patent claims licensable
144+
by such Contributor that are necessarily infringed by their
145+
Contribution(s) alone or by combination of their Contribution(s)
146+
with the Work to which such Contribution(s) was submitted. If You
147+
institute patent litigation against any entity (including a
148+
cross-claim or counterclaim in a lawsuit) alleging that the Work
149+
or a Contribution incorporated within the Work constitutes direct
150+
or contributory patent infringement, then any patent licenses
151+
granted to You under this License for that Work shall terminate
152+
as of the date such litigation is filed.
153+
154+
4. Redistribution. You may reproduce and distribute copies of the
155+
Work or Derivative Works thereof in any medium, with or without
156+
modifications, and in Source or Object form, provided that You
157+
meet the following conditions:
158+
159+
(a) You must give any other recipients of the Work or
160+
Derivative Works a copy of this License; and
161+
162+
(b) You must cause any modified files to carry prominent notices
163+
stating that You changed the files; and
164+
165+
(c) You must retain, in the Source form of any Derivative Works
166+
that You distribute, all copyright, patent, trademark, and
167+
attribution notices from the Source form of the Work,
168+
excluding those notices that do not pertain to any part of
169+
the Derivative Works; and
170+
171+
(d) If the Work includes a "NOTICE" text file as part of its
172+
distribution, then any Derivative Works that You distribute must
173+
include a readable copy of the attribution notices contained
174+
within such NOTICE file, excluding those notices that do not
175+
pertain to any part of the Derivative Works, in at least one
176+
of the following places: within a NOTICE text file distributed
177+
as part of the Derivative Works; within the Source form or
178+
documentation, if provided along with the Derivative Works; or,
179+
within a display generated by the Derivative Works, if and
180+
wherever such third-party notices normally appear. The contents
181+
of the NOTICE file are for informational purposes only and
182+
do not modify the License. You may add Your own attribution
183+
notices within Derivative Works that You distribute, alongside
184+
or as an addendum to the NOTICE text from the Work, provided
185+
that such additional attribution notices cannot be construed
186+
as modifying the License.
187+
188+
You may add Your own copyright statement to Your modifications and
189+
may provide additional or different license terms and conditions
190+
for use, reproduction, or distribution of Your modifications, or
191+
for any such Derivative Works as a whole, provided Your use,
192+
reproduction, and distribution of the Work otherwise complies with
193+
the conditions stated in this License.
194+
195+
5. Submission of Contributions. Unless You explicitly state otherwise,
196+
any Contribution intentionally submitted for inclusion in the Work
197+
by You to the Licensor shall be under the terms and conditions of
198+
this License, without any additional terms or conditions.
199+
Notwithstanding the above, nothing herein shall supersede or modify
200+
the terms of any separate license agreement you may have executed
201+
with Licensor regarding such Contributions.
202+
203+
6. Trademarks. This License does not grant permission to use the trade
204+
names, trademarks, service marks, or product names of the Licensor,
205+
except as required for reasonable and customary use in describing the
206+
origin of the Work and reproducing the content of the NOTICE file.
207+
208+
7. Disclaimer of Warranty. Unless required by applicable law or
209+
agreed to in writing, Licensor provides the Work (and each
210+
Contributor provides its Contributions) on an "AS IS" BASIS,
211+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
212+
implied, including, without limitation, any warranties or conditions
213+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
214+
PARTICULAR PURPOSE. You are solely responsible for determining the
215+
appropriateness of using or redistributing the Work and assume any
216+
risks associated with Your exercise of permissions under this License.
217+
218+
8. Limitation of Liability. In no event and under no legal theory,
219+
whether in tort (including negligence), contract, or otherwise,
220+
unless required by applicable law (such as deliberate and grossly
221+
negligent acts) or agreed to in writing, shall any Contributor be
222+
liable to You for damages, including any direct, indirect, special,
223+
incidental, or consequential damages of any character arising as a
224+
result of this License or out of the use or inability to use the
225+
Work (including but not limited to damages for loss of goodwill,
226+
work stoppage, computer failure or malfunction, or any and all
227+
other commercial damages or losses), even if such Contributor
228+
has been advised of the possibility of such damages.
229+
230+
9. Accepting Warranty or Additional Liability. While redistributing
231+
the Work or Derivative Works thereof, You may choose to offer,
232+
and charge a fee for, acceptance of support, warranty, indemnity,
233+
or other liability obligations and/or rights consistent with this
234+
License. However, in accepting such obligations, You may act only
235+
on Your own behalf and on Your sole responsibility, not on behalf
236+
of any other Contributor, and only if You agree to indemnify,
237+
defend, and hold each Contributor harmless for any liability
238+
incurred by, or claims asserted against, such Contributor by reason
239+
of your accepting any such warranty or additional liability.
240+
241+
END OF TERMS AND CONDITIONS
242+
243+
APPENDIX: How to apply the Apache License to your work.
244+
245+
To apply the Apache License to your work, attach the following
246+
boilerplate notice, with the fields enclosed by brackets "[]"
247+
replaced with your own identifying information. (Don't include
248+
the brackets!) The text should be enclosed in the appropriate
249+
comment syntax for the file format. We also recommend that a
250+
file or class name and description of purpose be included on the
251+
same "printed page" as the copyright notice for easier
252+
identification within third-party archives.
253+
254+
Copyright [yyyy] [name of copyright owner]
255+
256+
Licensed under the Apache License, Version 2.0 (the "License");
257+
you may not use this file except in compliance with the License.
258+
You may obtain a copy of the License at
259+
260+
http://www.apache.org/licenses/LICENSE-2.0
261+
262+
Unless required by applicable law or agreed to in writing, software
263+
distributed under the License is distributed on an "AS IS" BASIS,
264+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
265+
See the License for the specific language governing permissions and
266+
limitations under the License.
267+

repos/kaleido/py/kaleido/scopes/plotly.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import absolute_import
2-
from kaleido.scopes.base import BaseScope
2+
from kaleido.scopes.base import BaseScope, which
33
from _plotly_utils.utils import PlotlyJSONEncoder
44
import base64
5-
5+
import os
6+
from pathlib import Path
7+
import subprocess
68

79
class PlotlyScope(BaseScope):
810
"""
@@ -19,10 +21,15 @@ def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_tok
1921
# TODO: validate args
2022
# Save scope flags as internal properties
2123
self._plotlyjs = plotlyjs
22-
self._mathjax = mathjax
2324
self._topojson = topojson
2425
self._mapbox_access_token = mapbox_access_token
2526

27+
# Try to find local MathJax, but never fail if something goes wrong
28+
try:
29+
self._initialize_mathax(mathjax)
30+
except:
31+
self._mathjax = None
32+
2633
# to_image-level default values
2734
self.default_format = "png"
2835
self.default_width = 700
@@ -31,6 +38,37 @@ def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_tok
3138

3239
super(PlotlyScope, self).__init__(**kwargs)
3340

41+
def _initialize_mathax(self, mathjax=None):
42+
if mathjax is not None:
43+
self._mathjax = mathjax
44+
return
45+
46+
vendored_mathjax_path = os.path.join(
47+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
48+
'executable',
49+
'etc',
50+
'mathjax',
51+
'MathJax.js'
52+
)
53+
mathjax_path = None
54+
if os.path.exists(vendored_mathjax_path):
55+
# MathJax is vendored under kaleido/executable.
56+
# It was probably install as a PyPI wheel
57+
mathjax_path = vendored_mathjax_path
58+
else:
59+
mathjax_path_executable = which("mathjax-path")
60+
if mathjax_path_executable:
61+
# A script named "mathjax-path" found on the PATH,
62+
# MathJax was probably installed as a conda package
63+
path_bytes = subprocess.check_output(mathjax_path_executable)
64+
mathjax_path = path_bytes.decode("utf8").strip()
65+
66+
if mathjax_path:
67+
mathjax_uri = Path(mathjax_path).absolute().as_uri()
68+
self._mathjax = mathjax_uri
69+
else:
70+
self._mathjax = None
71+
3472
@property
3573
def scope_name(self):
3674
return "plotly"

repos/kaleido/py/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ def run(self):
294294
long_description=long_description,
295295
long_description_content_type="text/markdown",
296296
license="MIT",
297+
install_requires=[
298+
"pathlib ; python_version<'3.4'",
299+
],
297300
packages=["kaleido", "kaleido.scopes"],
298301
package_data={
299302
'kaleido': executable_files,

repos/linux_scripts/perform_kaleido_build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ cp -r /etc/fonts/ /repos/build/kaleido/etc/fonts
7474
mkdir -p /repos/build/kaleido/xdg
7575
cp -r /usr/share/fonts/ /repos/build/kaleido/xdg/
7676

77+
# mathjax
78+
unzip /repos/vendor/Mathjax-2.7.5.zip -d /repos/build/kaleido/etc/
79+
mv /repos/build/kaleido/etc/Mathjax-2.7.5 /repos/build/kaleido/etc/mathjax
80+
7781
# Add full launch script
7882
cp /repos/linux_scripts/launch_script /repos/build/kaleido/kaleido
7983

repos/mac_scripts/build_kaleido

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ rm -r ../build/kaleido/*
6464
mkdir -p ../build/kaleido/bin
6565
cp out/Kaleido_mac_$KALEIDO_ARCH/kaleido ../build/kaleido/bin
6666

67+
# mathjax
68+
mkdir -p ../build/kaleido/etc
69+
unzip ../vendor/Mathjax-2.7.5.zip -d ../build/kaleido/etc/
70+
mv ../build/kaleido/etc/Mathjax-2.7.5 ../build/kaleido/etc/mathjax
71+
6772
# Copy icudtl.dat and settings.dat
6873
cp ./out/Kaleido_mac_$KALEIDO_ARCH/icudtl.dat ../build/kaleido/bin
6974

repos/vendor/Mathjax-2.7.5.zip

1.84 MB
Binary file not shown.

repos/win_scripts/build_kaleido.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ cp ..\kaleido\version ..\build\kaleido\
6969
cp ..\kaleido\LICENSE.txt ..\build\kaleido\
7070
cp ..\kaleido\CREDITS.html ..\build\kaleido\
7171

72+
# mathjax
73+
if (-Not (Test-Path ..\build\kaleido\etc)) {
74+
New-Item -Path ..\build\kaleido\etc -ItemType "directory"
75+
}
76+
Expand-Archive -LiteralPath '..\vendor\Mathjax-2.7.5.zip' -DestinationPath ..\build\kaleido\etc\
77+
Rename-Item -Path ..\build\kaleido\etc\Mathjax-2.7.5 -NewName mathjax
78+
7279
# Copy icudtl.dat
7380
Copy-Item .\out\Kaleido_win_$arch\icudtl.dat -Destination ..\build\kaleido\bin
7481

@@ -110,4 +117,4 @@ Compress-Archive -Path ..\build\kaleido -DestinationPath ..\build\kaleido_win_$a
110117
if (Test-Path ..\kaleido\py\kaleido_wheel.zip) {
111118
Remove-Item -Recurse -Force ..\kaleido\py\kaleido_wheel.zip
112119
}
113-
Compress-Archive -Path ..\kaleido\py\dist -DestinationPath ..\kaleido\py\kaleido_wheel.zip
120+
Compress-Archive -Path ..\kaleido\py\dist -DestinationPath ..\kaleido\py\kaleido_wheel.zip

0 commit comments

Comments
 (0)