Dynamically filtering a dataframe using callbacks

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=[
                    options= email_options,

                    options= email_options
                    clearable = False,
                    options = job_options,
                    clearable = False,
                    options = job_options
                    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)

    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.

1 Like

It would be much easier to help if you posted a minimal working example or even the exact error you get.

In either case, your callback is missing an Output. I suspect you are using your dataframe data as a global variable and modyfying that, which is not a good idea (see section “Why global variables will break your app” here).

If you want to store the filtered dataframe, use a dcc.Store as Output to the callback instead.

I just made a much simpler version of this filter that works with the dash data table, but it still has problems. Could you please help me with that question?


I can not as of now help you on this, as you didn’t post the simpler version of this filter nor the problems that you are encountering (or if you did, I have overlooked it). Please post a short, self contained example that illustrates the problem.

1 Like