3

I'm new to Dash and trying to filter the the following dataframe with a dropdown and show the filter dataframe. I am not able to define the callback function properly. Any sample code is really appreciated,

df = pd.DataFrame({
    'col1': ['A', 'B', 'B', 'A', 'B', 'A'],
    'col2': [2, 1, 9, 8, 7, 4],
    'col3': [0, 1, 9, 4, 2, 3],   
})  

1 Answer 1

4

Here is the page from the docs that I think might be a good place to start. If you know the dataframe ahead of time, then you can populate the dropdown with the values that you know you'll need. If you don't, then you would need to use a callback to update the dropdown options prop.

The first example on that docs page shows how to get the value from a dropdown, and output it. In your case, you'd use the dropdown to filter the dataframe perhaps like:

@app.callback(
    Output('data-table-id', 'data'),
    [Input('dropdown-id', 'value')
)
def callback_func(dropdown_value):
    df_filtered = df[df['col1'].eq(dropdown_value)]

    return df_filtered.to_dict(orient='records)

And then you would set the output of the callback to the data prop of a Dash datatable. I've added some made-up values for the Output and Input part of the dropdown, hopefully that gives the general idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but in the layout, how should I use this output? A simple html.Div(id='data-table-id', ???), doesn't work ...
Right. You should use a data table component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.