88
99import plotly
1010from 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
1313ENGINE_SUPPORT_TIMELINE = "September 2025"
1414
@@ -98,35 +98,54 @@ 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+
118+ def __setattr__ (self , name , value ):
119+ if not name == "_scope" :
120+ if (
121+ hasattr (self ._scope , name )
122+ and getattr (self ._scope , name ) != value
123+ ):
124+ setattr (self ._scope , name , value )
125+ super ().__setattr__ (name , value )
112126
113127 scope = PlotlyScopeWithDeprecationWarnings ()
128+ defaults = DefaultsBackwardsCompatible (scope )
114129 # Compute absolute path to the 'plotly/package_data/' directory
115130 root_dir = os .path .dirname (os .path .abspath (plotly .__file__ ))
116131 package_dir = os .path .join (root_dir , "package_data" )
117132 scope .plotlyjs = os .path .join (package_dir , "plotly.min.js" )
118133 if scope .mathjax is None :
119- scope .mathjax = (
120- "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
121- )
134+ with warnings .catch_warnings ():
135+ warnings .filterwarnings (
136+ "ignore" , message = ".*scope\.mathjax.*" , category = DeprecationWarning
137+ )
138+ scope .mathjax = (
139+ "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
140+ )
122141 else :
123142 # Kaleido v1
124143 import kaleido
125144
126145 # Show a deprecation warning if the old method of setting defaults is used
127146 class DefaultsDeprecationWarning :
128147 def __getattr__ (self , name ):
129- if name in defaults . __dict__ :
148+ if hasattr ( defaults , name ) :
130149 warnings .warn (
131150 kaleido_scope_default_warning_func (name ),
132151 DeprecationWarning ,
@@ -137,7 +156,7 @@ def __getattr__(self, name):
137156 raise AttributeError (bad_attribute_error_msg_func (name ))
138157
139158 def __setattr__ (self , name , value ):
140- if name in defaults . __dict__ :
159+ if hasattr ( defaults , name ) :
141160 warnings .warn (
142161 kaleido_scope_default_warning_func (name ),
143162 DeprecationWarning ,
@@ -344,9 +363,7 @@ def to_image(
344363 height = height or defaults .default_height ,
345364 scale = scale or defaults .default_scale ,
346365 ),
347- topojson = Path (defaults .topojson ).as_uri ()
348- if defaults .topojson
349- else None ,
366+ topojson = defaults .topojson ,
350367 # mathjax=Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
351368 )
352369 except choreographer .errors .ChromeNotFoundError :
@@ -615,7 +632,7 @@ def write_images(
615632 height = d ["height" ] or defaults .default_height ,
616633 scale = d ["scale" ] or defaults .default_scale ,
617634 ),
618- "topojson" : Path ( defaults .topojson ). as_uri () if defaults . topojson else None ,
635+ "topojson" : defaults .topojson ,
619636 # "mathjax": Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
620637 }
621638 for d in arg_dicts
0 commit comments