我正试图在我的dash_bootstrap_components.Collapse应用程序中实现一个dash,但是它的行为有一个问题。这里的代码,我不是自己写的,我只是从dash_bootstrap_components.Collapse 文档复制它。
import dash
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output, State
app = dash.Dash()
app.layout = html.Div([dbc.Button('Open collapse',
id = 'collapse-button',
className = 'mb-3',
color = 'primary'),
dbc.Collapse(dbc.Card(dbc.CardBody('This content is hidden in the collapse')),
id = 'collapse')])
@app.callback(Output('collapse', 'is_open'),
[Input('collapse-button', 'n_clicks')],
[State('collapse', 'is_open')])
def toggle_collapse(n, is_open):
if n:
return not is_open
return is_open
if __name__ == "__main__":
app.run_server()我得到的是:

当我点击按钮时,什么都不会发生。
我试着找出问题出在哪里,我发现:
n在app.callback中被初始化为None,单击一次就变成1,然后随着点击按钮的次数增加1的数量。is_open在app.callback中被初始化为None,单击后它仍然保持为None,然后在第二次单击成为True之后,在第三次单击False之后,等等。我能做些什么才能让它发挥作用?
版本信息:
Python 3.7.0
dash 1.12.0
dash-bootstrap-components 0.10.1
dash-core-components 1.10.0
dash-html-components 1.0.3
dash-renderer 1.4.1
dash-table 4.7.0
plotly 4.7.0发布于 2020-06-04 14:42:06
如果您链接Boostrap样式表,您的代码将按原样工作。
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])https://stackoverflow.com/questions/62195938
复制相似问题