2121 )
2222}
2323
24+
25+ resize_code_strings = [
26+ 'window.addEventListener("resize", ' ,
27+ 'Plotly.Plots.resize('
28+ ]
29+
30+
2431PLOTLYJS = plotly .offline .offline .get_plotlyjs ()
2532
33+ cdn_script = ('<script src="https://cdn.plot.ly/plotly-latest.min.js">'
34+ '</script>' )
35+
36+ directory_script = '<script src="plotly.min.js"></script>'
37+
2638
2739class PlotlyOfflineBaseTestCase (TestCase ):
2840 def tearDown (self ):
2941 # Some offline tests produce an html file. Make sure we clean up :)
3042 try :
3143 os .remove ('temp-plot.html' )
44+ # Some tests that produce temp-plot.html
45+ # also produce plotly.min.js
46+ os .remove ('plotly.min.js' )
3247 except OSError :
3348 pass
3449
@@ -64,10 +79,142 @@ def test_default_plot_generates_expected_html(self):
6479 # and it's an <html> doc
6580 self .assertTrue (html .startswith ('<html>' ) and html .endswith ('</html>' ))
6681
67- def test_including_plotlyjs (self ):
68- html = self ._read_html (plotly .offline .plot (fig , include_plotlyjs = False ,
69- auto_open = False ))
70- self .assertNotIn (PLOTLYJS , html )
82+ def test_including_plotlyjs_truthy_html (self ):
83+ # For backwards compatibility all truthy values that aren't otherwise
84+ # recognized are considered true
85+ for include_plotlyjs in [True , 34 , 'non-empty-str' ]:
86+ html = self ._read_html (plotly .offline .plot (
87+ fig ,
88+ include_plotlyjs = include_plotlyjs ,
89+ output_type = 'file' ,
90+ auto_open = False ))
91+ self .assertIn (PLOTLYJS , html )
92+ self .assertNotIn (cdn_script , html )
93+ self .assertNotIn (directory_script , html )
94+
95+ def test_including_plotlyjs_truthy_div (self ):
96+ # For backwards compatibility all truthy values that aren't otherwise
97+ # recognized are considered true
98+ for include_plotlyjs in [True , 34 , 'non-empty-str' ]:
99+ html = plotly .offline .plot (
100+ fig ,
101+ include_plotlyjs = include_plotlyjs ,
102+ output_type = 'div' )
103+ self .assertIn (PLOTLYJS , html )
104+ self .assertNotIn (cdn_script , html )
105+ self .assertNotIn (directory_script , html )
106+
107+ def test_including_plotlyjs_false_html (self ):
108+ # For backwards compatibility all truthy values that aren't otherwise
109+ # recognized are considered true
110+ for include_plotlyjs in [False , 0 , '' ]:
111+ html = self ._read_html (plotly .offline .plot (
112+ fig ,
113+ include_plotlyjs = include_plotlyjs ,
114+ output_type = 'file' ,
115+ auto_open = False ))
116+ self .assertNotIn (PLOTLYJS , html )
117+ self .assertNotIn (cdn_script , html )
118+ self .assertNotIn (directory_script , html )
119+
120+ def test_including_plotlyjs_false_div (self ):
121+ for include_plotlyjs in [False , 0 , '' ]:
122+ html = plotly .offline .plot (
123+ fig ,
124+ include_plotlyjs = include_plotlyjs ,
125+ output_type = 'div' )
126+ self .assertNotIn (PLOTLYJS , html )
127+ self .assertNotIn (cdn_script , html )
128+ self .assertNotIn (directory_script , html )
129+
130+ def test_including_plotlyjs_cdn_html (self ):
131+ for include_plotlyjs in ['cdn' , 'CDN' , 'Cdn' ]:
132+ html = self ._read_html (plotly .offline .plot (
133+ fig ,
134+ include_plotlyjs = include_plotlyjs ,
135+ output_type = 'file' ,
136+ auto_open = False ))
137+ self .assertNotIn (PLOTLYJS , html )
138+ self .assertIn (cdn_script , html )
139+ self .assertNotIn (directory_script , html )
140+
141+ def test_including_plotlyjs_cdn_div (self ):
142+ for include_plotlyjs in ['cdn' , 'CDN' , 'Cdn' ]:
143+ html = plotly .offline .plot (
144+ fig ,
145+ include_plotlyjs = include_plotlyjs ,
146+ output_type = 'div' )
147+ self .assertNotIn (PLOTLYJS , html )
148+ self .assertIn (cdn_script , html )
149+ self .assertNotIn (directory_script , html )
150+
151+ def test_including_plotlyjs_directory_html (self ):
152+ self .assertFalse (os .path .exists ('plotly.min.js' ))
153+
154+ for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
155+ html = self ._read_html (plotly .offline .plot (
156+ fig ,
157+ include_plotlyjs = include_plotlyjs ,
158+ auto_open = False ))
159+ self .assertNotIn (PLOTLYJS , html )
160+ self .assertNotIn (cdn_script , html )
161+ self .assertIn (directory_script , html )
162+
163+ # plot creates plotly.min.js in the output directory
164+ self .assertTrue (os .path .exists ('plotly.min.js' ))
165+ with open ('plotly.min.js' , 'r' ) as f :
166+ self .assertEqual (f .read (), PLOTLYJS )
167+
168+ def test_including_plotlyjs_directory_div (self ):
169+ self .assertFalse (os .path .exists ('plotly.min.js' ))
170+
171+ for include_plotlyjs in ['directory' , 'Directory' , 'DIRECTORY' ]:
172+ html = plotly .offline .plot (
173+ fig ,
174+ include_plotlyjs = include_plotlyjs ,
175+ output_type = 'div' ,
176+ auto_open = False )
177+
178+ self .assertNotIn (PLOTLYJS , html )
179+ self .assertNotIn (cdn_script , html )
180+ self .assertIn (directory_script , html )
181+
182+ # plot does NOT create a plotly.min.js file in the output directory
183+ # when output_type is div
184+ self .assertFalse (os .path .exists ('plotly.min.js' ))
185+
186+ def test_including_plotlyjs_path_html (self ):
187+ for include_plotlyjs in [
188+ ('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/'
189+ 'plotly.min.js' ),
190+ 'subpath/to/plotly.min.js' ,
191+ 'something.js' ]:
192+
193+ html = self ._read_html (plotly .offline .plot (
194+ fig ,
195+ include_plotlyjs = include_plotlyjs ,
196+ output_type = 'file' ,
197+ auto_open = False ))
198+ self .assertNotIn (PLOTLYJS , html )
199+ self .assertNotIn (cdn_script , html )
200+ self .assertNotIn (directory_script , html )
201+ self .assertIn (include_plotlyjs , html )
202+
203+ def test_including_plotlyjs_path_div (self ):
204+ for include_plotlyjs in [
205+ ('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/'
206+ 'plotly.min.js' ),
207+ 'subpath/to/plotly.min.js' ,
208+ 'something.js' ]:
209+
210+ html = plotly .offline .plot (
211+ fig ,
212+ include_plotlyjs = include_plotlyjs ,
213+ output_type = 'div' )
214+ self .assertNotIn (PLOTLYJS , html )
215+ self .assertNotIn (cdn_script , html )
216+ self .assertNotIn (directory_script , html )
217+ self .assertIn (include_plotlyjs , html )
71218
72219 def test_div_output (self ):
73220 html = plotly .offline .plot (fig , output_type = 'div' , auto_open = False )
@@ -77,10 +224,7 @@ def test_div_output(self):
77224 self .assertTrue (html .startswith ('<div>' ) and html .endswith ('</div>' ))
78225
79226 def test_autoresizing (self ):
80- resize_code_strings = [
81- 'window.addEventListener("resize", ' ,
82- 'Plotly.Plots.resize('
83- ]
227+
84228 # If width or height wasn't specified, then we add a window resizer
85229 html = self ._read_html (plotly .offline .plot (fig , auto_open = False ))
86230 for resize_code_string in resize_code_strings :
@@ -96,6 +240,28 @@ def test_autoresizing(self):
96240 for resize_code_string in resize_code_strings :
97241 self .assertNotIn (resize_code_string , html )
98242
243+ def test_autoresizing_div (self ):
244+
245+ # If width or height wasn't specified, then we add a window resizer
246+ for include_plotlyjs in [True , False , 'cdn' , 'directory' ]:
247+ html = plotly .offline .plot (fig ,
248+ output_type = 'div' ,
249+ include_plotlyjs = include_plotlyjs )
250+
251+ for resize_code_string in resize_code_strings :
252+ self .assertIn (resize_code_string , html )
253+
254+ # If width or height was specified, then we don't resize
255+ html = plotly .offline .plot ({
256+ 'data' : fig ['data' ],
257+ 'layout' : {
258+ 'width' : 500 , 'height' : 500
259+ }
260+ }, output_type = 'div' )
261+
262+ for resize_code_string in resize_code_strings :
263+ self .assertNotIn (resize_code_string , html )
264+
99265 def test_config (self ):
100266 config = dict (linkText = 'Plotly rocks!' ,
101267 editable = True )
0 commit comments