plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.19k stars 2.04k forks source link

DashTable active_cell - Different Behaviour for mouse and keyboard actions #1800

Open pschaefers opened 2 years ago

pschaefers commented 2 years ago

Python 3.9.7

dash                   2.0.0
dash-core-components   2.0.0
dash-html-components   2.0.0
dash-table             5.0.0

Describe the bug

After editing a cell in a dash_table.DataTable, the value for the tables attribute active_cell, dictionary-key row differs when pressing the Enter/Return key on a keyboard vs. when mouse-clicking anywhere outside the selected cell (even when clickin in another cell). This is true for either callback input Input("table", "data"), Input("table", "data_timestamp") or Input("table", "active_cell"). _It seems active_cell is evaluated again when triggering the callback via a keyboard key press, while it is not re-evaluated when mouse-clicking in another cell._

Expected behavior

active_cell behaves identical for mouse-clicks and keyboard enter/return key presses. When a cell is selected and the return key is pressed, active_cell is not evaluated again.

Screen Capture

dash-table-active-cell-bug

MWE

from dash import Dash, dash_table, html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

app = Dash(__name__)

app.layout = html.Div([
    dash_table.DataTable(
        columns=[
            {'name': 'Input Data', 'id': 'input-data', 'editable': True},
            {'name': 'Input Squared', 'id': 'output-data'}
        ],
        data=[{'input-data': i} for i in range(1, 3)],
        id="table",
    ),
])

@app.callback(
    Output("table", 'data'),
    Input("table", "data"),
    State("table", "active_cell"),
    prevent_initial_call=True,
    )
def update_row_value(data: dict, active_cell: dict) -> dict:
    print("active_cell = ", active_cell)
    data[active_cell["row"]]["output-data"] = int(data[active_cell["row"]]["input-data"]) ** 2

    return data

if __name__ == '__main__':
    app.run_server(debug=True)
pschaefers commented 2 years ago

Update: Just realised, the same issue was already opened in 2019, but remains unsolved yet :/ https://github.com/plotly/dash-table/issues/427