✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🏦 Standard & Poor's chooses Dash Enterprise for ESG analysis. Learn why, sign up for the June 23 Webinar here!

How to intercept row deletion action from DataTable

Hello,

I have a Dash DataTable with row_deletable=True. When the user deletes a row, I want to issue an http post request, based on the data of the row that was deleted.

Eg, if the user deletes the row below with State=“California”, I want to make a post request with “California” in the payload.

I’ve created a working example where I compare data to data_previous. Is this the most efficient way? Is it necessary to create this empty_div, since I don’t need the callback to change any Output?

import dash
import dash_table
import pandas as pd
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

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

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Div(id='empty_div'),
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i} for i in df.columns],
        data=df.to_dict('records'),
        row_deletable=True
    )])

@app.callback(Output('empty_div', 'children'),
              [Input('table', 'data')],
              [State('table', 'data_previous')])
def engine_pool_list(data,data_previous): #,refresh_clicks
    data_previous = data_previous or [] # ensure we deal with empty list rather than None
    data = data or []
    data_dropped = [x for x in data_previous if x not in data]
    for row in data_dropped:
        print(f'dropped {row["State"]}')
    raise PreventUpdate

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

Thank you!