如何根据dash plotly中的用户输入显示列
我正在尝试根据用户的输入显示列。例如,未显示第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)
用户输入之前
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系!我设法弄清楚了,我只需要添加另一个style_data_conditional和style_header_conditional,它将额外的列隐藏着:
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: