Test that image output defaults can be set using pio.defaults.*
()
| 174 | |
| 175 | |
| 176 | def test_defaults(): |
| 177 | """Test that image output defaults can be set using pio.defaults.*""" |
| 178 | test_fig = go.Figure(fig) |
| 179 | test_image_bytes = b"mock image data" |
| 180 | |
| 181 | # Check initial defaults |
| 182 | assert pio.defaults.default_format == "png" |
| 183 | assert pio.defaults.default_width == 700 |
| 184 | assert pio.defaults.default_height == 500 |
| 185 | assert pio.defaults.default_scale == 1 |
| 186 | assert pio.defaults.mathjax is None |
| 187 | assert pio.defaults.topojson is None |
| 188 | assert pio.defaults.plotlyjs is None |
| 189 | |
| 190 | try: |
| 191 | # Set new defaults |
| 192 | pio.defaults.default_format = "svg" |
| 193 | pio.defaults.default_width = 701 |
| 194 | pio.defaults.default_height = 501 |
| 195 | pio.defaults.default_scale = 2 |
| 196 | pio.defaults.mathjax = ( |
| 197 | "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js" |
| 198 | ) |
| 199 | pio.defaults.topojson = "path/to/topojson/files/" |
| 200 | pio.defaults.plotlyjs = "https://cdn.plot.ly/plotly-3.0.0.js" |
| 201 | |
| 202 | # Check that new defaults are saved |
| 203 | assert pio.defaults.default_format == "svg" |
| 204 | assert pio.defaults.default_width == 701 |
| 205 | assert pio.defaults.default_height == 501 |
| 206 | assert pio.defaults.default_scale == 2 |
| 207 | assert ( |
| 208 | pio.defaults.mathjax |
| 209 | == "https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-svg.js" |
| 210 | ) |
| 211 | assert pio.defaults.topojson == "path/to/topojson/files/" |
| 212 | assert pio.defaults.plotlyjs == "https://cdn.plot.ly/plotly-3.0.0.js" |
| 213 | |
| 214 | if kaleido_major() > 0: |
| 215 | # Check that all the defaults values are passed through to the function call to calc_fig_sync |
| 216 | with patch( |
| 217 | "plotly.io._kaleido.kaleido.calc_fig_sync", |
| 218 | return_value=test_image_bytes, |
| 219 | ) as mock_calc_fig: |
| 220 | result = pio.to_image(test_fig, validate=False) |
| 221 | |
| 222 | # Verify calc_fig_sync was called with correct args |
| 223 | # taken from pio.defaults |
| 224 | mock_calc_fig.assert_called_once() |
| 225 | args, kwargs = mock_calc_fig.call_args |
| 226 | assert args[0] == test_fig.to_dict() |
| 227 | assert kwargs["opts"]["format"] == "svg" |
| 228 | assert kwargs["opts"]["width"] == 701 |
| 229 | assert kwargs["opts"]["height"] == 501 |
| 230 | assert kwargs["opts"]["scale"] == 2 |
| 231 | assert kwargs["topojson"] == "path/to/topojson/files/" |
| 232 | # mathjax and plotlyjs are passed through in kopts |
| 233 | assert ( |
nothing calls this directly
no test coverage detected