Python Dash,从输入文本中获取价值

时间:2018-07-18 16:47:18

标签: python-3.x plotly plotly-dash

我有一个输入和一个按钮,按下按钮时我需要保存文本输入的值。

dcc.Input(id='username', value='Initial Value', type='text'),

html.Button(id='submit-button', children='Submit'),

我在回调中丢失了什么?

@app.callback(Output('output_div','children' ),
          [Input('submit-button')],
          [State('input-element', 'value')],
          [Event('submit-button', 'click'])

 def update_output(input_element):
    print(input_element)

谢谢

1 个答案:

答案 0 :(得分:1)

最小工作示例:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, State, Input

if __name__ == '__main__':
    app = dash.Dash()

    app.layout = html.Div([
        dcc.Input(id='username', value='Initial Value', type='text'),
        html.Button(id='submit-button', type='submit', children='Submit'),
        html.Div(id='output_div')
    ])

    @app.callback(Output('output_div', 'children'),
                  [Input('submit-button', 'n_clicks')],
                  [State('username', 'value')],
                  )
    def update_output(clicks, input_value):
        if clicks is not None:
            print(clicks, input_value)

    app.run_server(host='0.0.0.0')

有关更多信息,请查看this answerthis one。如果您也对处理Enter事件感兴趣,可以在this thread中找到一些有用的提示。