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

如何一次性修改Plotly Dash所有组件的主色调?

解决方案

不用额外CSS文件的话,直接用dash-bootstrap-components自带的create_theme函数就能一次性替换全局主色调,所有组件会自动应用新颜色,不用逐个修改。

操作步骤:

  • 从dbc.themes导入create_theme工具
  • 用该函数定义自定义主题,通过primary参数指定你想要的主色调(支持十六进制、RGB值或标准颜色名)
  • 将生成的自定义主题传入Dash应用的external_stylesheets参数

修改后的完整代码示例:

from dash import Dash
import dash_bootstrap_components as dbc
from dbc.themes import create_theme

# 自定义主题,将主色调设置为深绿色(可替换为你需要的任意颜色值)
custom_theme = create_theme(
    base_theme=dbc.themes.BOOTSTRAP,
    primary="#28a745"
)

app = Dash(__name__, external_stylesheets=[custom_theme])
app.layout = dbc.Container([dbc.Button("Test"),
                            dbc.RadioItems(["Test1", "Test2"], "Test1")])

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

运行后,按钮背景、单选框选中状态等所有使用主色调的组件元素,都会自动切换为你设置的颜色。如果需要调整其他辅助色调(比如secondary、warning),直接在create_theme中添加对应参数即可,例如secondary="#ffc107"。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 20:12:04