如何在仪表板应用程序的文本框中输入用户延迟时间?

时间:2018-10-04 04:14:13

标签: python data-visualization dashboard plotly-dash

我正在学习构建一个简单的破折号应用程序。它具有一个供用户输入的文本框,具体取决于绘制文本的方式。

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

app = dash.Dash()

app.layout = html.Div(children=[
    html.Div(children='User input:'),
    dcc.Input(id='input', value='', type='text'),
    html.Div(id='output-graph'),
])

@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='input', component_property='value')]
)
def update_value(input_data):
    return dcc.Graph(
    ### Do something ###
    )

问题在于,由于破折号在后台使用了react,因此只要输入有效,它就会在控制台中不断抛出错误。例如,如果我要绘制股票代码(例如AAPL)的股票价格,则在键入每个字母直到输入所有4个有效字母后都会引发错误。

我想在用户输入最后一个字符后,例如在应用尝试读取输入之前添加1秒的延迟。我怎么做?我一直在网上搜索有关此内容,但找不到任何内容。

此外,如果经过1秒延迟后的输入是无效输入(例如,AAPF而不是AAPL),则它应返回类似Wrong input, please enter again.

1 个答案:

答案 0 :(得分:3)

古老的问题,但我在寻找同一件事时遇到了它。随后,我在密谋论坛上发现debounce关键字参数已添加到仪表板核心组件中(从0.35.0版开始)。如果debounceTrue,则只有在用户按下Enter键或在框外单击时,才传达输入内容。

例如:

dcc.Input(
    id='user-input',
    type='number',
    value=30,            
    style={'fontSize':28},
    debounce = True
),