Plotly Dash:dash_bootstrap_components.Collapse 不会崩溃
Plotly Dash: dash_bootstrap_components.Collapse does not collapse
我正在尝试在我的 dash
应用程序中实现 dash_bootstrap_components.Collapse
,但它的行为存在问题。这里的代码,我不是自己写的,我只是从dash_bootstrap_components.Collapse
documentation:
复制过来的
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()
这是我得到的:
当我点击按钮时,没有任何反应。
我试图弄清楚问题出在哪里,我发现:
app.callback
中的n
初始化为None
,点击后变为1
,然后随着次数增加1
按钮点击,正确
app.callback
中的is_open
初始化为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
如果您 link Bootstrap 样式表,您的代码将按原样工作。
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
我正在尝试在我的 dash
应用程序中实现 dash_bootstrap_components.Collapse
,但它的行为存在问题。这里的代码,我不是自己写的,我只是从dash_bootstrap_components.Collapse
documentation:
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
初始化为None
,点击后变为1
,然后随着次数增加1
按钮点击,正确is_open
初始化为None
,点击后还是None
,第二次点击后变成True
,第三次点击后False
依此类推
app.callback
中的app.callback
中的我该怎么做才能让它发挥作用?
版本信息:
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
如果您 link Bootstrap 样式表,您的代码将按原样工作。
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])