I’m trying to create a cascading set of selections, where each subsequent MultiSelect is dependent on the results of the previous select.
My current attempt is to create a GLOBAL DataFrame, which - after making the first selection - is updated based on the selection.
The second MultiSelect options (data=) is then calculated form the GLOBAL DataFrame. However, the options for the second selection are not changing - perhaps because there’s nothing to trigger it to refresh?
IS it possible to populate the data
field with the contents of dcc.Store(id='resultset_ids', storage_type='session')
app = Dash(__name__, use_pages=True)
app.title="Main Sidebar"
global selections_df
global resultset_names
def get_selections_df():
# Returns a large DataFrame with columns "resultset_name" and "injection_id"
return query_selections_df(connection())
def sidebar():
global selections_df
global resultset_names
selections_df = get_selections_df()
return html.Div([
html.Div([
dcc.Location(id="url"),
html.Div([
dcc.Store(id='resultset_names', storage_type='session'),
dcc.Store(id='injection_ids', storage_type='session'),
html.H2("Sidebar", className="display-4"), # Classname is the CSS class
html.Div([
dmc.MultiSelect(
label="Select one or more resultset names",
placeholder="",
id="set-resultset-name",
value=[],
data = sorted([name for name in selections_df.resultset_name.unique() if isinstance(name, str)]), # To prevent None getting in there
persistence = True,
style={"marginBottom": 10}),
dmc.MultiSelect(
label="Select one or more resultset IDs",
placeholder="",
id="set-resultset-ids",
value="All",
data = sorted([ID for ID in selections_df.injection_id.unique() if isinstance(ID, str)]),
persistence = True,
style={"marginBottom": 10}),
]
),
],
style=SIDEBAR_STYLE, # Css formatting saved in variable above
),
]),
])
app.layout = html.Div([
sidebar(),
])
# Resultset_name will be the primary filter=====================================
#===============================================================================
@callback(Output('resultset_names', 'data'),
Input('set-resultset-name', 'value'),
State('resultset_names', 'data'))
def set_resultset_name(value, data):
global selections_df
if data is None:
data = {"resultset_names":value}
else:
data["resultset_names"] = value
selections_df = selections_df[selections_df.resultset_name.isin(value)]
return data
@callback(Output('injection_ids', 'data'),
Input('set-resultset-ids', 'value'),
State('injection_ids', 'data'))
def set_injection_ids(value, data):
global selections_df
if value == "All":
value = sorted([ID for ID in selections_df.injection_id.unique() if isinstance(ID, str)])
if data is None:
data = {"injection_ids":value}
else:
data["injection_ids"] = value
selections_df = selections_df[selections_df.injection_id.isin(value)]
return data
app.scripts.config.serve_locally = True
if __name__ == '__main__':
app.run_server(debug=True)