【问题标题】:Is there a way how I can specify which .css file stored in the assets folder my app should use?有没有办法指定我的应用程序应该使用的资产文件夹中存储的 .css 文件?
【发布时间】:2022-01-04 16:08:13
【问题描述】:

对于一个项目,我正在尝试使用 Dash 创建一个 Web 应用程序仪表板。

因为我有多个具有不同布局的网络应用程序版本,我希望我的应用程序的每个版本都指向一个特定的 .css 文件,存储在我的资产文件夹中,以供使用。

我不想删除用于不同版本但位于同一项目文件夹中的其他 .css 文件。其他的.css文件基本上应该忽略。

project
│   app.py
│   app_custom.py    
│
└───/assets
    │   style.css
    │   custom_style.css

因此,如果我运行 app_custom.py,我希望应用只使用 style_custom.css

在我的代码下面:

# external css/js files are loaded before .css files in /assets
external_stylesheets = [dbc.themes.BOOTSTRAP,
    {
        "href": "https://fonts.googleapis.com/css2?"
        "family=Lato:wfght@400;700&display=swap",
        "rel": "stylesheet",
    }
]

app = Dash(__name__, external_stylesheets=external_stylesheets)

app.index_string = '''
<!DOCTYPE html>
<html>
    <head>
        {%metas%}
        <title>{%title%}</title>
        {%favicon%}
        {%css%}
    </head>
    <body>
        <div>My Custom header</div>
        {%app_entry%}
        <footer>
            {%config%}
            {%scripts%}
            {%renderer%}
        </footer>
        <div>My Custom footer</div>
    </body>
</html>
'''

app.layout = html.Div(
    dbc.Button("Success", color="success", className="mr-1")
)

if __name__ == "__main__":
    app.run_server()

【问题讨论】:

    标签: python css plotly-dash


    【解决方案1】:

    您可以使用以下命令忽略资产文件夹中的某些文件:

    app = dash.Dash(assets_ignore='fileToIgnore')
    

    解释这一点的文档:https://dash.plotly.com/external-resources

    我会编译你的资产文件夹中所有文件的列表,然后在将列表传递给 assets_ignore 参数之前从列表中删除要使用的文件。

    【讨论】:

    • 感谢您的回答!!
    猜你喜欢
    • 2021-12-30
    • 2012-04-22
    • 1970-01-01
    • 2022-10-18
    • 2019-02-09
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多