-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_offline.py
436 lines (359 loc) · 15 KB
/
test_offline.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
test__offline
"""
import json
import os
from unittest import TestCase
import pytest
import plotly
import plotly.io as pio
from plotly.io._utils import plotly_cdn_url
packages_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(plotly.__file__))))
)
here = os.path.dirname(os.path.realpath(__file__))
html_filename = os.path.join(here, "temp-plot.html")
fig = {
"data": [plotly.graph_objs.Scatter(x=[1, 2, 3], y=[10, 20, 30])],
"layout": plotly.graph_objs.Layout(title="offline plot"),
}
fig_frames = {
"data": [plotly.graph_objs.Scatter(x=[1, 2, 3], y=[10, 20, 30])],
"layout": plotly.graph_objs.Layout(title="offline plot"),
"frames": [{"layout": {"title": "frame 1"}}],
}
PLOTLYJS = plotly.offline.get_plotlyjs()
plotly_config_script = """\
<script type="text/javascript">\
window.PlotlyConfig = {MathJaxConfig: 'local'};</script>"""
cdn_script = '<script charset="utf-8" src="{cdn_url}"></script>'.format(
cdn_url=plotly_cdn_url()
)
directory_script = '<script charset="utf-8" src="plotly.min.js"></script>'
mathjax_cdn = "https://cdnjs.cloudflare.com" "/ajax/libs/mathjax/2.7.5/MathJax.js"
mathjax_config_str = "?config=TeX-AMS-MML_SVG"
mathjax_cdn_script = '<script src="{cdn}{config}"></script>'.format(
cdn=mathjax_cdn, config=mathjax_config_str
)
mathjax_font = "STIX-Web"
add_frames = "Plotly.addFrames"
do_auto_play = "Plotly.animate"
download_image = "Plotly.downloadImage"
class PlotlyOfflineBaseTestCase(TestCase):
def tearDown(self):
# Some offline tests produce an html file. Make sure we clean up :)
try:
os.remove(os.path.join(here, "temp-plot.html"))
# Some tests that produce temp-plot.html
# also produce plotly.min.js
os.remove(os.path.join(here, "plotly.min.js"))
except OSError:
pass
class PlotlyOfflineTestCase(PlotlyOfflineBaseTestCase):
def setUp(self):
pio.templates.default = None
def tearDown(self):
pio.templates.default = "plotly"
super().tearDown()
def _read_html(self, file_url):
"""Read and return the HTML contents from a file_url
in the form e.g. file:///Users/chriddyp/Repos/plotly.py/plotly-temp.html
"""
with open(file_url.replace("file://", "").replace(" ", "")) as f:
return f.read()
def test_default_plot_generates_expected_html(self):
layout_json = pio.json.to_json_plotly(fig["layout"])
html = self._read_html(
plotly.offline.plot(fig, auto_open=False, filename=html_filename)
)
# I don't really want to test the entire script output, so
# instead just make sure a few of the parts are in here?
self.assertIn("Plotly.newPlot", html) # plot command is in there
x_data = '"x":[1,2,3]'
y_data = '"y":[10,20,30]'
self.assertTrue(x_data in html and y_data in html) # data in there
self.assertIn(layout_json, html) # so is layout
self.assertIn(plotly_config_script, html) # so is config
self.assertIn(PLOTLYJS, html) # and the source code
# and it's an <html> doc
self.assertTrue(html.startswith("<html>") and html.endswith("</html>"))
def test_including_plotlyjs_truthy_html(self):
# For backwards compatibility all truthy values that aren't otherwise
# recognized are considered true
for include_plotlyjs in [True, 34, "non-empty-str"]:
html = self._read_html(
plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_truthy_div(self):
# For backwards compatibility all truthy values that aren't otherwise
# recognized are considered true
for include_plotlyjs in [True, 34, "non-empty-str"]:
html = plotly.offline.plot(
fig, include_plotlyjs=include_plotlyjs, output_type="div"
)
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_false_html(self):
# For backwards compatibility all truthy values that aren't otherwise
# recognized are considered true
for include_plotlyjs in [False, 0, ""]:
html = self._read_html(
plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertNotIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_false_div(self):
for include_plotlyjs in [False, 0, ""]:
html = plotly.offline.plot(
fig, include_plotlyjs=include_plotlyjs, output_type="div"
)
self.assertNotIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_cdn_html(self):
for include_plotlyjs in ["cdn", "CDN", "Cdn"]:
html = self._read_html(
plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_cdn_div(self):
for include_plotlyjs in ["cdn", "CDN", "Cdn"]:
html = plotly.offline.plot(
fig, include_plotlyjs=include_plotlyjs, output_type="div"
)
self.assertIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertIn(cdn_script, html)
self.assertNotIn(directory_script, html)
def test_including_plotlyjs_directory_html(self):
self.assertFalse(os.path.exists(os.path.join(here, "plotly.min.js")))
for include_plotlyjs in ["directory", "Directory", "DIRECTORY"]:
html = self._read_html(
plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertIn(directory_script, html)
# plot creates plotly.min.js in the output directory
self.assertTrue(os.path.exists(os.path.join(here, "plotly.min.js")))
with open(os.path.join(here, "plotly.min.js"), "r") as f:
self.assertEqual(f.read(), PLOTLYJS)
def test_including_plotlyjs_directory_div(self):
self.assertFalse(os.path.exists(os.path.join(here, "plotly.min.js")))
for include_plotlyjs in ["directory", "Directory", "DIRECTORY"]:
html = plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
output_type="div",
auto_open=False,
)
self.assertIn(plotly_config_script, html)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertIn(directory_script, html)
# plot does NOT create a plotly.min.js file in the output directory
# when output_type is div
self.assertFalse(os.path.exists("plotly.min.js"))
def test_including_plotlyjs_path_html(self):
for include_plotlyjs in [
(
"https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/"
"plotly.min.js"
),
"subpath/to/plotly.min.js",
"something.js",
]:
html = self._read_html(
plotly.offline.plot(
fig,
include_plotlyjs=include_plotlyjs,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
self.assertIn(include_plotlyjs, html)
def test_including_plotlyjs_path_div(self):
for include_plotlyjs in [
(
"https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/"
"plotly.min.js"
),
"subpath/to/plotly.min.js",
"something.js",
]:
html = plotly.offline.plot(
fig, include_plotlyjs=include_plotlyjs, output_type="div"
)
self.assertNotIn(PLOTLYJS, html)
self.assertNotIn(cdn_script, html)
self.assertNotIn(directory_script, html)
self.assertIn(include_plotlyjs, html)
def test_div_output(self):
html = plotly.offline.plot(fig, output_type="div", auto_open=False)
self.assertNotIn("<html>", html)
self.assertNotIn("</html>", html)
self.assertTrue(html.startswith("<div>") and html.endswith("</div>"))
def test_config(self):
config = dict(linkText="Plotly rocks!", showLink=True, editable=True)
html = self._read_html(
plotly.offline.plot(
fig, config=config, auto_open=False, filename=html_filename
)
)
self.assertIn('"linkText": "Plotly rocks!"', html)
self.assertIn('"showLink": true', html)
self.assertIn('"editable": true', html)
def test_config_bad_options(self):
config = dict(bogus=42)
def get_html():
return self._read_html(
plotly.offline.plot(
fig, config=config, auto_open=False, filename=html_filename
)
)
# Attempts to validate warning ran into
# https://bugs.python.org/issue29620, don't check warning for now.
# Revisit when we move to pytest
html = get_html()
self.assertIn('"bogus": 42', html)
def test_include_mathjax_false_html(self):
html = self._read_html(
plotly.offline.plot(
fig,
include_mathjax=False,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(mathjax_cdn_script, html)
self.assertNotIn(mathjax_font, html)
def test_include_mathjax_false_div(self):
html = plotly.offline.plot(fig, include_mathjax=False, output_type="div")
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(mathjax_cdn_script, html)
self.assertNotIn(mathjax_font, html)
def test_include_mathjax_cdn_html(self):
html = self._read_html(
plotly.offline.plot(
fig,
include_mathjax="cdn",
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertIn(mathjax_cdn_script, html)
self.assertIn(mathjax_font, html)
def test_include_mathjax_cdn_div(self):
html = plotly.offline.plot(fig, include_mathjax="cdn", output_type="div")
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertIn(mathjax_cdn_script, html)
self.assertIn(mathjax_font, html)
def test_include_mathjax_path_html(self):
other_cdn = "http://another/cdn/MathJax.js"
html = self._read_html(
plotly.offline.plot(
fig,
include_mathjax=other_cdn,
output_type="file",
filename=html_filename,
auto_open=False,
)
)
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(mathjax_cdn_script, html)
self.assertIn(other_cdn + mathjax_config_str, html)
self.assertIn(mathjax_font, html)
def test_include_mathjax_path_div(self):
other_cdn = "http://another/cdn/MathJax.js"
html = plotly.offline.plot(fig, include_mathjax=other_cdn, output_type="div")
self.assertIn(plotly_config_script, html)
self.assertIn(PLOTLYJS, html)
self.assertNotIn(mathjax_cdn_script, html)
self.assertIn(other_cdn + mathjax_config_str, html)
self.assertIn(mathjax_font, html)
def test_auto_play(self):
html = plotly.offline.plot(fig_frames, output_type="div")
self.assertIn(add_frames, html)
self.assertIn(do_auto_play, html)
def test_no_auto_play(self):
html = plotly.offline.plot(fig_frames, output_type="div", auto_play=False)
self.assertIn(add_frames, html)
self.assertNotIn(do_auto_play, html)
def test_animation_opts(self):
animation_opts = {"frame": {"duration": 5000}}
expected_opts_str = json.dumps(animation_opts)
# When auto_play is False, animation options are skipped
html = plotly.offline.plot(
fig_frames,
output_type="div",
auto_play=False,
animation_opts=animation_opts,
)
self.assertIn(add_frames, html)
self.assertNotIn(do_auto_play, html)
self.assertNotIn(expected_opts_str, html)
# When auto_play is True, animation options are included
html = plotly.offline.plot(
fig_frames, output_type="div", auto_play=True, animation_opts=animation_opts
)
self.assertIn(add_frames, html)
self.assertIn(do_auto_play, html)
self.assertIn(expected_opts_str, html)
def test_download_image(self):
# Not download image by default
html = plotly.offline.plot(fig_frames, output_type="div", auto_play=False)
self.assertNotIn(download_image, html)
# Request download image
html = plotly.offline.plot(
fig_frames, output_type="div", auto_play=False, image="png"
)
self.assertIn(download_image, html)