2017-08-17 69 views
1

我刚开始用破折号。以here为例。我想在仪表应用转换下面如何使用按钮触发回叫更新?

import dash 
from dash.dependencies import Input, Output 
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.Div(id='my-div') 
]) 

@app.callback(
    Output(component_id='my-div', component_property='children'), 
    [Input(component_id='my-id', component_property='value')] 
) 
def update_output_div(input_value): 
    return 'You\'ve entered "{}"'.format(input_value) 

if __name__ == '__main__': 
    app.run_server() 

要更新,当用户按下一个按钮,而不是当的输入字段的值更改。我该如何做到这一点?

+0

这原来是重复[这](https://开头计算器.com/questions/45579844/dash-core-component-for-basic-button-click-event)问题。 – user25064

回答

1

这是一个类似的问题,这个post。在最新的dash_html_components中有一个按钮可用的点击事件,但它似乎尚未完整记录。创建者chriddyp具有statedEvent对象可能不是未来验证的,但State应该是。

使用State喜欢:

@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值:

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()