Callback for Deleting a Row in a Data Table

Hi @shrub,

You can use the previous_data and data to deduce the removed data, at least that is the method I was able to use. Below is a simple program that demonstrates this.

That said, you might want to add some optimizations to improve performance, perhaps converting all the values of a unique column into a set and doing a set difference.

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

app = dash.Dash(__name__)

app.layout = html.Div([

    dash_table.DataTable(
        id='table',
        columns=[{
            'name': 'Column {}'.format(i),
            'id': 'column-{}'.format(i),
            'deletable': True,
            'editable_name': True
        } for i in range(1, 5)],
        data=[
            {'column-{}'.format(i): (j + (i-1)*5) for i in range(1, 5)}
            for j in range(5)
        ],
        editable=True,
        row_deletable=True
    ),

    html.Div(id='output')
])

@app.callback(Output('output', 'children'),
              [Input('table', 'data_previous')],
              [State('table', 'data')])
def show_removed_rows(previous, current):
    if previous is None:
        dash.exceptions.PreventUpdate()
    else:
        return [f'Just removed {row}' for row in previous if row not in current]


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