I have been having strange issues when developing new components and dash apps and I'd like to share them.
Sometimes callbacks are not called during initialization, sometimes the default prop never get applied. There are some inconsistencies with the callback system.
If a prop doesn't have a defaultProps entry, it never get an initial callback.
Also true if the default props is nil. Discovered while working on the Storage component, I initially thought that setProps didn't work in componentWillMount, when I added a timestamp with a default of -1, I was getting the callback.
Example to get the data props from a storage component using the timestamp on initial load:
importdashfromdash.dependenciesimportOutput, Input, Stateimportdash_html_componentsashtmlimportdash_core_componentsasdccfromdash.exceptionsimportPreventUpdateapp=dash.Dash(__name__)
app.scripts.config.serve_locally=Trueapp.layout=html.Div([
dcc.Storage(id='storage'),
html.Button('set storage', id='set-storage'),
html.Div(id='output')
])
@app.callback(Output('storage', 'data'), [Input('set-storage', 'n_clicks')])defon_click(n_clicks):
ifn_clicksisNone:
raisePreventUpdatereturn'initial'@app.callback(Output('output', 'children'), [Input('storage', 'modified_timestamp')], [State('storage', 'data')])defon_init(ts, data):
returndataif__name__=='__main__':
app.run_server(port=30600, debug=True)If you click the button, then reload the page, it should show initial below the button.
If you remove the modified_timestamp input:
@app.callback(Output('output', 'children'), [Input('storage', 'data')])defon_init(data):
returndataIt won't be called on initial load.
Add a second Input to an input list: the default props never get applied to the Inputs it stays at None.
importdashfromdash.dependenciesimportOutput, Inputfromdash.exceptionsimportPreventUpdateimportdash_html_componentsashtmlapp=dash.Dash(__name__)
app.scripts.config.serve_locally=Trueapp.layout=html.Div([
html.Button('btn1', id='btn1'),
html.Button('btn2', id='btn2'),
html.Div(id='output')
])
@app.callback(Output('output', 'children'), [Input('btn1', 'n_clicks'), Input('btn2', 'n_clicks')])defon_click(n1, n2):
ifn1isNone:
raisePreventUpdateprint(n1)
print(n2)
return'Hello'if__name__=='__main__':
app.run_server(port=30500, debug=True)If you only click btn2, you will always get a PreventUpdate, it also ends up missing the second initial callback.
There is two initial callbacks that can pop up, only the second is useful since the first one only have None as arguments.
I think we should remove the first callback with all None arguments. It has no use and only create bloat IMO because I always have a check if None then raise PreventUpdate. The only time it is useful is if you want to act on the initial layout, but if we fix the initial callback for non default props it's a non-issue.
Also could be useful to have the initial values if we implement state saving for the dash-renderer redux store.
cc @plotly/dash
I have been having strange issues when developing new components and dash apps and I'd like to share them.
Sometimes callbacks are not called during initialization, sometimes the default prop never get applied. There are some inconsistencies with the callback system.
If a prop doesn't have a defaultProps entry, it never get an initial callback.
Also true if the default props is nil. Discovered while working on the Storage component, I initially thought that setProps didn't work in
componentWillMount, when I added a timestamp with a default of-1, I was getting the callback.Example to get the data props from a storage component using the timestamp on initial load:
If you click the button, then reload the page, it should show
initialbelow the button.If you remove the modified_timestamp input:
It won't be called on initial load.
Add a second Input to an input list: the default props never get applied to the Inputs it stays at None.
If you only click btn2, you will always get a PreventUpdate, it also ends up missing the second initial callback.
There is two initial callbacks that can pop up, only the second is useful since the first one only have None as arguments.
I think we should remove the first callback with all
Nonearguments. It has no use and only create bloat IMO because I always have a check if None then raise PreventUpdate. The only time it is useful is if you want to act on the initial layout, but if we fix the initial callback for non default props it's a non-issue.Also could be useful to have the initial values if we implement state saving for the dash-renderer redux store.
cc @plotly/dash