|
| 1 | +from contextlib import contextmanager |
1 | 2 | import os |
2 | 3 | import json |
3 | 4 | from pathlib import Path |
|
8 | 9 |
|
9 | 10 | import plotly |
10 | 11 | from plotly.io._utils import validate_coerce_fig_to_dict, broadcast_args_to_dicts |
11 | | -from plotly.io._defaults import defaults |
| 12 | +from plotly.io._defaults import defaults, DEFAULT_HEADERS |
12 | 13 | from _plotly_utils.optional_imports import get_module |
13 | 14 |
|
14 | 15 | kaleido = get_module("kaleido", should_load=True) |
|
35 | 36 | _KALEIDO_AVAILABLE = None |
36 | 37 |
|
37 | 38 |
|
| 39 | +@contextmanager |
| 40 | +def _ignore_default_kopts_warning(kopts): |
| 41 | + # A running kaleido sync server ignores kopts and warns about any. Only |
| 42 | + # warn when the user set an option, not for plotly's default headers. |
| 43 | + with warnings.catch_warnings(): |
| 44 | + if kopts == {"headers": DEFAULT_HEADERS}: |
| 45 | + warnings.filterwarnings( |
| 46 | + "ignore", message="The kopts argument", category=UserWarning |
| 47 | + ) |
| 48 | + yield |
| 49 | + |
| 50 | + |
38 | 51 | def kaleido_available() -> bool: |
39 | 52 | """ |
40 | 53 | Returns True if Kaleido version 1.0.0 or greater is installed, otherwise False. |
@@ -182,17 +195,18 @@ def to_image( |
182 | 195 | or defaults.default_height |
183 | 196 | ) |
184 | 197 |
|
185 | | - img_bytes = kaleido.calc_fig_sync( |
186 | | - fig_dict, |
187 | | - opts=dict( |
188 | | - format=format or defaults.default_format, |
189 | | - width=width, |
190 | | - height=height, |
191 | | - scale=scale or defaults.default_scale, |
192 | | - ), |
193 | | - topojson=defaults.topojson, |
194 | | - kopts=kopts, |
195 | | - ) |
| 198 | + with _ignore_default_kopts_warning(kopts): |
| 199 | + img_bytes = kaleido.calc_fig_sync( |
| 200 | + fig_dict, |
| 201 | + opts=dict( |
| 202 | + format=format or defaults.default_format, |
| 203 | + width=width, |
| 204 | + height=height, |
| 205 | + scale=scale or defaults.default_scale, |
| 206 | + ), |
| 207 | + topojson=defaults.topojson, |
| 208 | + kopts=kopts, |
| 209 | + ) |
196 | 210 | except ChromeNotFoundError: |
197 | 211 | raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG) |
198 | 212 |
|
@@ -430,10 +444,11 @@ def write_images( |
430 | 444 | kopts["mathjax"] = defaults.mathjax |
431 | 445 | if defaults.headers: |
432 | 446 | kopts["headers"] = defaults.headers |
433 | | - kaleido.write_fig_from_object_sync( |
434 | | - kaleido_specs, |
435 | | - kopts=kopts, |
436 | | - ) |
| 447 | + with _ignore_default_kopts_warning(kopts): |
| 448 | + kaleido.write_fig_from_object_sync( |
| 449 | + kaleido_specs, |
| 450 | + kopts=kopts, |
| 451 | + ) |
437 | 452 | except ChromeNotFoundError: |
438 | 453 | raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG) |
439 | 454 |
|
|
0 commit comments