如何在 Dash 回调中使用多个状态?
How to use multiple States in Dash callback?
我想在一个回调中使用多个状态。我没有让它工作,所以我检查了下面的示例 from the documentation,其中在回调中使用了多个状态。然而,当我 运行 这段代码时,我得到了错误:
输入参数submit-button-state.n_clicks
必须是列表或元组
dash.dependencies.Input
s
两个问题:
- 有什么变化吗?文档是否过时了? (这似乎不太可能,因为他们有很好的文档)
- 如何让它工作?
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id='input-1-state', type='text', value='Montréal'),
dcc.Input(id='input-2-state', type='text', value='Canada'),
html.Button(id='submit-button-state', n_clicks=0, children='Submit'),
html.Div(id='output-state')
])
@app.callback(Output('output-state', 'children'),
Input('submit-button-state', 'n_clicks'),
State('input-1-state', 'value'),
State('input-2-state', 'value'))
def update_output(n_clicks, input1, input2):
return u'''
The Button has been pressed {} times,
Input 1 is "{}",
and Input 2 is "{}"
'''.format(n_clicks, input1, input2)
if __name__ == '__main__':
app.run_server(debug=True)
正如错误提示的那样简单明了。我已经尝试过一些联系,但可能我在某处打错了字。
解决方案是将输入、输出和状态全部放在一个单独的列表中。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id='input-1-state', type='text', value='Montréal'),
dcc.Input(id='input-2-state', type='text', value='Canada'),
html.Button(id='submit-button-state', n_clicks=0, children='Submit'),
html.Div(id='output-state')
])
@app.callback([Output('output-state', 'children')],
[Input('submit-button-state', 'n_clicks')],
[State('input-1-state', 'value'),
State('input-2-state', 'value')])
def update_output(n_clicks, input1, input2):
return u'''
The Button has been pressed {} times,
Input 1 is "{}",
and Input 2 is "{}"
'''.format(n_clicks, input1, input2)
if __name__ == '__main__':
app.run_server(debug=True)
问题中的代码有效——在 Dash 的最新版本中,当前版本为 1.20.0。
将 @app.callback
的所有 Output
、Input
和 State
参数放在三个单独的列表中的要求(如 Ger 在回答中所建议的那样)在2020 年 8 月版本 1.15.0(参见 release notes)。正如文档中的示例所示,不再建议这样做。如果可能,请更新您的 Dash 版本,而不是切换到旧样式。
我想在一个回调中使用多个状态。我没有让它工作,所以我检查了下面的示例 from the documentation,其中在回调中使用了多个状态。然而,当我 运行 这段代码时,我得到了错误:
输入参数submit-button-state.n_clicks
必须是列表或元组
dash.dependencies.Input
s
两个问题:
- 有什么变化吗?文档是否过时了? (这似乎不太可能,因为他们有很好的文档)
- 如何让它工作?
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id='input-1-state', type='text', value='Montréal'),
dcc.Input(id='input-2-state', type='text', value='Canada'),
html.Button(id='submit-button-state', n_clicks=0, children='Submit'),
html.Div(id='output-state')
])
@app.callback(Output('output-state', 'children'),
Input('submit-button-state', 'n_clicks'),
State('input-1-state', 'value'),
State('input-2-state', 'value'))
def update_output(n_clicks, input1, input2):
return u'''
The Button has been pressed {} times,
Input 1 is "{}",
and Input 2 is "{}"
'''.format(n_clicks, input1, input2)
if __name__ == '__main__':
app.run_server(debug=True)
正如错误提示的那样简单明了。我已经尝试过一些联系,但可能我在某处打错了字。
解决方案是将输入、输出和状态全部放在一个单独的列表中。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Input(id='input-1-state', type='text', value='Montréal'),
dcc.Input(id='input-2-state', type='text', value='Canada'),
html.Button(id='submit-button-state', n_clicks=0, children='Submit'),
html.Div(id='output-state')
])
@app.callback([Output('output-state', 'children')],
[Input('submit-button-state', 'n_clicks')],
[State('input-1-state', 'value'),
State('input-2-state', 'value')])
def update_output(n_clicks, input1, input2):
return u'''
The Button has been pressed {} times,
Input 1 is "{}",
and Input 2 is "{}"
'''.format(n_clicks, input1, input2)
if __name__ == '__main__':
app.run_server(debug=True)
问题中的代码有效——在 Dash 的最新版本中,当前版本为 1.20.0。
将 @app.callback
的所有 Output
、Input
和 State
参数放在三个单独的列表中的要求(如 Ger 在回答中所建议的那样)在2020 年 8 月版本 1.15.0(参见 release notes)。正如文档中的示例所示,不再建议这样做。如果可能,请更新您的 Dash 版本,而不是切换到旧样式。