Ill keep this short. I am trying to filter a dataframe using dash dropdown boxes and callbacks. I plan on using the filtered dataframe as the basis for a dash graph. I just cannot for the life of me figure out how to get the dataframe to filter properly and I could really use some help. This is the code for my filter
app.layout = html.Div([
html.Div(className='Filters', children=[
dcc.Tabs(id='filters', children=[
dcc.Tab(label='Control Panel', children=[
dcc.Dropdown(
id='ToEmaildropdown',
options= email_options,
clearable=False
),
dcc.Dropdown(
id='FromEmaildropdown',
clearable=False,
options= email_options
),
dcc.Dropdown(
id='ToJobdropdown',
clearable = False,
options = job_options,
),
dcc.Dropdown(
id='FromJobdropdown',
clearable = False,
options = job_options
),
dcc.DatePickerRange(
id='date-picker-range',
min_date_allowed=date(1998, 11, 12),
max_date_allowed=date(2002, 6, 20),
initial_visible_month=date(1998, 11, 12),
end_date=date(2002, 6, 20)
),
html.Button('Update', id='btn-nclicks-1', n_clicks=0)
])
]),
])
])
@app.callback(
Input('ToEmaildropdown', 'value'),
Input('FromEmaildropdown', 'value'),
Input('ToJobdropdown', 'value'),
Input('FromJobdropdown', 'value'),
Input('date-picker-range', 'start_date'),
Input('date-picker-range', 'end_date'),
Input('btn-nclicks-1', 'n_clicks'))
def update_df(btn1 , ToEmail, FromEmail, ToJob, FromJob, DatePicker, ):
if btn1 :
FromEmailF = (data['fromEmail'] == FromEmail)
ToEmailF = (data['toEmail'] == ToEmail)
FromJobF = (data['fromJobtitle'] == FromJob)
ToJobF = (data['toJobtitle'] == ToJob)
DateF = ((data['date'] <= 'end_date')&(data['date'] >= 'start_date'))
Filter = (FromEmailF & ToEmailF & FromJobF & ToJobF & DateF)
data_filtered = data[Filter]
return data_filtered
As you see, I start by creaing the drop down boxes with each of the parameters I need, then I set up a callback, using all of the drop box’s (and date picker’s) values in order to filter the data. I have it set up so that it only returns the filtered data set once I press the update button, but I don’t know if that will work because I don’t fully understand how the callback would work here. And even after all that, I still don’t know how I could actually use the returned dataset. In my other file where I create the graph, I use the following:
data = update_df(ToEmail, FromEmail, ToJob, FromJob, DatePicker, btn1)
But then I get an error saying I haven’t defined any of the variables in the function to get my dataset, even though I imported everything.
I am so lost and confused. I am not anywhere near sure that my filter works in the first place, and even if it did work, I can’t find a way to actually use the returned dataframe. Please help me.