Hello,I have been trying to make an app on dash filter with a dynamic/unlimited number of filters from user’s end .
I am stuck I cannot proceed more the button is not working and table also did not get loaded when button works please help.
The codes are below.
app = dash.Dash(name, suppress_callback_exceptions=True)
app.layout = html.Div([
html.Button(“Add Filter”, id=“add-filter”, n_clicks=0),
html.Div(id=‘dropdown-container’, children=),
DataTable(id='t',columns=[
{"name": i, "id": i} for i in df.columns],
data=df.to_dict('rows'))
])
@app.callback(
Output(‘dropdown-container’, ‘children’),
Output(‘t’, ‘data’),
Input(‘add-filter’, ‘n_clicks’),
State(‘dropdown-container’, ‘children’),
State(‘t’,‘data’))
def display_dropdowns(n_clicks, children):
new_dropdown = html.Div(
children=[
dcc.Dropdown({‘type’: ‘filter-dropdown’,‘index’: n_clicks},
options=[{
‘label’ : c ,‘value’ : c
}
for c in sorted(df.columns)]),
dcc.Dropdown({'type': 'dynamic-choice','index': n_clicks},
options=[{'label': 'greater than', 'value': 'a'},
{'label': 'equal', 'value': 'b'},
{'label': 'less than', 'value': 'c'},
{'label': 'less than equal to', 'value': 'd'},
{'label': 'greater than equal to', 'value': 'e'},
{'label': 'not equal to', 'value': 'f'}],
value= 'a',
placeholder='Chose a logical operator'),
'Value: ',
dcc.Input({'type': 'dynamic-choice','index': n_clicks},type='number'),
]
)
children.append(new_dropdown)
return children
print(children)
@app.callback(Output(‘t’,‘data’),
[Input({‘type’: ‘filter-dropdown’,‘index’: MATCH}, ‘value’),
Input({‘type’: ‘dynamic-choice’,‘index’: MATCH}, ‘value’),
Input({‘type’: ‘dynamic-choice’,‘index’: MATCH}, ‘value’)])
def update_table(col,l,f):
print(col,l,f)
if l == ‘a’:
dff = df[df[col] > f]
return dff.to_dict(“rows”)
elif l == ‘b’:
dff = df[df[col] == f]
return dff.to_dict(“rows”)
elif l == ‘c’:
dff = df[df[col] < f]
return dff.to_dict(“rows”)
elif l == ‘d’:
dff = df[df[col] <= f]
return dff.to_dict(“rows”)
elif l == ‘e’:
dff = df[df[col] >= f]
return dff.to_dict(“rows”)
elif l == ‘f’:
dff = df[df[col] != f]
return dff.to_dict(“rows”)
else:
return df.to_dict(“rows”)
if name == ‘main’:
app.run_server()