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

Python Dash , get value from Input text

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

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)

谢谢

最小工作示例:

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 answer or . If you are interested in handling enter event also, you can find some useful hints in this thread