如何根据dash plotly中的用户输入显示列

发布于 2025-02-06 18:40:43 字数 1441 浏览 3 评论 0原文

我正在尝试根据用户的输入显示列。例如,未显示第2和3列,但是如果用户类型2,则将显示第2列。但是,即使唯一的输入是2,我当前的程序也显示两个列,我不确定为什么。回调有问题吗?

data = {"1": [1, 2, 3, 4, 5], "2": [6, 7, 8, 9, 10], "3": [11, 12, 13, 14, 15]}
hide_cols = ["2", "3"]

df = pd.DataFrame(data)

app.layout = html.Div(
    [
        "Choose Column to Show: ",
        dcc.Input(id="show_col", type="text", placeholder="", debounce=True),
        html.Br(),
        dash_table.DataTable(
            id="df",
            columns=[{"name": i, "id": i} for i in df.columns],
            data=df.to_dict("records"),
            row_deletable=True,
        ),
    ]
)


@app.callback(Output("df", "style_data_conditional"), Input("show_col", "value"))
def show_column_data(value):
    if value is not None and value in hide_cols:
        return [{"if": {"column_id": value}, "display": ""}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


@app.callback(Output("df", "style_header_conditional"), Input("show_col", "value"))
def show_column_header(value):
    if value is not None and value in hide_cols:
        return [{"if": {"column_id": value}, "display": ""}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


if __name__ == "__main__":
    app.run_server(debug=True)

用户输入之前

png“ rel =” nofollow noreferrer>在用户输入之后

I'm trying to display a column based on the user's input. For example, columns 2 and 3 are not displayed, but if the user types 2, column 2 will be displayed. However, my current program is displaying both columns even if the only input is 2 and I'm not sure why. Is there an issue with the callback?

data = {"1": [1, 2, 3, 4, 5], "2": [6, 7, 8, 9, 10], "3": [11, 12, 13, 14, 15]}
hide_cols = ["2", "3"]

df = pd.DataFrame(data)

app.layout = html.Div(
    [
        "Choose Column to Show: ",
        dcc.Input(id="show_col", type="text", placeholder="", debounce=True),
        html.Br(),
        dash_table.DataTable(
            id="df",
            columns=[{"name": i, "id": i} for i in df.columns],
            data=df.to_dict("records"),
            row_deletable=True,
        ),
    ]
)


@app.callback(Output("df", "style_data_conditional"), Input("show_col", "value"))
def show_column_data(value):
    if value is not None and value in hide_cols:
        return [{"if": {"column_id": value}, "display": ""}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


@app.callback(Output("df", "style_header_conditional"), Input("show_col", "value"))
def show_column_header(value):
    if value is not None and value in hide_cols:
        return [{"if": {"column_id": value}, "display": ""}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


if __name__ == "__main__":
    app.run_server(debug=True)

Before user input

After user input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2025-02-13 18:40:44

没关系!我设法弄清楚了,我只需要添加另一个style_data_conditional和style_header_conditional,它将额外的列隐藏着:

@app.callback(Output("df", "style_data_conditional"), Input("show_col", "value"))
def show_column_data(value):
    if value is not None and value in hide_cols:
        index = hide_cols.index(value)
        keep_hidden = hide_cols[:index] + hide_cols[index + 1 :]

        return [{"if": {"column_id": value}, "display": ""}] + [{"if": {"column_id": keep_hidden}, "display": "None"}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


@app.callback(Output("df", "style_header_conditional"), Input("show_col", "value"))
def show_column_header(value):
    if value is not None and value in hide_cols:
        index = hide_cols.index(value)
        keep_hidden = hide_cols[:index] + hide_cols[index + 1 :]

        return [{"if": {"column_id": value}, "display": ""}] + [{"if": {"column_id": keep_hidden}, "display": "None"}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]

Never mind! I managed to figure it out, I just had to add another style_data_conditional and style_header_conditional that keeps the extra columns hidden:

@app.callback(Output("df", "style_data_conditional"), Input("show_col", "value"))
def show_column_data(value):
    if value is not None and value in hide_cols:
        index = hide_cols.index(value)
        keep_hidden = hide_cols[:index] + hide_cols[index + 1 :]

        return [{"if": {"column_id": value}, "display": ""}] + [{"if": {"column_id": keep_hidden}, "display": "None"}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]


@app.callback(Output("df", "style_header_conditional"), Input("show_col", "value"))
def show_column_header(value):
    if value is not None and value in hide_cols:
        index = hide_cols.index(value)
        keep_hidden = hide_cols[:index] + hide_cols[index + 1 :]

        return [{"if": {"column_id": value}, "display": ""}] + [{"if": {"column_id": keep_hidden}, "display": "None"}]
    else:
        return [{"if": {"column_id": hide_cols}, "display": "None"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文