【问题标题】:Python Dash navigation and dashboard from bootstrap template not working来自引导模板的 Python Dash 导航和仪表板不起作用
【发布时间】:2018-09-10 12:18:07
【问题描述】:

由于几乎没有任何可用于 Python Dash 的模板,我正在构建自己的仪表板/导航模板,使用可用的资源(如引导程序和标准 jquery)。

我将 this template 转换为 Python Dash,但由于某种原因,切换和某些 css 都不能像在现场 demo 中那样工作。我假设我对data-*arial-* 参数做错了,但为了完整起见,我提供了所有代码,除了css,可以在演示源代码中找到。我的模板不能像现场演示那样按预期工作的原因是什么?特别是,为什么切换不起作用,为什么标记不同?

app.py:

import dash
import dash_html_components as html

external_stylesheets = [
    {
        'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css',
        'rel': 'stylesheet',
        'integrity': 'sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4',
        'crossorigin': 'anonymous'
    }
]

external_scripts = [
    {
        'src': 'https://use.fontawesome.com/releases/v5.0.13/js/solid.js',
        'integrity': 'sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js',
        'integrity': 'sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
        'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js',
        'integrity': 'sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js',
        'integrity': 'sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm',
        'crossorigin': 'anonymous'
    }
]


app = dash.Dash(
    __name__,
    external_scripts=external_scripts,
    external_stylesheets=external_stylesheets)

app.layout = html.Div(
    className='wrapper',
    children=[
        # <!-- Sidebar -->
        html.Nav(
            id='sidebar',
            children=[
                html.Div(
                    children=html.H3('DGTL'),
                    className='sidebar-header'
                ),
                html.Ul(
                    className='list-unstyled components',
                    children=[
                        html.P('Navigation'),
                        html.Li(
                            className='active',
                            children=[
                                html.A(
                                    'Home',
                                    href='#homeSubmenu',
                                    className = 'dropdown-toggle',
                                    **{'aria-expanded': 'false', 'data-toggle': 'collapse'}
                                ),
                                html.Ul(
                                    className='collapse list-unstyled',
                                    id='homeSubmenu',
                                    children=[
                                        html.Li(
                                            html.A(
                                                'Home 1',
                                                href='#'
                                            )
                                        ),
                                        html.Li(
                                            html.A(
                                                'Home 2',
                                                href='#'
                                            )
                                        ),
                                        html.Li(
                                            html.A(
                                                'Home 3',
                                                href='#'
                                            )
                                        ),
                                    ]
                                )
                            ]
                        ),
                        html.Li(
                            children=[
                                html.A(
                                    'About',
                                    href='#'
                                )
                            ]
                        ),
                        html.Li(
                            children=[
                                html.A(
                                    'Contact',
                                    href='#'
                                )
                            ]
                        )
                    ]
                )
            ]

        ),

         # <!-- Page Content -->
         html.Div(
             id='content',
             children=[
                 html.Nav(
                     className = 'navbar navbar-expand-lg navbar-light bg-light',
                     children=[
                         html.Div(
                             className = 'container-fluid',
                             children = [
                                 html.Button(
                                     type = 'button',
                                     id = 'sidebarCollapse',
                                     className = 'btn btn-info',
                                     children = [
                                         html.I(className='fas fa-align-left'),
                                         html.Span('Toggle Sidebar')
                                     ]
                                 )
                             ]
                         )
                     ]
                 )
             ]
         ),
        html.Script(
            '''
            $(document).ready(function () {
                $('#sidebarCollapse').on('click', function () {
                    $('#sidebar').toggleClass('active');
                });
            });
            '''
        )
    ]
)

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

【问题讨论】:

  • data-* 领域仍然没有得到完全支持,我认为在 dash 中,它仍在积极开发中。这是实现此任务的问题:community.plot.ly/t/can-data-attributes-be-created-in-dash/7222
  • 这是否会出现导航栏中不切换和错误颜色选项卡的行为?
  • 是的,据我所知
  • 嗨@Dendrobates,你解决了吗?

标签: python bootstrap-4 plotly-dash


【解决方案1】:

您的侧边栏没有切换的原因是因为html.Script 不是在页面加载时加载,而是在稍后 dash 呈现页面时加载。

如果您使用 chrome 右键单击​​您的页面并查看源代码(不要使用开发者控制台),那么您可以看到页面加载时加载的内容。

您需要将 javascript 添加为文件,以便将其包含在初始页面中。 详情请见Adding Your Own CSS and JavaScript to Dash Apps

【讨论】:

  • 嗨@phifre你是对的。我已经尝试过您的解决方案并且它有效。但是,当我重新启动我的应用程序时,它不再工作了。有时它可以正常工作和加载,有时却不能,会发生什么?当我转到开发人员工具、网络、JS 时,我看到“请求标头:显示临时标头”。是这个问题吗?
【解决方案2】:

@phifre 给出了部分正确的answer,所以为了完整起见,我想给出一个解决所有问题和 cmets 的问题。

data-*aria-* 属性与侧边栏的切换无关。 data-* 属性用于折叠提到的demo 中的 Home 子菜单。

此外,对于@JustInTime 的信息,data-*aria-* 属性的问题在被问到这个问题时已经是fixed

我尝试了@phifre 给出的答案,但也可能出于同样的原因也没有成功。 javascript 无法找到稍后由破折号呈现的#sidebarCollapse 元素。

“导航栏中的标签颜色错误”可能是 CSS 问题。

解决方案

由于侧边栏和按钮是静态的,不影响任何dash组件,我建议通过index_string插入。见Customizing Dash's HTML Index Template。在下面工作app.py

import dash
import dash_html_components as html

external_stylesheets = [
    {
        'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css',
        'rel': 'stylesheet',
        'integrity': 'sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4',
        'crossorigin': 'anonymous'
    }
]

external_scripts = [
    {
        'src': 'https://use.fontawesome.com/releases/v5.0.13/js/solid.js',
        'integrity': 'sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://use.fontawesome.com/releases/v5.0.13/js/fontawesome.js',
        'integrity': 'sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
        'integrity': 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js',
        'integrity': 'sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ',
        'crossorigin': 'anonymous'
    },
    {
        'src': 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js',
        'integrity': 'sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm',
        'crossorigin': 'anonymous'
    }
]


app = dash.Dash(
    __name__,
    external_scripts=external_scripts,
    external_stylesheets=external_stylesheets)

app.index_string = '''<!DOCTYPE html>
<html>
    <head>
        {%metas%}
        <title>{%title%}</title>
        {%favicon%}
        {%css%}
        <link rel="stylesheet" href="https://bootstrapious.com/tutorial/sidebar/style.css">
    </head>
    <body>
        <div class="wrapper">
            <nav id="sidebar">
                <div class="sidebar-header">
                    <h3>DGTL</h3>
                </div>
                <ul class="list-unstyled components">
                    <p>Navigation</p>
                    <li class="active">
                        <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle">Home</a>
                        <ul class="collapse list-unstyled" id="homeSubmenu">
                            <li>
                                <a href="#">Home 1</a>
                            </li>
                            <li>
                                <a href="#">Home 2</a>
                            </li>
                            <li>
                                <a href="#">Home 3</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </nav>
            <div id="content">
                <nav class="navbar navbar-expand-lg navbar-light bg-light">
                    <div class="container-fluid">
                        <button type="button" id="sidebarCollapse" class="btn btn-info">
                            <i class="fas fa-align-left"></i>
                            <span>Toggle Sidebar</span>
                        </button>
                    </div>
                </nav>
            </div>
            {%app_entry%}
            <footer>
                {%config%}
                {%scripts%}
                <script type="text/javascript">
                    $(document).ready(function () {
                        $('#sidebarCollapse').on('click', function () {
                            $('#sidebar').toggleClass('active');
                        });
                    });
                </script>
            </footer>
        </div>
    </body>
</html>'''
app.layout = html.Div()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多