Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com

Filtering an existing data-frame using a callback

I have an existing dataframe (large) that I’d like to filter via a callback, and then insert the result into a data-table.

I am confused because I think I’ll need the original big dataframe as an input in the callback, however I’m not sure how I would do that.

Below is the structure of my code.

Many thanks!

@app.callback(
    Output('filtered_table', 'data'),
    [Input('dropdown1', 'value'),
     Input('dropdown2', 'value'),
     "Original Dataframe here???" 
     ])

def filter(dropdown1,dropdown2):
    filtered dataframe based on dropdown 1& 2 value

Typically I would throw that original data frame into a State…so it would look like this:

@app.callback(
    Output('filtered_table', 'data'),
    [Input('dropdown1', 'value'),
     Input('dropdown2', 'value')],
     [State('filtered_table','data')] 
     )

def filter(dropdown1,dropdown2,data):
    df = pd.DataFrame(data)
    filtered dataframe based on dropdown 1& 2 value

You’ll have to do a bit of research into what exactly you need to pull in, but I think it will be the ‘data’ property. But data tables are a bit weird and have several attributes you might need to play around with. I think the way I’ve written it will work.

Is the data frame that you filter static? If so, you could just use a global variable.