DataTable: accessing value of active_cell

How to get value of active_cell on callback?
DataTable is editable and I’m attempting to capture updated cell value.
See working code sample below.
Thank you!

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

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": name, "id": name} for name in df.columns],
        data=df.to_dict("rows"),
        editable=True,
    ),
    html.H3(
    id='text-output',
    children='',
    style={'textAlign': 'left'}
    )
])

@app.callback(  Output('text-output', 'children'),
                [Input('table', 'data_timestamp')],
                [State('table', 'active_cell'),
                 State('table', 'data')])
def get_active_cell_value (timestamp, active_cell, data):
    active_cell_row_index = 'NA '
    active_cell_column_index = 'NA '
    active_cell_value = 'NA '
    if active_cell:
        active_cell_row_index = 'RowIndex: ' + str(active_cell[0])
        active_cell_column_index = '    ColumnIndex: ' + str(active_cell[1])
        # not working for column index
        # active_cell_value = '  CellValue: ' + str(data[active_cell[0]][active_cell[1]]) 
        # working for column name
        active_cell_value = '    UpdatedCellValue: ' + str(data[active_cell[0]]['State']) 
        # how to dynamically get active cell value?
    return active_cell_row_index, active_cell_column_index, active_cell_value

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

Found one solution would be to use the active_cell, the output is of the form:
location = {'row': i, 'column', j, 'column_id': 'something'}

directly:

data[location['row']][location['column_id']]

But I fear this might have side-effects if you have some empty rows. So I changed it as Dataframe and use:

pd.DataFrame(data).iat[location['row'], location['column']]

1 Like

@Dumblidore,

On what change I will get the row_id on active_cell?

2 Likes