You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 03:15:10