这是一个与此类似的问题post https://stackoverflow.com/questions/45579844/dash-core-component-for-basic-button-with-click-event。最新的按钮有一个点击事件dash_html_components
,但似乎尚未完整记录。创作者 Chriddyp 拥有stated https://community.plot.ly/t/is-there-a-way-to-only-update-on-a-button-press-for-apps-where-updates-are-slow/4679/7认为Event
对象可能不是面向未来的,但是State
应该。
Using State
like:
@app.callback(
Output('output', 'children'),
[Input('button-2', 'n_clicks')],
state=[State('input-1', 'value'),
State('input-2', 'value'),
State('slider-1', 'value')])
您可以使用值作为输入,而无需在它们更改时启动回调。仅当以下情况时才会触发回调Input('button', 'n_clicks')
更新。
因此,对于您的示例,我添加了一个按钮并将现有 html.Input 的值提供给 State 对象:
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Button('Click Me', id='button'),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input('button', 'n_clicks')],
state=[State(component_id='my-id', component_property='value')]
)
def update_output_div(n_clicks, input_value):
return 'You\'ve entered "{}" and clicked {} times'.format(input_value, n_clicks)
if __name__ == '__main__':
app.run_server()