【问题标题】:Python Dash , get value from Input textPython Dash,从输入文本中获取值
【发布时间】:2018-07-18 16:47:17
【问题描述】:

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

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)

谢谢

【问题讨论】:

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


    【解决方案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 中找到一些有用的提示。

    【讨论】:

    • 我们能否在不将其列为输入或状态的情况下获取另一个文本框的值
    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2022-08-12
    相关资源
    最近更新 更多