1414import plotly .io as pio
1515import json
1616
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+
1725
1826fig = {
1927 'data' : [
6472
6573download_image = 'Plotly.downloadImage'
6674
75+
6776class PlotlyOfflineBaseTestCase (TestCase ):
6877 def tearDown (self ):
6978 # Some offline tests produce an html file. Make sure we clean up :)
7079 try :
71- os .remove ('temp-plot.html' )
80+ os .remove (os . path . join ( here , 'temp-plot.html' ) )
7281 # Some tests that produce temp-plot.html
7382 # also produce plotly.min.js
74- os .remove ('plotly.min.js' )
83+ os .remove (os . path . join ( here , 'plotly.min.js' ) )
7584 except OSError :
7685 pass
7786
@@ -96,7 +105,8 @@ def test_default_plot_generates_expected_html(self):
96105 fig ['layout' ],
97106 cls = plotly .utils .PlotlyJSONEncoder )
98107
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 ))
100110
101111 # I don't really want to test the entire script output, so
102112 # instead just make sure a few of the parts are in here?
@@ -120,6 +130,7 @@ def test_including_plotlyjs_truthy_html(self):
120130 fig ,
121131 include_plotlyjs = include_plotlyjs ,
122132 output_type = 'file' ,
133+ filename = html_filename ,
123134 auto_open = False ))
124135
125136 self .assertIn (plotly_config_script , html )
@@ -149,6 +160,7 @@ def test_including_plotlyjs_false_html(self):
149160 fig ,
150161 include_plotlyjs = include_plotlyjs ,
151162 output_type = 'file' ,
163+ filename = html_filename ,
152164 auto_open = False ))
153165
154166 self .assertNotIn (plotly_config_script , html )
@@ -173,6 +185,7 @@ def test_including_plotlyjs_cdn_html(self):
173185 fig ,
174186 include_plotlyjs = include_plotlyjs ,
175187 output_type = 'file' ,
188+ filename = html_filename ,
176189 auto_open = False ))
177190 self .assertIn (plotly_config_script , html )
178191 self .assertNotIn (PLOTLYJS , html )
@@ -191,25 +204,29 @@ def test_including_plotlyjs_cdn_div(self):
191204 self .assertNotIn (directory_script , html )
192205
193206 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' )))
195209
196210 for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
197211 html = self ._read_html (plotly .offline .plot (
198212 fig ,
199213 include_plotlyjs = include_plotlyjs ,
214+ filename = html_filename ,
200215 auto_open = False ))
201216 self .assertIn (plotly_config_script , html )
202217 self .assertNotIn (PLOTLYJS , html )
203218 self .assertNotIn (cdn_script , html )
204219 self .assertIn (directory_script , html )
205220
206221 # 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' )))
208224 with open ('plotly.min.js' , 'r' ) as f :
209225 self .assertEqual (f .read (), PLOTLYJS )
210226
211227 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' )))
213230
214231 for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
215232 html = plotly .offline .plot (
@@ -238,6 +255,7 @@ def test_including_plotlyjs_path_html(self):
238255 fig ,
239256 include_plotlyjs = include_plotlyjs ,
240257 output_type = 'file' ,
258+ filename = html_filename ,
241259 auto_open = False ))
242260 self .assertNotIn (PLOTLYJS , html )
243261 self .assertNotIn (cdn_script , html )
@@ -271,8 +289,8 @@ def test_config(self):
271289 config = dict (linkText = 'Plotly rocks!' ,
272290 showLink = True ,
273291 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 ))
276294 self .assertIn ('"linkText": "Plotly rocks!"' , html )
277295 self .assertIn ('"showLink": true' , html )
278296 self .assertIn ('"editable": true' , html )
@@ -282,7 +300,7 @@ def test_config_bad_options(self):
282300
283301 def get_html ():
284302 return self ._read_html (plotly .offline .plot (
285- fig , config = config , auto_open = False ))
303+ fig , config = config , auto_open = False , filename = html_filename ))
286304
287305 # Attempts to validate warning ran into
288306 # https://bugs.python.org/issue29620, don't check warning for now.
@@ -293,7 +311,8 @@ def get_html():
293311
294312 @attr ('nodev' )
295313 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 :
297316 package_json = json .load (f )
298317 expected_version = package_json ['dependencies' ]['plotly.js' ]
299318
@@ -305,6 +324,7 @@ def test_include_mathjax_false_html(self):
305324 fig ,
306325 include_mathjax = False ,
307326 output_type = 'file' ,
327+ filename = html_filename ,
308328 auto_open = False ))
309329
310330 self .assertIn (plotly_config_script , html )
@@ -328,6 +348,7 @@ def test_include_mathjax_cdn_html(self):
328348 fig ,
329349 include_mathjax = 'cdn' ,
330350 output_type = 'file' ,
351+ filename = html_filename ,
331352 auto_open = False ))
332353
333354 self .assertIn (plotly_config_script , html )
@@ -352,6 +373,7 @@ def test_include_mathjax_path_html(self):
352373 fig ,
353374 include_mathjax = other_cdn ,
354375 output_type = 'file' ,
376+ filename = html_filename ,
355377 auto_open = False ))
356378
357379 self .assertIn (plotly_config_script , html )
0 commit comments