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)