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

dej*_*dej 3 python-3.x plotly plotly-dash

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

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

html.Button(id='submit-button', children='Submit'),
Run Code Online (Sandbox Code Playgroud)

我的回调中遗漏了一些东西吗?

@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)
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 6

最小工作示例:

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')
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您可以查看此答案此答案。如果您也对处理 Enter 事件感兴趣,您可以在此线程中找到一些有用的提示。

  • 我们可以获取另一个文本框的值而不将其列为输入或状态吗 (2认同)