用 dash_bootstrap_components 为 dash 生成美观的导航栏

先安装 pip 包:

pip install dash-bootstrap-components

把 bootstrap 的 sheetstyle 加入你的 app 中:

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

生成 navbar:

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

PLOTLY_LOGO = "https://images.plot.ly/logo/new-branding/plotly-logomark.png"

search_bar = dbc.Row(
    [
        dbc.Col(dbc.Input(type="search", placeholder="Search")),
        dbc.Col(
            dbc.Button("Search", color="primary", className="ml-2"),
            width="auto",
        ),
    ],
    no_gutters=True,
    className="ml-auto flex-nowrap mt-3 mt-md-0",
    align="center",
)

navbar = dbc.Navbar(
    [
        html.A(
            # Use row and col to control vertical alignment of logo / brand
            dbc.Row(
                [
                    dbc.Col(html.Img(src=PLOTLY_LOGO, height="30px")),
                    dbc.Col(dbc.NavbarBrand("Navbar", className="ml-2")),
                ],
                align="center",
                no_gutters=True,
            ),
            href="https://plot.ly",
        ),
        dbc.NavbarToggler(id="navbar-toggler"),
        dbc.Collapse(search_bar, id="navbar-collapse", navbar=True),
    ],
    color="dark",
    dark=True,
)


# add callback for toggling the collapse on small screens
@app.callback(
    Output("navbar-collapse", "is_open"),
    [Input("navbar-toggler", "n_clicks")],
    [State("navbar-collapse", "is_open")],
)
def toggle_navbar_collapse(n, is_open):
    if n:
        return not is_open
    return is_open

效果图:

 

欢迎关注我的知乎专栏【数据池塘】,专注于分享机器学习、数据挖掘相关内容:https://zhuanlan.zhihu.com/datapool

 

⬇️ 扫描下方二维码关注公众号【数据池塘】 ⬇️

回复【算法】,获取最全面的机器学习算法网络图:

### 解决 Python `ModuleNotFoundError` 错误:dash_bootstrap_components 当遇到 `ModuleNotFoundError: No module named 'dash_bootstrap_components'` 错误时,这通常意味着该库未被正确安装或环境配置存在问题。 #### 验证当前环境中是否存在 dash_bootstrap_components 库 可以先通过以下命令验证是否已成功安装此库: ```bash pip show dash-bootstrap-components ``` 如果显示找不到包,则说明尚未安装。 #### 正确安装 dash_bootstrap_components 方法 对于大多数标准 Python 环境而言,推荐使用 pip 来完成安装操作: ```bash pip install dash-bootstrap-components ``` 而对于基于 Anaconda 的开发环境来说,建议采用 conda 渠道进行安装以保持依赖关系的一致性: ```bash conda install -c conda-forge dash-bootstrap-components ``` #### 导入库的方式 确保在代码文件顶部按照如下方式引入所需模块: ```python import dash_bootstrap_components as dbc ``` #### 常见问题排查 - **虚拟环境激活状态**:确认工作于正确的 Python 虚拟环境中[^1]。 - **多版本冲突**:如果有多个 Python 版本共存,请指定特定版本下的 pip 工具执行安装任务,比如 python3.x -m pip install dash-bootstrap-components。 - **权限不足**:尝试加上 --user 参数来绕过全局写权限限制,即 pip install --user dash-bootstrap-components。 #### 示例应用初始化设置 创建一个新的 Dash 应用程序实例并连接到 Bootstrap 主题样式表: ```python from dash import Dash import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风控大鱼

如果帮到了您,请我喝杯咖啡吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值