Skip to content

Commit 25e7945

Browse files
committed
fix a few bugs with setting topojson default
1 parent c990736 commit 25e7945

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

plotly/io/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ._html import to_html, write_html
1717
from ._renderers import renderers, show
1818
from . import base_renderers
19-
from ._defaults import defaults
19+
from ._kaleido import defaults
2020

2121
__all__ = [
2222
"to_image",
@@ -57,7 +57,7 @@
5757
"._html.write_html",
5858
"._renderers.renderers",
5959
"._renderers.show",
60-
"._defaults.defaults",
60+
"._kaleido.defaults",
6161
],
6262
)
6363

plotly/io/_kaleido.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import plotly
1010
from plotly.io._utils import validate_coerce_fig_to_dict, broadcast_args_to_dicts
11-
from plotly.io import defaults
11+
from plotly.io._defaults import defaults
1212

1313
ENGINE_SUPPORT_TIMELINE = "September 2025"
1414

@@ -98,19 +98,30 @@ def __setattr__(self, name, value):
9898
DeprecationWarning,
9999
stacklevel=2,
100100
)
101-
setattr(defaults, name, value)
102-
super(PlotlyScopeWithDeprecationWarnings, self).__setattr__(name, value)
101+
super().__setattr__(name, value)
103102

104103
def __getattr__(self, name):
105-
if name in defaults.__dict__:
104+
if hasattr(defaults, name):
106105
warnings.warn(
107106
kaleido_scope_default_warning_func(name),
108107
DeprecationWarning,
109108
stacklevel=2,
110109
)
111-
return super(PlotlyScopeWithDeprecationWarnings, self).__getattr__(name)
110+
return super().__getattr__(name)
111+
112+
# Ensure the new method of setting defaults is backwards compatible with Kaleido v0
113+
class DefaultsBackwardsCompatible(defaults.__class__):
114+
def __init__(self, scope):
115+
self._scope = scope
116+
super().__init__()
117+
def __setattr__(self, name, value):
118+
if not name == "_scope":
119+
if hasattr(self._scope, name) and getattr(self._scope, name) != value:
120+
setattr(self._scope, name, value)
121+
super().__setattr__(name, value)
112122

113123
scope = PlotlyScopeWithDeprecationWarnings()
124+
defaults = DefaultsBackwardsCompatible(scope)
114125
# Compute absolute path to the 'plotly/package_data/' directory
115126
root_dir = os.path.dirname(os.path.abspath(plotly.__file__))
116127
package_dir = os.path.join(root_dir, "package_data")
@@ -126,7 +137,7 @@ def __getattr__(self, name):
126137
# Show a deprecation warning if the old method of setting defaults is used
127138
class DefaultsDeprecationWarning:
128139
def __getattr__(self, name):
129-
if name in defaults.__dict__:
140+
if hasattr(defaults, name):
130141
warnings.warn(
131142
kaleido_scope_default_warning_func(name),
132143
DeprecationWarning,
@@ -137,7 +148,7 @@ def __getattr__(self, name):
137148
raise AttributeError(bad_attribute_error_msg_func(name))
138149

139150
def __setattr__(self, name, value):
140-
if name in defaults.__dict__:
151+
if hasattr(defaults, name):
141152
warnings.warn(
142153
kaleido_scope_default_warning_func(name),
143154
DeprecationWarning,
@@ -344,9 +355,7 @@ def to_image(
344355
height=height or defaults.default_height,
345356
scale=scale or defaults.default_scale,
346357
),
347-
topojson=Path(defaults.topojson).as_uri()
348-
if defaults.topojson
349-
else None,
358+
topojson=defaults.topojson,
350359
# mathjax=Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
351360
)
352361
except choreographer.errors.ChromeNotFoundError:
@@ -615,7 +624,7 @@ def write_images(
615624
height=d["height"] or defaults.default_height,
616625
scale=d["scale"] or defaults.default_scale,
617626
),
618-
"topojson": Path(defaults.topojson).as_uri() if defaults.topojson else None,
627+
"topojson": defaults.topojson,
619628
# "mathjax": Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
620629
}
621630
for d in arg_dicts

0 commit comments

Comments
 (0)