plotly / dash

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

Pasting into a DataTable overwrites `data_previous` with new data #2326

Open frnhr opened 1 year ago

frnhr commented 1 year ago

Environment

Describe the bug

When doing a paste action (as in copy&paste), two things happen:

Expected behavior

After a paste, I expect data_previous property to contain last the data before the paste.

Why is this Important

Normally, comparing data with data_previous makes it is easy to see what has changed. This is important when values in different columns are inter-dependent. This bug makes it impossible (without non-pretty workarounds) to tell which values were pasted into the table.

Screen Recording

https://user-images.githubusercontent.com/1173748/202705073-96d56e50-b59f-4bab-8a77-df9e9bd1f352.mp4

Example App

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

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app = dash.Dash(__name__)

app.layout = html.Div([
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i, "editable": True} for i in df.columns],
        data=df.to_dict('records'),
        editable=True,
    ),
    # counter for callback calls, to make sure the callback is not running multiple times:
    html.H4("Callback count:"),
    dcc.Input(id="count", value=0),
    # current and old data for inspection:
    html.H4("Current data:"),
    html.Pre(id="data", children=""),
    html.H4("Previous data:"),
    html.Pre(id="prev_data", children=""),
])

@app.callback(
    Output("data", "children"),
    Output("prev_data", "children"),
    Output("count", "value"),
    Input("table", "data"),
    State("table", "data_previous"),
    State("count", "value"),
)
def show_data_and_prev_data(data, prev_data, count):
    return str(pd.DataFrame(data)), str(pd.DataFrame(prev_data)), int(count) + 1

if __name__ == '__main__':
    app.run_server(debug=True)
frnhr commented 1 year ago

Found this bug reported in the old repo: https://github.com/plotly/dash-table/issues/936