简体   繁体   English

通知用户 Dash 中的无效输入

[英]Inform user of invalid input in Dash

I have created a dashboard using dash.我使用破折号创建了一个仪表板。 It has an input box in which the user enters some data.它有一个输入框,用户可以在其中输入一些数据。 If the data is found in the attached dataframe then a few graphs are shown.如果在随附的 dataframe 中找到数据,则会显示一些图表。 But if the input the user entered is not found in the dataframe then nothing happens and the page stays blank.但是,如果在 dataframe 中找不到用户输入的输入,则不会发生任何事情并且页面保持空白。 Is there a way to show an error message as so if the input is invalid:如果输入无效,有没有办法显示错误消息:

在此处输入图像描述

dash-core-component 's Input component has a pattern property you could use: dash-core-componentInput组件有一个可以使用的pattern属性:

A regular expression that the control's value is checked against.检查控件值的正则表达式。 The pattern must match the entire value, not just some subset.模式必须匹配整个值,而不仅仅是某个子集。 Use the title attribute to describe the pattern to help the user.使用 title 属性来描述模式以帮助用户。 This attribute applies when the value of the type attribute is text, search, tel, url, email, or password, otherwise it is ignored.当 type 属性的值为 text、search、tel、url、email 或密码时,该属性适用,否则将被忽略。 The regular expression language is the same as JavaScript RegExp algorithm, with the 'u' parameter that makes it treat the pattern as a sequence of unicode code points.正则表达式语言与 JavaScript RegExp 算法相同,使用 'u' 参数使其将模式视为 unicode 代码点的序列。 The pattern is not surrounded by forward slashes.该模式没有被正斜杠包围。

Now if you only wanted to validate the format of the string you could pass some regex pattern to the pattern prop.现在,如果您只想验证字符串的格式,您可以将一些正则表达式模式传递给pattern道具。

If you want to validate based on whether the input value is found in your dataframe you could do something like this:如果您想根据是否在 dataframe 中找到输入值进行验证,您可以执行以下操作:

df = pd.DataFrame({"data": ["1", "2", "3"]})

app = dash.Dash()
app.layout = html.Div(
    [dcc.Input(id="input", type="text"), html.Div(id="output")], style={"padding": 10}
)


@app.callback(
    Output("output", "children"),
    Output("input", "pattern"),
    Input("input", "value"),
)
def number_render(input_value):
    pattern = input_value
    filtered_df = df[df["data"] == input_value]

    if filtered_df.empty:
        pattern = pattern + "_invalid"

    return input_value, pattern

The idea here is to filter the dataframe based on the value of the input.这里的想法是根据输入的值过滤 dataframe。 If the input value is not found in the dataframe we want to make the pattern invalid.如果在 dataframe 中找不到输入值,我们想让模式无效。

The way I did it here was to make the pattern value equal to the input value concatenated with another value to make sure the pattern doesn't match.我在这里做的方式是使模式值等于与另一个值连接的输入值,以确保模式不匹配。

If the input value does match the data in the dataframe the pattern is set equal to the input value.如果输入值与 dataframe 中的数据匹配,则将模式设置为等于输入值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM