plotly / plotly/dash

DataTable returns selected_rows out of list index

Open
#2,130 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug dash-data-table P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

Describe your context
Please provide us your environment, so we can easily reproduce the issue.

  • replace the result of pip list | grep dash below
dash                 2.5.1
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0
  • if frontend related, tell us your Browser, Version and OS

    • OS: CentOS
    • Browser chrome
    • Version Version 103.0.5060.114 (Official Build) (64-bit)

Describe the bug
I have a DataTable in which I can select rows. At the start no row is selected.
image
If I click selected_rows returns None as it should.

Now I click on Bernd and remove the name and get this:
image
If I click now without selecting anything the selected_rows value is 1, even though there is only one element (with index 0). If I repeat remove Anna, I have list with no entries and clicking again return selected_rows= 0.

Expected behavior

If no option is (visibly) selected it should return None.

This is a minimal working example:

import dash
from dash import Dash, html, dcc, dash_table, Input, Output, State, ctx
import pandas as pd

def nameTable():
    filtered_df = df.loc[df['Name'].isin(enabledName)]
    return html.Div([
                dash_table.DataTable(filtered_df.to_dict('records'), [{"name": i, "id": i} for i in df.columns], 
                                     id='nameTable',
                                     sort_action='native',
                                     sort_mode="multi",
                                     row_selectable="single",)
            ], style={'width': '5%'})

name_list = ["Anna", "Bernd"]

df = pd.DataFrame({'Name':name_list
                   }, index=name_list)

enabledName = ["Anna", "Bernd"]

app = Dash(__name__)
app.layout = html.Div(children=[
    nameTable(),
    html.Button("remove", id="removeButton")
])

@app.callback(
    Output('nameTable', 'data'),
    Input('removeButton', 'n_clicks'),
    State('nameTable', 'selected_rows'),
    State('nameTable', 'data'),
)
def updateEnable(n_clicks, selected_rows, data):
    if(selected_rows is not None):
        if(selected_rows[0] < len(data)):
            enabledName.remove(data[selected_rows[0]]['Name'])
        else:
            print(f"Tried to enable Name that's not in list: index= {selected_rows[0] }")
    else:
        print("is none")

    enabled_df = df.loc[df['Name'].isin(enabledName)]
    return enabled_df.to_dict('records')

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DataTable and the provided minimal Python example, focusing on how selected_rows behaves after the updateEnable callback changes the data. Reproduce the row-removal sequence and verify the selection value after each update. Done means an unselected table returns None rather than an out-of-range or stale row index.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.