How to copy Figure or Trace object? Deepcopy no longer working

I’m trying to create a copy for caching purposes, so I can load and make slight modifications to some traces without generating a new Figure which would take a lot of time in my Dash app.

Deepcopy used to be implemented for Figure, but it was removed somewhere along the way with recent updates. Regenerating from figure.to_dict() is unfortunately way too slow vs my prior use of deepcopy. A simple copy does not replicate the traces, instead only creating pointers.

Any suggestions? Creating copies of Traces seems to be the bottleneck here

Hi @jab551,

I just created a performance issue that I think covers this usecase: https://github.com/plotly/plotly.py/issues/1078

With this optimization you would be able to write:

fig2 = go.Figure(fig1)

And it would perform the deep copy underneath and not re-trigger the validation of all of the properties in fig1, which I believe is what’s slowing things down right now.

At this point, I think your only real option is to use raw dictionaries (rather that graph objects) for performance critical code.

1 Like

Thanks @jmmease for creating this issue. Using raw dicts for now. The plotting function itself triggers validation though which may possibly slow down this method when passing in a dict.

Are you talking about the plot/iplot plotting functions? These should have a validate=False option that will bypass validation.
-Jon

Ah, that is great. Thanks Jon!

Do you know whether dcc.Graph or other components would perform any type of validation? (did not look like it when perusing the Python code)

I’m not certain, but I don’t think dcc.Graph performs and validation if you pass in a dict.