如何用dash_bootstrap_components创建跨多行的指定仪表盘布局?
实现指定的dash-bootstrap-components布局
当然可以实现你要的布局,dash-bootstrap-components(DBC)完全支持这种跨多行的组件排列,核心是利用Col组件的rowspan属性来控制组件的垂直跨度。
核心实现思路
- 组件1通过
rowspan=2设置为跨两行显示 - 右侧区域使用嵌套的
Row拆分上下两部分:上半部分放置组件2和3,下半部分放置组件4 - 利用DBC的12栅格系统分配列宽,保证整体布局对齐
完整代码示例
import dash from dash import html import dash_bootstrap_components as dbc # 初始化Dash应用 app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) # 构建布局 app.layout = dbc.Container( dbc.Row([ # 组件1:跨2行,占3列宽度 dbc.Col( html.Div("组件1", className="bg-primary text-white p-4 text-center"), width=3, rowspan=2 ), # 右侧嵌套布局容器,占9列宽度 dbc.Col([ # 上半行:组件2和3 dbc.Row([ dbc.Col(html.Div("组件2", className="bg-secondary text-white p-4 text-center"), width=6), dbc.Col(html.Div("组件3", className="bg-success text-white p-4 text-center"), width=6) ], className="mb-2"), # 上下行之间添加间距 # 下半行:组件4 dbc.Row( dbc.Col(html.Div("组件4", className="bg-info text-white p-4 text-center"), width=12) ) ], width=9) ]), fluid=True ) # 运行应用 if __name__ == "__main__": app.run_server(debug=True)
关键说明
rowspan=2是实现组件1跨多行的核心参数,DBC的Col组件原生支持该属性- 代码中添加的
className样式(背景色、内边距等)是为了直观展示布局结构,实际开发中可以替换为你的仪表盘组件(如dcc.Graph、dbc.Card等) - 12栅格系统的宽度分配(3+9、6+6、12)保证了布局的响应式适配,在不同屏幕尺寸下会自动调整
内容的提问来源于stack exchange,提问作者Ben Karl
相关产品推荐
相关产品推荐

