But this is still only one dropdown input, if you add button, The first function's callback has an extra variable(dropdown_value,radioitem_value), and there seems to be no way to determine which value to return.
Hello, can you ask me some questions about dash web development?
is what to say if you want to control both drop down and om, and then call back output based on the last variable change in both selections.
(for example, dropdown and bottom are apples, pears, peaches, and then click the apples in dropdown, then the peaches in bottom, and then the apples, then the peaches)
0 Answer
But this is still only one dropdown input, if you add button, The first function's callback has an extra variable(dropdown_value,radioitem_value), and there seems to be no way to determine which value to return.
This answer quotes ChatGPT
When you need to control the same output with Dropdown and Button, you can use dcc.Store to store your variables and update it when the user changes either control. You can then use the Input component and one or more State components to implement your callback functions to update your output based on recent variable changes.
Here is a sample code that shows how to implement the behavior you describe:
import dash
import dash_core_components as dcc
import dash_html_components as html
om dash.dependencies import Input, Output, State
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Store(id='fruit-storage', data=''),
dcc.Dropdown(
id='fruit-dropdown',
options=[
{'label': 'Apple', 'value': 'apple'},
{'label': 'Pear', 'value': 'pear'},
{'label': 'Peach', 'value': 'peach'}
],
placeholder='Select a fruit'
),
html.Button('Submit', id='submit-button', n_clicks=0),
html.Div(id='output')
])
@app.callback(
Output('fruit-storage', 'data'),
Input('submit-button', 'n_clicks'),
State('fruit-dropdown', 'value'),
State('fruit-storage', 'data')
)
def update_fruit_data(n_clicks, dropdown_value, stored_data):
if n_clicks > 0:
return dropdown_value
return stored_data
@app.callback(
Output('output', 'children'),
Input('fruit-dropdown', 'value'),
Input('submit-button', 'n_clicks'),
State('fruit-storage', 'data')
)
def update_output(dropdown_value, n_clicks, stored_data):
if n_clicks == 0:
return ''
if stored_data == '':
return ''
return f'{stored_data}, {dropdown_value}'
if __name__ == '__main__':
app.run_server(debug=True)
In this example code, fruit-storage stores the fruit selected before the last click of the button, fruit-Dropdown is a drop-down menu that allows the user to select a fruit, and submit-button is a button that when the user clicks it, Saves the most recent selection to fruit-storage. output is a div element that displays the last two selections.
这家伙很懒,什么都没留下...