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

Plotly Dash Bootstrap Components行列布局与官方示例不符问题排查

问题原因及解决办法

你的代码使用了dash-bootstrap-components(dbc)的Row和Col组件实现栅格布局,但没有引入Bootstrap的样式表,导致dbc布局组件无法生效,所有列只能垂直堆叠,无法呈现官网示例中的横向分栏效果。

解决步骤

在初始化Dash应用时,添加external_stylesheets参数引入Bootstrap样式文件即可:

import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc

# 引入Bootstrap样式表,让dbc布局组件生效
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

main_content = html.Div(
    [
        dbc.Row(dbc.Col(html.Div("A single, half-width column"), width=12)),
        dbc.Row(
            dbc.Col(html.Div("An automatically sized column"), width="auto")
        ),
        dbc.Row(
            [
                dbc.Col(html.Div("One of three columns"), width=3),
                dbc.Col(html.Div("One of three columns")),
                dbc.Col(html.Div("One of three columns"), width=3),
            ]
        ),
    ]
)

app.layout = main_content

if __name__ == '__main__':
    app.run_server(debug=True,)

原理说明

dash-bootstrap-components的Row、Col等布局组件完全依赖Bootstrap的CSS栅格系统,只有加载对应的样式表,组件才能识别width等布局属性,实现横向分栏效果。如果不加载样式,这些组件会退化成普通块级元素,只能垂直堆叠显示。

内容的提问来源于stack exchange,提问作者dsbbsd9

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 21:01:10