@mbkupfer hereâs what I meant with the countries:
from random import random
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id='countries',
multi=True,
options=[
{'label': 'France', 'value': 'France'},
{'label': 'Germany', 'value': 'Germany'},
{'label': 'UK', 'value': 'UK'}
],
value=['France', 'Germany', 'UK']
),
dcc.Graph(id='graph')
])
@app.callback(Output('graph', 'figure'), [Input('countries', 'value')])
def update_graph(countries):
traces = [dict(
type='bar',
x=range(10),
y=[random() for _ in range(10)],
uid=country,
name=country
) for country in countries]
return dict(data=traces, layout={'legend': {'uirevision': True}})
if __name__ == '__main__':
app.run_server(debug=True)
By using the country as uid
, you can hide the 2nd or 3rd trace from the legend, then delete the first trace from the dropdown, and the same country will still be hidden in the legend. Without uid
it would be the same trace index that was hidden. Note though that if you delete a hidden trace via dropdown then later bring it back, weâll lose memory that you had previously hidden it.
As to changing tabs resetting the UI - Iâm afraid thatâs expected. plotly.js stores this UI state on the DOM element and when you switch to a different tab that DOM element is destroyed, only to be recreated when you come back. I suppose you could pre-render all the tabs and just hide the inactive ones, rather than removing them from the DOM; that might be bad for initial load time, though if done right (just using a multi-output on style
props?) it could be good for switching performance. Other than that all I can think of is manually tracking these changes via restyleData
etc⌠but the pain and complication associated with that is why we added uirevision
in the first place!