dash-table icon indicating copy to clipboard operation
dash-table copied to clipboard

setting selected_row_ids doesn't seem to initialied the selected row IDs

Open chriddyp opened this issue 4 years ago • 4 comments

it should update the derived_viewport_selected_row_ids property and initialize the checkboxes in the table

import dash
from dash.dependencies import Input, Output
import dash_table
import dash_html_components as html
import dash_core_components as dcc
from app import app
import plotly.express as px
import pandas as pd

df = px.data.iris()  # sample dataset
df['id'] = df.index

app = dash.Dash(__name__)

app.layout = html.Div([

    dash_table.DataTable(
            data=df.to_dict('rows'),
            columns=[
                {'name': i, 'id': i}
                for i in df.columns
            ],
            style_table={'height': 500, 'overflowY': 'scroll'},
            filter_action='native',
            sort_action='native',
            id='data',
            editable=True,
            row_selectable='multi',
            selected_row_ids=[0, 1, 5]
        ),
    ], width=100),

    dcc.Graph('another-graph')
])


@app.callback(
    Output('another-graph', 'figure'),
    [Input('column-dropdown', 'value'),
     Input('data', 'derived_virtual_data'),
     Input('data', 'derived_viewport_selected_row_ids')]
)
def update_graph(column, rows, row_ids):
    print(row_ids)
    if rows is None:
        dff = df
    elif len(rows) == 0:
        return px.scatter()
    elif rows is not None:
        dff = pd.DataFrame(rows)
    dff['color'] = [
        'Selected' if i in row_ids else '' for i in range(len(dff))
    ]

    return px.bar(
        dff,
        x=dff.columns[0],
        y=column,
        hover_data=dff.columns,
        color='color',
        category_orders={'color': ['', 'Selected']}
    ).update_layout(showlegend=False)

chriddyp avatar Mar 02 '20 20:03 chriddyp

Hi. have you managed to find a work around for this, yet?

simbapy avatar Mar 25 '20 16:03 simbapy

I have exactly the same issue

My scenario is to pre select rows of a table based on some conditions (as filter in Excel) by setting seletected_row_ids property of the table.

But nothing is reflected in the row check box. Worst, if I select a row , my initial selection is gone, and only selected row is taken into account.

Have you a workaround to handle this scenario ?

jeanlouis-tournay avatar May 07 '20 12:05 jeanlouis-tournay

Here is an example for a workaround to this issue where selected_row_ids updates selected_rows which then preselects the correct checkboxes.

import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv')
df['id'] = df.index

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Dropdown(id='filter', options=[dict(label=i, value=i) for i in df.country.unique()]),
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i, "editable":True} for i in df.columns],
        editable=True,
        data=df.to_dict('records'),
        row_selectable='multi',
        selected_row_ids=[2, 3, 4, 10, 13000],
        ),
])


@app.callback([Output('table', 'data'), Output('table', 'selected_rows')],
              [Input('filter', 'value')], [State('table', 'selected_row_ids')])
def update_data(country, selected_row_ids):
    if not country is None:
        dff = df.query('country == @country')
        
        # convert the selected_row_ids to what the selected_rows should look like in dff
        if selected_row_ids is not None:
            selected_rows = dff.reset_index().query('id == @selected_row_ids').index
        return [dff.to_dict('records'), selected_rows]

    if selected_row_ids is not None:
        selected_rows = df.reset_index().query('id == @selected_row_ids').index
    return [df.to_dict('records'), selected_rows]


if __name__ == '__main__':
    app.run_server(debug=True, port=8003)

michaelbabyn avatar May 11 '20 18:05 michaelbabyn

I have also run across this same issue, any chance on a fix?

DonardGrey avatar Jan 04 '23 16:01 DonardGrey