In Callback Gotchas I’ve read that:
For full validation, all components within your callback must exist in the layout when your app starts, and you will see an error if they do not.
Below is a piece of code where, as far as I see, not all components exist in the layout when the application starts. This is because 3 components (dcc.Input, html.Button and html.Div) are only set when the callback entry returns. However, this code seems to work just fine.
So how much should I be concerned about this
Callbacks require their
Inputs,States, andOutputto be present in the layout
section in Callback Gotchas?
from dash import Dash, Input, Output, State, dcc, html
from dash.dash import PreventUpdate
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
id='main',
children=[
html.Button(
id='load',
children='Load'
),
]
)
@app.callback(
Output('main', 'children'),
Input('load', 'n_clicks')
)
def entry(n_clicks):
if n_clicks is None:
raise PreventUpdate
return html.Div([
dcc.Input(id='input', value='initial value'),
html.Button(id='button', children='Submit'),
html.Div(id='output')
]
)
@app.callback(
Output('output', 'children'),
Input('button', 'n_clicks'),
State('input', 'value')
)
def echo(n_clicks, value):
if n_clicks is None:
raise PreventUpdate
return value
if __name__ == '__main__':
app.run_server()