@@ -438,7 +438,7 @@ def _ipython_display_(self):
438
438
else :
439
439
print (repr (self ))
440
440
441
- def update (self , dict1 = None , ** kwargs ):
441
+ def update (self , dict1 = None , overwrite = False , ** kwargs ):
442
442
"""
443
443
Update the properties of the figure with a dict and/or with
444
444
keyword arguments.
@@ -450,6 +450,10 @@ def update(self, dict1=None, **kwargs):
450
450
----------
451
451
dict1 : dict
452
452
Dictionary of properties to be updated
453
+ overwrite: bool
454
+ If True, overwrite existing properties. If False, apply updates
455
+ to existing properties recursively, preserving existing
456
+ properties that are not specified in the update operation.
453
457
kwargs :
454
458
Keyword/value pair of properties to be updated
455
459
@@ -484,10 +488,11 @@ def update(self, dict1=None, **kwargs):
484
488
if d :
485
489
for k , v in d .items ():
486
490
update_target = self [k ]
487
- if update_target == ():
488
- # existing data or frames property is empty
489
- # In this case we accept the v as is.
491
+ if update_target == () or overwrite :
490
492
if k == "data" :
493
+ # Overwrite all traces as special due to
494
+ # restrictions on trace assignment
495
+ self .data = ()
491
496
self .add_traces (v )
492
497
else :
493
498
# Accept v
@@ -843,7 +848,14 @@ def for_each_trace(self, fn, selector=None, row=None, col=None, secondary_y=None
843
848
return self
844
849
845
850
def update_traces (
846
- self , patch = None , selector = None , row = None , col = None , secondary_y = None , ** kwargs
851
+ self ,
852
+ patch = None ,
853
+ selector = None ,
854
+ row = None ,
855
+ col = None ,
856
+ secondary_y = None ,
857
+ overwrite = False ,
858
+ ** kwargs
847
859
):
848
860
"""
849
861
Perform a property update operation on all traces that satisfy the
@@ -877,6 +889,10 @@ def update_traces(
877
889
created using plotly.subplots.make_subplots. See the docstring
878
890
for the specs argument to make_subplots for more info on
879
891
creating subplots with secondary y-axes.
892
+ overwrite: bool
893
+ If True, overwrite existing properties. If False, apply updates
894
+ to existing properties recursively, preserving existing
895
+ properties that are not specified in the update operation.
880
896
**kwargs
881
897
Additional property updates to apply to each selected trace. If
882
898
a property is specified in both patch and in **kwargs then the
@@ -890,10 +906,10 @@ def update_traces(
890
906
for trace in self .select_traces (
891
907
selector = selector , row = row , col = col , secondary_y = secondary_y
892
908
):
893
- trace .update (patch , ** kwargs )
909
+ trace .update (patch , overwrite = overwrite , ** kwargs )
894
910
return self
895
911
896
- def update_layout (self , dict1 = None , ** kwargs ):
912
+ def update_layout (self , dict1 = None , overwrite = False , ** kwargs ):
897
913
"""
898
914
Update the properties of the figure's layout with a dict and/or with
899
915
keyword arguments.
@@ -905,6 +921,10 @@ def update_layout(self, dict1=None, **kwargs):
905
921
----------
906
922
dict1 : dict
907
923
Dictionary of properties to be updated
924
+ overwrite: bool
925
+ If True, overwrite existing properties. If False, apply updates
926
+ to existing properties recursively, preserving existing
927
+ properties that are not specified in the update operation.
908
928
kwargs :
909
929
Keyword/value pair of properties to be updated
910
930
@@ -913,7 +933,7 @@ def update_layout(self, dict1=None, **kwargs):
913
933
BaseFigure
914
934
The Figure object that the update_layout method was called on
915
935
"""
916
- self .layout .update (dict1 , ** kwargs )
936
+ self .layout .update (dict1 , overwrite = overwrite , ** kwargs )
917
937
return self
918
938
919
939
def _select_layout_subplots_by_prefix (
@@ -2697,7 +2717,7 @@ def _is_dict_list(v):
2697
2717
return isinstance (v , list ) and len (v ) > 0 and isinstance (v [0 ], dict )
2698
2718
2699
2719
@staticmethod
2700
- def _perform_update (plotly_obj , update_obj ):
2720
+ def _perform_update (plotly_obj , update_obj , overwrite = False ):
2701
2721
"""
2702
2722
Helper to support the update() methods on :class:`BaseFigure` and
2703
2723
:class:`BasePlotlyType`
@@ -2747,6 +2767,12 @@ def _perform_update(plotly_obj, update_obj):
2747
2767
# ------------------------
2748
2768
for key in update_obj :
2749
2769
val = update_obj [key ]
2770
+
2771
+ if overwrite :
2772
+ # Don't recurse and assign property as-is
2773
+ plotly_obj [key ] = val
2774
+ continue
2775
+
2750
2776
validator = plotly_obj ._get_prop_validator (key )
2751
2777
2752
2778
if isinstance (validator , CompoundValidator ) and isinstance (val , dict ):
@@ -3530,7 +3556,7 @@ def _raise_on_invalid_property_error(self, *args):
3530
3556
)
3531
3557
)
3532
3558
3533
- def update (self , dict1 = None , ** kwargs ):
3559
+ def update (self , dict1 = None , overwrite = False , ** kwargs ):
3534
3560
"""
3535
3561
Update the properties of an object with a dict and/or with
3536
3562
keyword arguments.
@@ -3542,6 +3568,10 @@ def update(self, dict1=None, **kwargs):
3542
3568
----------
3543
3569
dict1 : dict
3544
3570
Dictionary of properties to be updated
3571
+ overwrite: bool
3572
+ If True, overwrite existing properties. If False, apply updates
3573
+ to existing properties recursively, preserving existing
3574
+ properties that are not specified in the update operation.
3545
3575
kwargs :
3546
3576
Keyword/value pair of properties to be updated
3547
3577
@@ -3552,11 +3582,11 @@ def update(self, dict1=None, **kwargs):
3552
3582
"""
3553
3583
if self .figure :
3554
3584
with self .figure .batch_update ():
3555
- BaseFigure ._perform_update (self , dict1 )
3556
- BaseFigure ._perform_update (self , kwargs )
3585
+ BaseFigure ._perform_update (self , dict1 , overwrite = overwrite )
3586
+ BaseFigure ._perform_update (self , kwargs , overwrite = overwrite )
3557
3587
else :
3558
- BaseFigure ._perform_update (self , dict1 )
3559
- BaseFigure ._perform_update (self , kwargs )
3588
+ BaseFigure ._perform_update (self , dict1 , overwrite = overwrite )
3589
+ BaseFigure ._perform_update (self , kwargs , overwrite = overwrite )
3560
3590
3561
3591
return self
3562
3592
0 commit comments