简体   繁体   English

Dash 引导组件填充

[英]Dash bootstrap components padding

I've been trying to combine the Structuring a Multi-Page App example https://dash.plotly.com/urls with the Dash bootstrap components simple side bar example: https://dash-bootstrap-components.opensource.faculty.ai/examples/simple-sidebar/page-1 .我一直在尝试将构建多页应用程序示例https://dash.plotly.com/urls与 Dash bootstrap 组件简单的侧边栏示例相结合: Z5E056C500A1C4B6A7110BADE -bootstrap-components7110BADE50D807 ai/examples/simple-sidebar/page-1 It works and displays correctly the first time it loads, however each time I navigate from page to page the main div is pushed further and further to the right, the relative padding is seemingly incremented with each page change.它在第一次加载时工作并正确显示,但是每次我从一个页面导航到另一个页面时,主 div 被推得越来越远,相对填充似乎随着每个页面的变化而增加。 How do I avoid this?我该如何避免这种情况?

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





# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "16rem",
    "padding": "2rem 1rem",
    "background-color": "#f8f9fa",
}

# the styles for the main content position it to the right of the sidebar and
# add some padding.
CONTENT_STYLE = {
    "margin-left": "18rem",
    "margin-right": "2rem",
    "padding": "2rem 1rem",
}

sidebar = html.Div(
    [

        dbc.Nav(
            [
                dbc.NavLink("Home", href="/", active="exact"),
                dbc.NavLink("Page 1", href="/apps/app1", active="exact"),
                dbc.NavLink("Page 2", href="/apps/app2", active="exact"),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)
        
content = html.Div(id="page-content",
                   
                   style=CONTENT_STYLE,
                   children = (html.P("I am the main div")
                   
                   ))    
        
layout = html.Div([
    sidebar,
    content

])

I managed to find the solution.我设法找到了解决方案。 The app followed the structure from the Plotly example.该应用程序遵循 Plotly 示例中的结构。 I've included app.py and app1.py, this should be all that is needed to recreate the issue.我已经包含了 app.py 和 app1.py,这应该是重新创建问题所需的全部内容。 The problem came from the fact that a DIV with ID "page-content" from app1.py is nested inside a DIV with ID "page-content" in index.py.问题来自这样一个事实,即 app1.py 中 ID 为“page-content”的 DIV 嵌套在 index.py 中 ID 为“page-content”的 DIV 中。 Renaming the outer DIV resolved the problem.重命名外部 DIV 解决了这个问题。

  • app.py应用程序.py
  • index.py索引.py
  • apps |-- init .py |-- app1.py |-- app2.py应用程序 |--初始化.py |-- app1.py |-- app2.py

app1.py ''' app1.py '''

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app
import dash
import dash_bootstrap_components as dbc



# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "16rem",
    "padding": "2rem 1rem",
    "background-color": "#f8f9fa",
}

# the styles for the main content position it to the right of the sidebar and
# add some padding.
CONTENT_STYLE = {
    "margin-left": "18rem",
    "margin-right": "2rem",
    "padding": "2rem 1rem",
}
sidebar = html.Div(
    [

        dbc.Nav(
            [
                dbc.NavLink("Home", href="/", active="exact"),
                dbc.NavLink("Page 1", href="/apps/app1", active="exact"),
                dbc.NavLink("Page 2", href="/apps/app2", active="exact"),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)

content = html.Div(id="page-content",
                   
                   style=CONTENT_STYLE,
                   children = (html.P("I am the main div")
                   
                   ))    
        
layout = html.Div([
    sidebar,
    content

])


@app.callback(
    Output('app-1-display-value', 'children'),
    Input('app-1-dropdown', 'value'))
def display_value(value):
    return 'You have selected "{}"'.format(value)

index.py '''索引.py '''

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app
from apps import app1, app2


app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])


@app.callback(Output('page-content', 'children'),
              Input('url', 'pathname'))
def display_page(pathname):
    if pathname == '/apps/app1':
        return app1.layout
    elif pathname == '/apps/app2':
        return app2.layout
    else:
        return '404'

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

app.py '''应用程序.py'''

import dash
import dash_bootstrap_components as dbc
app = dash.Dash(__name__, suppress_callback_exceptions=True,external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM