@@ -154,15 +154,99 @@ def test_bytesio():
154154
155155def test_defaults ():
156156 """Test that image output defaults can be set using pio.defaults.*"""
157+ test_fig = go .Figure (fig )
158+ test_image_bytes = b"mock image data"
159+ 160+ # Check initial defaults
161+ assert pio .defaults .default_format == "png"
162+ assert pio .defaults .default_width == 700
163+ assert pio .defaults .default_height == 500
164+ assert pio .defaults .default_scale == 1
165+ assert pio .defaults .mathjax is None
166+ assert pio .defaults .topojson is None
167+ assert pio .defaults .plotlyjs is None
168+ 157169 try :
158- assert pio . defaults . default_format == "png"
170+ # Set new defaults
159171 pio .defaults .default_format = "svg"
172+ pio .defaults .default_width = 701
173+ pio .defaults .default_height = 501
174+ pio .defaults .default_scale = 2
175+ pio .defaults .mathjax = (
176+ "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js"
177+ )
178+ pio .defaults .topojson = "path/to/topojson/files/"
179+ pio .defaults .plotlyjs = "https://cdn.plot.ly/plotly-3.0.0.js"
180+ 181+ # Check that new defaults are saved
160182 assert pio .defaults .default_format == "svg"
161- result = pio .to_image (fig , format = "svg" , validate = False )
162- assert result .startswith (b"<svg" )
183+ assert pio .defaults .default_width == 701
184+ assert pio .defaults .default_height == 501
185+ assert pio .defaults .default_scale == 2
186+ assert (
187+ pio .defaults .mathjax
188+ == "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js"
189+ )
190+ assert pio .defaults .topojson == "path/to/topojson/files/"
191+ assert pio .defaults .plotlyjs == "https://cdn.plot.ly/plotly-3.0.0.js"
192+ 193+ if kaleido_major () > 0 :
194+ # Check that all the defaults values are passed through to the function call to calc_fig_sync
195+ with patch (
196+ "plotly.io._kaleido.kaleido.calc_fig_sync" ,
197+ return_value = test_image_bytes ,
198+ ) as mock_calc_fig :
199+ result = pio .to_image (test_fig , validate = False )
200+ 201+ # Verify calc_fig_sync was called with correct args
202+ # taken from pio.defaults
203+ mock_calc_fig .assert_called_once ()
204+ args , kwargs = mock_calc_fig .call_args
205+ assert args [0 ] == test_fig .to_dict ()
206+ assert kwargs ["opts" ]["format" ] == "svg"
207+ assert kwargs ["opts" ]["width" ] == 701
208+ assert kwargs ["opts" ]["height" ] == 501
209+ assert kwargs ["opts" ]["scale" ] == 2
210+ assert kwargs ["topojson" ] == "path/to/topojson/files/"
211+ # mathjax and plotlyjs are passed through in kopts
212+ assert (
213+ kwargs ["kopts" ]["mathjax" ]
214+ == "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js"
215+ )
216+ assert (
217+ kwargs ["kopts" ]["plotlyjs" ]
218+ == "https://cdn.plot.ly/plotly-3.0.0.js"
219+ )
220+ 221+ else :
222+ # Check that all the default values have been set in pio._kaleido.scope
223+ assert pio ._kaleido .scope .default_format == "svg"
224+ assert pio ._kaleido .scope .default_width == 701
225+ assert pio ._kaleido .scope .default_height == 501
226+ assert pio ._kaleido .scope .default_scale == 2
227+ assert (
228+ pio ._kaleido .scope .mathjax
229+ == "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js"
230+ )
231+ assert pio ._kaleido .scope .topojson == "path/to/topojson/files/"
232+ assert pio ._kaleido .scope .plotlyjs == "https://cdn.plot.ly/plotly-3.0.0.js"
233+ 163234 finally :
235+ # Reset defaults to original values and check that they are restored
164236 pio .defaults .default_format = "png"
237+ pio .defaults .default_width = 700
238+ pio .defaults .default_height = 500
239+ pio .defaults .default_scale = 1
240+ pio .defaults .mathjax = None
241+ pio .defaults .topojson = None
242+ pio .defaults .plotlyjs = None
165243 assert pio .defaults .default_format == "png"
244+ assert pio .defaults .default_width == 700
245+ assert pio .defaults .default_height == 500
246+ assert pio .defaults .default_scale == 1
247+ assert pio .defaults .mathjax is None
248+ assert pio .defaults .topojson is None
249+ assert pio .defaults .plotlyjs is None
166250
167251
168252def test_fig_write_image ():
0 commit comments