14
14
import plotly .io as pio
15
15
import json
16
16
17
+ project_root = os .path .dirname (
18
+ os .path .dirname (
19
+ os .path .dirname (
20
+ os .path .realpath (plotly .__file__ ))))
21
+
22
+ here = os .path .dirname (os .path .realpath (__file__ ))
23
+ html_filename = os .path .join (here , 'temp-plot.html' )
24
+
17
25
18
26
fig = {
19
27
'data' : [
64
72
65
73
download_image = 'Plotly.downloadImage'
66
74
75
+
67
76
class PlotlyOfflineBaseTestCase (TestCase ):
68
77
def tearDown (self ):
69
78
# Some offline tests produce an html file. Make sure we clean up :)
70
79
try :
71
- os .remove ('temp-plot.html' )
80
+ os .remove (os . path . join ( here , 'temp-plot.html' ) )
72
81
# Some tests that produce temp-plot.html
73
82
# also produce plotly.min.js
74
- os .remove ('plotly.min.js' )
83
+ os .remove (os . path . join ( here , 'plotly.min.js' ) )
75
84
except OSError :
76
85
pass
77
86
@@ -96,7 +105,8 @@ def test_default_plot_generates_expected_html(self):
96
105
fig ['layout' ],
97
106
cls = plotly .utils .PlotlyJSONEncoder )
98
107
99
- html = self ._read_html (plotly .offline .plot (fig , auto_open = False ))
108
+ html = self ._read_html (plotly .offline .plot (
109
+ fig , auto_open = False , filename = html_filename ))
100
110
101
111
# I don't really want to test the entire script output, so
102
112
# instead just make sure a few of the parts are in here?
@@ -120,6 +130,7 @@ def test_including_plotlyjs_truthy_html(self):
120
130
fig ,
121
131
include_plotlyjs = include_plotlyjs ,
122
132
output_type = 'file' ,
133
+ filename = html_filename ,
123
134
auto_open = False ))
124
135
125
136
self .assertIn (plotly_config_script , html )
@@ -149,6 +160,7 @@ def test_including_plotlyjs_false_html(self):
149
160
fig ,
150
161
include_plotlyjs = include_plotlyjs ,
151
162
output_type = 'file' ,
163
+ filename = html_filename ,
152
164
auto_open = False ))
153
165
154
166
self .assertNotIn (plotly_config_script , html )
@@ -173,6 +185,7 @@ def test_including_plotlyjs_cdn_html(self):
173
185
fig ,
174
186
include_plotlyjs = include_plotlyjs ,
175
187
output_type = 'file' ,
188
+ filename = html_filename ,
176
189
auto_open = False ))
177
190
self .assertIn (plotly_config_script , html )
178
191
self .assertNotIn (PLOTLYJS , html )
@@ -191,25 +204,29 @@ def test_including_plotlyjs_cdn_div(self):
191
204
self .assertNotIn (directory_script , html )
192
205
193
206
def test_including_plotlyjs_directory_html (self ):
194
- self .assertFalse (os .path .exists ('plotly.min.js' ))
207
+ self .assertFalse (
208
+ os .path .exists (os .path .join (here , 'plotly.min.js' )))
195
209
196
210
for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
197
211
html = self ._read_html (plotly .offline .plot (
198
212
fig ,
199
213
include_plotlyjs = include_plotlyjs ,
214
+ filename = html_filename ,
200
215
auto_open = False ))
201
216
self .assertIn (plotly_config_script , html )
202
217
self .assertNotIn (PLOTLYJS , html )
203
218
self .assertNotIn (cdn_script , html )
204
219
self .assertIn (directory_script , html )
205
220
206
221
# plot creates plotly.min.js in the output directory
207
- self .assertTrue (os .path .exists ('plotly.min.js' ))
222
+ self .assertTrue (
223
+ os .path .exists (os .path .join (here , 'plotly.min.js' )))
208
224
with open ('plotly.min.js' , 'r' ) as f :
209
225
self .assertEqual (f .read (), PLOTLYJS )
210
226
211
227
def test_including_plotlyjs_directory_div (self ):
212
- self .assertFalse (os .path .exists ('plotly.min.js' ))
228
+ self .assertFalse (
229
+ os .path .exists (os .path .join (here , 'plotly.min.js' )))
213
230
214
231
for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
215
232
html = plotly .offline .plot (
@@ -238,6 +255,7 @@ def test_including_plotlyjs_path_html(self):
238
255
fig ,
239
256
include_plotlyjs = include_plotlyjs ,
240
257
output_type = 'file' ,
258
+ filename = html_filename ,
241
259
auto_open = False ))
242
260
self .assertNotIn (PLOTLYJS , html )
243
261
self .assertNotIn (cdn_script , html )
@@ -271,8 +289,8 @@ def test_config(self):
271
289
config = dict (linkText = 'Plotly rocks!' ,
272
290
showLink = True ,
273
291
editable = True )
274
- html = self ._read_html (plotly .offline .plot (fig , config = config ,
275
- auto_open = False ))
292
+ html = self ._read_html (plotly .offline .plot (
293
+ fig , config = config , auto_open = False , filename = html_filename ))
276
294
self .assertIn ('"linkText": "Plotly rocks!"' , html )
277
295
self .assertIn ('"showLink": true' , html )
278
296
self .assertIn ('"editable": true' , html )
@@ -282,7 +300,7 @@ def test_config_bad_options(self):
282
300
283
301
def get_html ():
284
302
return self ._read_html (plotly .offline .plot (
285
- fig , config = config , auto_open = False ))
303
+ fig , config = config , auto_open = False , filename = html_filename ))
286
304
287
305
# Attempts to validate warning ran into
288
306
# https://bugs.python.org/issue29620, don't check warning for now.
@@ -293,7 +311,8 @@ def get_html():
293
311
294
312
@attr ('nodev' )
295
313
def test_plotlyjs_version (self ):
296
- with open ('js/package.json' , 'rt' ) as f :
314
+ path = os .path .join (project_root , 'js' , 'package.json' )
315
+ with open (path , 'rt' ) as f :
297
316
package_json = json .load (f )
298
317
expected_version = package_json ['dependencies' ]['plotly.js' ]
299
318
@@ -305,6 +324,7 @@ def test_include_mathjax_false_html(self):
305
324
fig ,
306
325
include_mathjax = False ,
307
326
output_type = 'file' ,
327
+ filename = html_filename ,
308
328
auto_open = False ))
309
329
310
330
self .assertIn (plotly_config_script , html )
@@ -328,6 +348,7 @@ def test_include_mathjax_cdn_html(self):
328
348
fig ,
329
349
include_mathjax = 'cdn' ,
330
350
output_type = 'file' ,
351
+ filename = html_filename ,
331
352
auto_open = False ))
332
353
333
354
self .assertIn (plotly_config_script , html )
@@ -352,6 +373,7 @@ def test_include_mathjax_path_html(self):
352
373
fig ,
353
374
include_mathjax = other_cdn ,
354
375
output_type = 'file' ,
376
+ filename = html_filename ,
355
377
auto_open = False ))
356
378
357
379
self .assertIn (plotly_config_script , html )
0 commit comments