Skip to content

Commit 902bddf

Browse files
committed
Don't warn about plotly's default kopts under a kaleido sync server
1 parent 23ed573 commit 902bddf

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Fixed
88
- Fix concurrent first access to lazily initialized graph object properties, which could raise `ValueError("Invalid value")` [[#3441](https://github.com/plotly/plotly.py/issues/3441)], with thanks to @hb1915 for the contribution!
99
- Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5765](https://github.com/plotly/plotly.py/issues/5765)], with thanks to @Irahan2 for the contribution!
10+
- Fix `write_image`, `to_image` and `write_images` emitting `UserWarning: The kopts argument is ignored if using a server.` on every call under `kaleido.start_sync_server()` when no Kaleido options are set; the warning still appears when `plotly.io.defaults` sets `plotlyjs`, `mathjax` or custom `headers` [[#5786](https://github.com/plotly/plotly.py/issues/5786)], with thanks to @jayzhou2309 for the contribution!
1011

1112

1213
## [7.1.0] - 2026-09-15

‎plotly/io/_defaults.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Default settings for image generation
22

3+
# This header is necessary to comply with Open Street Map tile policy:
4+
# https://openstreetmap.github.io/owg-website/policies/tiles/#31-identification
5+
DEFAULT_HEADERS = {"X-Requested-With": "plotly.py"}
6+
37

48
class _Defaults(object):
59
"""
@@ -14,9 +18,7 @@ def __init__(self):
1418
self.mathjax = None
1519
self.topojson = None
1620
self.plotlyjs = None
17-
# This header is necessary to comply with Open Street Map tile policy:
18-
# https://openstreetmap.github.io/owg-website/policies/tiles/#31-identification
19-
self.headers = {"X-Requested-With": "plotly.py"}
21+
self.headers = dict(DEFAULT_HEADERS)
2022

2123

2224
defaults = _Defaults()

‎plotly/io/_kaleido.py‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
import os
23
import json
34
from pathlib import Path
@@ -8,7 +9,7 @@
89

910
import plotly
1011
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
1213
from _plotly_utils.optional_imports import get_module
1314

1415
kaleido = get_module("kaleido", should_load=True)
@@ -35,6 +36,18 @@
3536
_KALEIDO_AVAILABLE = None
3637

3738

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+
3851
def kaleido_available() -> bool:
3952
"""
4053
Returns True if Kaleido version 1.0.0 or greater is installed, otherwise False.
@@ -182,17 +195,18 @@ def to_image(
182195
or defaults.default_height
183196
)
184197

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+
)
196210
except ChromeNotFoundError:
197211
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
198212

@@ -430,10 +444,11 @@ def write_images(
430444
kopts["mathjax"] = defaults.mathjax
431445
if defaults.headers:
432446
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+
)
437452
except ChromeNotFoundError:
438453
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
439454

0 commit comments

Comments
 (0)