use*_*064 11 python plotly-dash
我刚刚开始使用破折号。以这里为例。我想转换下面的破折号应用程序
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()
Run Code Online (Sandbox Code Playgroud)
在用户按下按钮而不是在输入字段的值更改时更新。我该如何实现?
Tyl*_*ler 11
这是与这篇文章类似的问题。最新版本中的按钮有一个单击事件可用dash_html_components,但似乎还没有完全记录下来。创作者 chriddyp 曾表示,该Event对象可能不会面向未来,但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')])
Run Code Online (Sandbox Code Playgroud)
您可以使用值作为输入,如果它们发生变化,则无需启动回调。回调仅在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()
Run Code Online (Sandbox Code Playgroud)