I have an app, that have dependencies across a few filters (dropdown, datepicker), and the value from it was used as input again.
It works perfectly, if I were just to a single input in the callback, however, issues arises when there are multiple inputs in the callback.
Below is a small example, illustrating this.
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd
from datetime import datetime as dt
import datetime
app = dash.Dash()
app.config['suppress_callback_exceptions'] = True
d= {'X': list('ABCABC'), 'Y':list('dddeee'), 'Z': [dt.today() + datetime.timedelta(days = i) for i in range(6)]}
data = pd.DataFrame(d)
filter_options = {i:[j for j in data[data.X == i].Y.unique()] for i in data.X.unique()}
app.layout = html.Div([
html.Div([
dcc.RadioItems(
id ='filter1',
options = [{'label': i, 'value': i} for i in data.X.unique()]
),
dcc.Dropdown(id ='filter2'),
html.Div(id = 'filter-ph'),
html.Div(id = "output-div")
])
])
@app.callback(Output('filter2','options'), [Input('filter1','value')])
def get_f2_options(selected_f1_value):
return [{'label':i, 'value': i} for i in filter_options[selected_f1_value]]
@app.callback(Output('filter-ph','children'), [Input('filter2','value')])
def get_date_value(selected_f2_value):
if selected_f2_value:
df = data[data.Y == selected_f2_value]
return dcc.DatePickerSingle(
id='filter3',
date=df.Z.max(),
max_date_allowed=df.Z.max(),
min_date_allowed=df.Z.min()
)
@app.callback(Output('output-div','children'), [Input('filter3','date')])
def update_output_div(v1):
return "Selected date: {}".format(v1)
if __name__ == '__main__':
app.run_server()
The code above works fine, as there is only one input in the update_output_div
.
However, it shows “error loading dependencies” error if I were to use additional input of value from filter2 in update_output_div
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd
from datetime import datetime as dt
import datetime
app = dash.Dash()
app.config['suppress_callback_exceptions'] = True
d= {'X': list('ABCABC'), 'Y':list('dddeee'), 'Z': [dt.today() + datetime.timedelta(days = i) for i in range(6)]}
data = pd.DataFrame(d)
filter_options = {i:[j for j in data[data.X == i].Y.unique()] for i in data.X.unique()}
app.layout = html.Div([
html.Div([
dcc.RadioItems(
id ='filter1',
options = [{'label': i, 'value': i} for i in data.X.unique()]
),
dcc.Dropdown(id ='filter2'),
html.Div(id = 'filter-ph'),
html.Div(id = "output-div")
])
])
@app.callback(Output('filter2','options'), [Input('filter1','value')])
def get_f2_options(selected_f1_value):
return [{'label':i, 'value': i} for i in filter_options[selected_f1_value]]
@app.callback(Output('filter-ph','children'), [Input('filter2','value')])
def get_date_value(selected_f2_value):
if selected_f2_value:
df = data[data.Y == selected_f2_value]
return dcc.DatePickerSingle(
id='filter3',
date=df.Z.max(),
max_date_allowed=df.Z.max(),
min_date_allowed=df.Z.min()
)
@app.callback(Output('output-div','children'), [Input('filter3','date'), Input('filter2', 'value')])
def update_output_div(v1,v2):
return "Selected date: {}".format(v1)
if __name__ == '__main__':
app.run_server()