Hi @AnnMarieW -
Thanks for the background and pointing out the distinction between rows
.
My initial DataFrame is generated and rendered from the callback, unlike the example here:https://dash.plotly.com/datatable/editable. I do the calculations in the callback function as well.
When the user updates an editable column, I’d like to update the values in another column in same data-table.
Here’s a minimal reproducible example:
import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_html_components as html
app = dash.Dash(__name__)
app.layout = html.Div([
dbc.Button("Run", id="dt-button", size="lg"),
dash_table.DataTable(
id='table1',
columns=[
{'name': 'Year', 'id': 'year'},
{'name': 'Income', 'id': 'income'},
{'name': 'Expenses', 'id': 'expense'},
{'name': 'Cash flow', 'id': 'cash flow'}
],
editable=True,
),
])
# Callback
@app.callback(Output("table1", "data"),
[
Input("dt-button", "n_clicks"),
Input("table1", "data_timestamp")
],
[
State("table1", "data"),
]
)
def table(n_clicks, timestamp, rows):
if n_clicks:
# Construct Pandas DataFrame
df = pd.DataFrame({
"Year": ['2018','2019','2020'],
"Income": ['200','300','400'],
"Expense": np.nan, # Editable column, user inputs expenses
"Cash Flow": np.nan #Income - expense
})
# Update columns
for row in rows:
try:
row['cash flow'] = (row['income'] - row['expense']) # Expense is entered by the user
except:
row['cash flow'] = row['income']
df_table = df.to_dict("records")
return (df_table)
else:
return (no_update)