Black Lives Matter. Please consider donating to Black Girls Code today.

How to get the data of the selected rows of dash-table-experiments?

For now, I use the selected_row_indices to get the corresponding rows of pandas dataframe, but after clicking the column head to sort data, using selected_row_indices can’t get the corresponding rows of df. Is it possible to get the data of the selected rows, not just the indices?

Thanks.

You’ll need to use the rows property as input along with selected_row_indices. There’s an example at https://github.com/plotly/dash-table-experiments. But essentially something like

@app.callback(
    Output('div-out','children'),
    [Input('datatable', 'rows'),
     Input('datatable', 'selected_row_indices')])
def f(rows,selected_row_indices):
    #either:
    selected_rows=[rows[i] for i in selected_row_indices]
    #or
    selected_rows=pd.DataFrame(rows).iloc[i] 
    return do_something
1 Like

Thanks @chubukov, it works.

1 Like

@chriddyp If the data table component is getting an upgrade at some point, it might be worth thinking about this design. If I have a big table, I’m sending the entire set of rows to the server just so that it can get the couple of rows that are selected. If the selected rows can be a component level property it might be a non-trivial performance improvement for big tables.

I know it’s not really compatible with the current design (e.g. you’d have conflicts between rows and selected_rows) but just some thoughts for the future.

@chriddyp having the precise same issue described by @chubukov as I’m using a reasonable sized table. Doesn’t feel right to be passing such a huge data frame back&forward, specially if the table is not editable.

Would be interesting to have the original indexes returned & always tracked. Think that’s how it works in R shiny, we get the original dataframe indexes selected. Adding, that it would be also useful to get pandas index support, if so the pandas original index would be returned, not the position from dictionary.
(think this would probably also avoid triggering callbacks if the actual selected_indexes don’t change with sorting or filtering)

What I am doing now:
-load a reasonable sized dataframe, with hundreds of columns
-sample small percentage, sampled dataframe keeps the original index
-create a data frame to show in dash, one column is the original index, subset few number of columns to show in dash (not all the original columns)
-then load in dash, index column is visible (hack),
-so any sorting,filtering, I get the dash selected_rows+selected_indexes, with that I get the index column values, then use that to select on my master data frame, to get all columns data and do what I need then

should be way easier :slight_smile: this is one of my typical patterns for this apps
or if anyone has better workaround!
Rui

adding, at least from my tests, if we need the rows in two callbacks, two identical request will be sent with the full rows payload, which seems really redundant and can add a lot on bandwith, (multiple outputs would help here also)

Can you please elaborate on how you achieve this? I need to set an index column and get those values instead of the selected rows indices, as my dataset is very large.

Why do I have to get back all the rows when I only need one single row?