Hi,
I’m currently creating an app that requires a dynamic change in the Input parameter for a callback depending on another callback. Here is my example. The first callback will create/update the global variable special
which is used in the second callback. The length of special
is changing. However, one you select ‘A’, the second callback is no longer able to receive the special
parameter. So args becomes empty. Any idea on how to solve this. Btw, I’m new to Dash and my coding style may not be very ideal. Thanks for your help!
import dash
from dash.dependencies import Input, Output # for user interface
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash(__name__)
app.layout = html.Div(children=[
html.Div(dcc.Dropdown(id='select_input', options=[{'label': 'A', 'value': 'A'}, {'label': 'B', 'value': 'B'}])),
html.Div([dcc.Input(id='text_input', type='text')]),
html.Div(id='output1'),
html.Div(id='output2'),
])
@app.callback(
Output('output1', 'children'),
[Input('select_input', 'value')],
)
def dropdown_options(select_input):
if select_input is None:
return html.P('please select')
if select_input == 'A':
global special
special = [Input('select_input', 'value'), Input('text_input', 'value')]
return html.P(f'special is updated!')
else:
return html.P('special will not be updated.')
special = [Input('select_input', 'value')]
@app.callback(
Output('output2', 'children'),
special
)
def uploaded_files(option, *args):
if option == 'A' and len(args) == 0:
return html.P(f'You selected A, the length of special is: {len(special)}, and length of args is: {len(args)}')
if option == 'A' and len(args) != 0:
return html.P(f'You selected A and entered {args}')
if option == 'B':
return html.P(f'you have selected B, text will be ignored!')
return html.P(f'Before u select, the length of special is: {len(special)}, and length of args is: {len(args)}')
if __name__ == '__main__':
app.run_server(debug=True)