đź“Ł Preserving UI State, like Zoom, in dcc.Graph with uirevision with Dash

The usage @chriddyp shows - where you simply specify layout.uirevision that changes only when you want to reset any user-driven changes to the plot - covers most cases. In fact, often you can just set it to a truthy constant (True, or 'the user is always right') and forget about it. That should work as long as you’re not replacing the whole data set on the plot. If you do replace the whole data set it’s important to change uirevision though, or the plot could be zoomed into a blank plot after the change, which could be confusing.

There are also cases where you want more control. Say the x and y data for a plot can be changed separately. Perhaps x is always time, but y can change between price and volume. You might want to preserve x zoom while resetting y zoom. There are lots of uirevision attributes, that normally all inherit from layout.uirevision but you can set them separately if you want. In this case set a constant xaxis.uirevision = 'time' but let yaxis.revision change between 'price' and 'volume'. Be sure to still set layout.uirevision to preserve other items like trace visibility and modebar buttons!

Trace visibility is a little different: traces are tracked by their uid (or by index in data if no uid is given) - So let’s say your plot can have traces for France, Germany, and UK. Initially only Germany and UK are plotted and the user hides Germany (the first trace) using the legend. Then they add France, which becomes the new first trace. Using uirevision without uid, France is immediately hidden! But if you give each trace a uid ('France', 'Germany', 'UK' would do), the visible: 'legendonly' flag will follow Germany as it moves to the second trace.

The visibility state of all traces is preserved based on layout.legend.uirevision. So if you want to re-show all traces the user may have hidden by clicking the legend, change legend.uirevision. (We do have a trace.uirevision, but it only controls a few things like parcoords constraintrange)

4 Likes