@chriddyp
Below is the code. Tried to strip it and reproduced the error.
To reproduce:
- Run code as it is. Select a radio button. Text area will be filled with value. Click on “Create” button. Selected row will be removed from table above.
- Un-comment the third callback re-run.
- Perform above steps and nothing will work. e.g. selecting radio button will not populate text area
df = pd.DataFrame(np.random.randint(0,100,size=(5, 4)), columns=list(‘ABCD’))
app = dash.Dash(name, external_stylesheets=[dbc.themes.BOOTSTRAP])
create_button = dbc.Container([ dbc.Row( [ dbc.Button(‘Create’, id=‘submit-button’, n_clicks=0, color=“primary”),
dbc.Button(‘Refresh’, id=‘refresh-button’, n_clicks=0, color=“success”)],
justify=“center”, align=“center”, className=“h-50”)
],style={“height”: “10vh”}
)
app.layout = html.Div([ html.Div(id=‘alert-div’),
html.Div([
dash_table.DataTable(
id=‘table-dropdown’,
data = df.to_dict(‘records’),
columns=[
{‘id’: ‘A’, ‘name’: ‘A’, ‘editable’: False},
{‘id’: ‘B’, ‘name’: ‘B’},
{‘id’: ‘C’, ‘name’: ‘C’},
{‘id’: ‘D Group’, ‘name’: ‘D’},
],
css = [{‘selector’: ‘tr:hover’, ‘rule’: ‘backgroundColor: pink;’}],
editable=True,
row_selectable=‘single’,
selected_rows=
),
html.Div(id=‘table-dropdown-container’)
], style={‘display’: ‘inline-block’}),
dcc.Loading(id=“loading-1”, children=[html.Div(id=“loading-output-1”)], type=“default”),
html.Div([
dcc.Textarea(
id=‘textarea-example’,
value=‘Please select a row above to display the contents in this area’,
style={‘width’: ‘100%’, ‘height’: 300, ‘fonSize’: 21},
disabled=True,
),
create_button,
html.Div(id=‘textarea-example-output’, style={‘display’: ‘inline-block’, ‘whiteSpace’: ‘pre-line’})
])
], style={‘marginLeft’: 30, ‘marginRight’: 30, ‘display’: ‘inline-block’})
@app.callback(
[Output(‘table-dropdown’,‘data’),
Output(‘table-dropdown’,‘selected_rows’),
Output(‘alert-div’,‘children’),
Output(‘refresh-button’,‘disabled’),
Output(‘submit-button’,‘disabled’)],
[Input(‘submit-button’,‘n_clicks’)],
[State(‘table-dropdown’, ‘selected_rows’),
State(‘table-dropdown’, ‘data’)],
prevent_initial_call=True)
def update_datatable(n_clicks, selected_rows, data):
global df
print("\nupdate_datatable")
alert = None
refresh_hide = True
create_hide = False
if n_clicks:
if selected_rows:
df = df.drop(selected_rows[0]).reset_index(drop=True)
selected_rows =
return (df.to_dict(‘records’), selected_rows, alert, refresh_hide, create_hide)
@app.callback(
Output(‘textarea-example’,‘value’),
[Input(‘table-dropdown’, ‘selected_rows’)],
[State(‘table-dropdown’, ‘data’)],
prevent_initial_call=True)
def update_textarea(selected_rows ,data):
print("\nupdate_email")
#print(selected_rows)
if selected_rows:
return data[selected_rows[0]][‘A’]
else:
return “Please select a row above to display the contents in this area”
‘’’@app.callback([Output(‘table-dropdown’,‘data’),
Output(‘table-dropdown’,‘selected_rows’),
Output(‘refresh-button’,‘disabled’),
Output(‘submit-button’,‘disabled’)],
[Input(‘refresh-button’,‘n_clicks’)],
prevent_initial_call=True)
def refresh_emails(n_clicks):
print("\nrefresh_emails")
refresh_hide = False
create_hide = False
if n_clicks:
refresh_hide = True
create_hide = False
return (email_df.to_dict(‘records’), , refresh_hide, create_hide)’’’
if name == ‘main’:
app.run_server(debug=False)