Hi I am having issues with a callback I am making in DCC dropdowns.
I have an app that reads in data from a csv file into a pandas dataframe. I then want to analyse that data with Dash/Plotly.
I went through the tutorials again yesterday and the one about the cascading callback. I have 3 dropdowns and this should be used to slice and dice the data. If I select ALL from the Level 1 dropdown then the callback should return me my complete data frame.
If I select an option that is not all ( Operational Risk, Financial Risk, Business & Strategic Risk) then it should propagate those values into my Level 2 Dropdown, and likewise onto level 3. At that point the callback should return me a subset of the data.
As an example here is Financial risk broken down into its there levels
I have 3 dropdowns with a cascade between them. it kind of works if I select the All option, but as soon as I select a category in Level 1 I get a callback error
Traceback (most recent call last):
File "C:\Data\PythonProjects\clensed\clensed.py", line 680, in set_tl2_options
for i in sorted(raca_options['risk'].astype(str).unique())]
File "C:\Data\PythonProjects\clensed\venv\Lib\site-packages\pandas\core\series.py", line 824, in __getitem__
return self._get_value(key)
File "C:\Data\PythonProjects\clensed\venv\Lib\site-packages\pandas\core\series.py", line 932, in _get_value
loc = self.index.get_loc(label)
File "C:\Data\PythonProjects\clensed\venv\Lib\site-packages\pandas\core\indexes\range.py", line 354, in get_loc
raise KeyError(key)
KeyError: 'risk'
As soon as I change the level 1 option to another item I get a Key Error updating risk.options.
Here is my code for the callback
@app.callback(
Output('risk', 'options'),
Input('risk_types', 'value'))
def set_tl2_options(tl1_options):
if tl1_options != 'All':
raca_options = raca_df['risk_types'] == tl1_options
#print(f'DEBUG1: TL 1 Not equal to all: {raca_options}')
else:
raca_options = raca_df
#print(f'DEBUG2: TL1 equal to all: {raca_options}')
return [{'label': i, 'value': i}
for i in sorted(raca_options['risk'].astype(str).unique())]
@app.callback(
Output('level3', 'options'),
Input('risk', 'value'))
def set_tl3_options(tl2_options):
if tl2_options != 'All':
raca_options = raca_df[raca_df['risk'] == tl2_options]
#print(f'DEBUG3: TL2 Not equal to all: {raca_options}')
else:
raca_options = raca_df
#print(f'DEBUG4: TL2 equal to all: {raca_options}')
return [{'label': i, 'value': i}
for i in sorted(raca_options['level3'].astype(str).unique())]
And here is my Dropdown code.
# ------------------------------------------------------------------------------
# Define dropdowns
# ------------------------------------------------------------------------------
# Risk Category 1
risk_types_dropdown = dcc.Dropdown(
id='risk_types',
multi=False,
value = 'All',
clearable=False,
searchable=True,
persistence=True,
persistence_type='session',
style={"width": "100%"},
options=[{'label': k, 'value': k}
for k in sorted(raca_df['risk_types'].astype(str).unique())]
+ [{'label': 'All', 'value': 'All'}],
)
# Risk Category 2
risk_dropdown = dcc.Dropdown(
id='risk',
multi=False,
value ='All',
clearable=False,
searchable=True,
placeholder='Select...',
persistence=True,
persistence_type='session',
style={"width": "100%"},
options=[{'label': k, 'value': k}
for k in sorted(raca_df['risk'].astype(str).unique())]
)
# Risk Category 2
level3_dropdown = dcc.Dropdown(
id='level3',
multi=False,
value ='All',
clearable=False,
searchable=True,
placeholder='Select...',
persistence=True,
persistence_type='session',
style={"width": "100%"},
options=[],
My full code and the complete risk breakdown matrix along with test data can be had from my git repo https://github.com/twelsh37/clensed
And finally, how would I get that chart/data returned data back? Would it come as the value from the callback that I would then feed into a function to make the chart / data table?
All help gratefully received.
3 posts - 2 participants