-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathplotly.py
316 lines (271 loc) · 10.8 KB
/
plotly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from __future__ import absolute_import
import os
from pathlib import Path
import tempfile
import sys
from plotly.graph_objects import Figure
import kaleido # kaleido __init__.py, dislike
from choreographer import which_browser
# The original kaleido provided a global lock (instead of supporting concurrency)
# So kaleido 2.0 will as well if it's used from scopes (old api)
from threading import Lock
_proc_lock = Lock()
try:
from json import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError
class PlotlyScope():
"""
Scope for transforming Plotly figures to static images
"""
_all_formats = kaleido._all_formats_
_text_formats = kaleido._text_formats_
_scope_flags = kaleido._scope_flags_
def __init__(self, plotlyjs=None, mathjax=None, topojson=None, mapbox_access_token=None, debug=None, tmp_path=None, **kwargs):
if debug is None:
debug = "KALEIDO-DEBUG" in os.environ or "KALEIDO_DEBUG" in os.environ
self.debug=debug
if tmp_path is None:
tmp_path = os.environ.get("KALEIDO_TMP_PATH", None)
self._tmp_path = tmp_path
# TODO: #2 This is deprecated, this whole FILE is deprecated
self._plotlyjs = plotlyjs
self._topojson = topojson
self._mapbox_access_token = mapbox_access_token
# Try to find local MathJax, but never fail if something goes wrong
try:
self._initialize_mathax(mathjax)
except: # noqa TODO what would the actual error be
self._mathjax = None
# to_image-level default values
self.default_format = "png"
self.default_width = 700
self.default_height = 500
self.default_scale = 1
self._plotlyfier = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'vendor',
'kaleido_scopes.js'
)
path = os.environ.get("BROWSER_PATH", which_browser())
if tmp_path:
temp_args = dict(dir=self.tmp_path)
elif "snap" in path:
temp_path = Path.home()
if self.debug:
print("Snap detected, moving tmp directory to home", file=sys.stderr)
temp_args = dict(prefix=".kaleido-", dir=temp_path)
else:
self._snap = False
temp_args = {}
self._tempdir = tempfile.TemporaryDirectory(**temp_args)
if self.debug: print(f"Tempdir: {self._tempdir.name}", file=sys.stderr)
self._tempfile = open(f"{self._tempdir.name}/index.html", "w")
self._tempfile.write(self.make_page_string())
self._tempfile.close()
def _initialize_mathax(self, mathjax=None):
if mathjax is not None:
self._mathjax = mathjax
return
vendored_mathjax_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'vendor',
'mathjax',
'MathJax.js'
)
# There is something wild going on w/ mathjax, I think plotly is injecting it?
mathjax_path = None
if os.path.exists(vendored_mathjax_path):
# MathJax is vendored under kaleido/executable.
# It was probably install as a PyPI wheel
mathjax_path = vendored_mathjax_path
if mathjax_path:
mathjax_uri = Path(mathjax_path).absolute().as_uri()
self._mathjax = mathjax_uri
else:
self._mathjax = None
def make_page_string(self):
page = \
"""<!DOCTYPE html>
<html>
<head>
<style id=\"head-style\"></style>
<title>Kaleido-fier</title>
<script>
window.KaleidoReport = ["start"];
function logError(e) {
window.KaleidoReport.push("error");
window.KaleidoReport.push(e);
if (!navigator.onLine) {
window.KaleidoReport.push("offline");
}
}
</script>
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'}
</script>
"""
pjs = self._plotlyjs
if not pjs:
pjs = "https://cdn.plot.ly/plotly-2.35.2.min.js"
elif not pjs.startswith("http") and not pjs.startswith("file"):
pjs = Path(pjs).absolute().as_uri()
page += f" <script src=\"{pjs}\" charset=\"utf-8\" onerror=\"logError('plotly')\"></script>\n"
if self._mathjax:
page += f" <script type=\"text/javascript\" id=\"MathJax-script\" src=\"{self._mathjax}?config=TeX-AMS-MML_SVG\" onerror=\"logError('mathjax')\"></script>\n"
page+= \
f""" <script src="{Path(self._plotlyfier).absolute().as_uri()}" onerror=\"logError('scoper')\"></script>"""+\
""" </head>
<body style=\"{margin: 0; padding: 0;}\"><img id=\"kaleido-image\"><img></body>
</html>"""
if self.debug:
print("Displaying generated HTML".center(50, "*"), file=sys.stderr)
print(page, file=sys.stderr)
print("end".center(50, "*"), file=sys.stderr)
return page
@property
def scope_name(self):
# TODO: #2 This is deprecated
return "plotly"
def make_spec(self, figure, format=None, width=None, height=None, scale=None):
# TODO: validate args
if isinstance(figure, Figure):
figure = figure.to_dict()
# Apply default format and scale
format = format if format is not None else self.default_format
scale = scale if scale is not None else self.default_scale
# Get figure layout
layout = figure.get("layout", {})
# Compute image width / height
width = (
width
or layout.get("width", None)
or layout.get("template", {}).get("layout", {}).get("width", None)
or self.default_width
)
height = (
height
or layout.get("height", None)
or layout.get("template", {}).get("layout", {}).get("height", None)
or self.default_height
)
# Normalize format
original_format = format
format = format.lower()
if format == 'jpg':
format = 'jpeg'
if format not in self._all_formats:
supported_formats_str = repr(list(self._all_formats))
raise ValueError(
"Invalid format '{original_format}'.\n"
" Supported formats: {supported_formats_str}"
.format(
original_format=original_format,
supported_formats_str=supported_formats_str
)
)
js_args = dict(format=format, width=width, height=height, scale=scale)
return dict(js_args, data = figure)
def transform(self, figure, format=None, width=None, height=None, scale=None, debug=None):
"""
Convert a Plotly figure into a static image
:param figure: Plotly figure or figure dictionary
:param format: The desired image format. One of
'png', 'jpg', 'jpeg', 'webp', 'svg', 'pdf', or 'json'.
If 'json', the following arguments are ignored and a full
JSON representation of the figure is returned.
If not specified, will default to the `scope.default_format` property
:param width: The width of the exported image in layout pixels.
If the `scale` property is 1.0, this will also be the width
of the exported image in physical pixels.
If not specified, will default to the `scope.default_width` property
:param height: The height of the exported image in layout pixels.
If the `scale` property is 1.0, this will also be the height
of the exported image in physical pixels.
If not specified, will default to the `scope.default_height` property
:param scale: The scale factor to use when exporting the figure.
A scale factor larger than 1.0 will increase the image resolution
with respect to the figure's layout pixel dimensions. Whereas as
scale factor of less than 1.0 will decrease the image resolution.
If not specified, will default to the `scope.default_scale` property
:return: image bytes
"""
if not debug:
debug=self.debug
spec = self.make_spec(figure, format=format, width=width, height=height, scale=scale)
# Write to process and read result within a lock so that can be
# sure we're reading the response to our request
with _proc_lock:
img = kaleido.to_image_block(spec, Path(self._tempfile.name).absolute(), self._topojson, self._mapbox_access_token, debug=debug, tmp_path=self._tmp_path)
return img
# Flag property methods
@property
def plotlyjs(self):
"""
URL or local file path to plotly.js bundle to use for image export.
If not specified, will default to CDN location.
"""
return self._plotlyjs
@plotlyjs.setter
def plotlyjs(self, val):
self._plotlyjs = val
self._shutdown_kaleido()
@property
def mathjax(self):
"""
URL to MathJax bundle needed for LaTeX rendering.
If not specified, LaTeX rendering support will be disabled.
"""
return self._mathjax
@mathjax.setter
def mathjax(self, val):
self._mathjax = val
self._shutdown_kaleido()
@property
def topojson(self):
"""
URL to the topojson files needed to render choropleth traces.
If not specified, will default to CDN location.
"""
return self._topojson
@topojson.setter
def topojson(self, val):
self._topojson = val
self._shutdown_kaleido()
@property
def mapbox_access_token(self):
"""
Mapbox access token required to render mapbox layers.
If not specified, mapbox layers will only be rendered
if a valid token is specified inline in the figure specification
"""
return self._mapbox_access_token
@mapbox_access_token.setter
def mapbox_access_token(self, val):
self._mapbox_access_token = val
self._shutdown_kaleido()
def _shutdown_kaleido(self):
self._tempfile = open(f"{self._tempdir.name}/index.html", "w")
self._tempfile.write(self.make_page_string())
self._tempfile.close()
def __del__(self):
try:
if os.path.exists(self._tempfile.name):
try:
os.remove(self._tempfile.name)
except Exception:
pass
except Exception:
pass
try:
if os.path.exists(self._tempdir.name):
try:
self._tempdir.cleanup()
except Exception:
pass
try:
os.rmdir(self._tempdir.name)
except Exception:
pass
except Exception:
pass