如何在Dash Python中创建侧边栏

huangapple go评论77阅读模式
英文:

How to create a sidebar in dash python

问题

Here are the translations of the code comments and issues:

  1. 我试图创建一个多页面仪表板,其中页面在顶部固定,侧边栏也固定。但我遇到了一些问题:

  2. 我无法包裹侧边栏中的文本。

  3. 有一个奇怪的边框横穿我的导航栏。

  4. 是否有一种方法可以找出我的侧边栏样式的顶部值,而不是每次手动检查?

  5. 我的代码:

在这里,我提供了主要代码部分的翻译。如果您需要进一步的解释或修复问题,请告诉我。

app.py:

import dash
from dash import html
import dash_bootstrap_components as dbc

app = dash.Dash(__name__, use_pages=True, external_stylesheets=[dbc.themes.SPACELAB], suppress_callback_exceptions=True)
sidebar = dbc.Nav(
            [
                dbc.NavLink(
                    [
                        html.Div(page["name"], className="ms-2"),
                    ],
                    href=page["path"],
                    active="exact",
                )
                for page in dash.page_registry.values()
            ],
            vertical=False,
            pills=True,
            className="text-center border",
            justified=True,
            fill=True,
            style={"position": "fixed"}
)

app.layout = dbc.Container([
    dbc.Row(sidebar),
    html.Hr(),
    dbc.Row(dash.page_container)
], fluid=True)

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

page1.py:

import dash
from dash import html, dcc
import dash_bootstrap_components as dbc

dash.register_page(__name__, path='/', name='Page 1')

SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 42,
    "left": 0,
    "bottom": 0,
    "background-color": "#f8f9fa",
    'overflowY': 'auto'
}

CONTENT_STYLE = {
    "display": "inline-block"
}

content = dcc.RadioItems(['New York City', 'Montreal', 'San Francisco'], 'Montreal', labelStyle={'display':'block'})

sidebar = html.Div(
    [
        html.H2("Sidebar", className="display-4"),
        html.Hr(),
        html.P(
            "A simple sidebar layout with navigation links"
        ),
        content
    ],
    style=SIDEBAR_STYLE,
)

maindiv = html.Div(
    id="first-div",
    children=[
        # first row
        html.Div([
            html.H2("First Row"),
            html.Hr(),
            html.P(
                "First row stuff", className="lead"
            )
        ]),

        # second row
        html.Div([
            html.H2("Second Row"),
            html.Hr(),
            html.P(
                "Second row stuff", className="lead"
            )
        ]),
    ],
    style=CONTENT_STYLE
)

# Create the Dash app layout
layout = html.Div([
    dbc.Row([
        dbc.Col(sidebar, width=2),
        dbc.Col(maindiv, width=10)
    ])
])

如果您需要更多帮助或具体问题的解决方案,请告诉我。

英文:

I am trying to make a multipage dashboard where the pages are fixed on top and there is a sidebar that is also fixed. But I have run into some issues:

  1. I can't wrap my text in the sidebar
  2. There is a weird border that cuts across my navbar
  3. Is there a way to find out the top value for my style of the sidebar without manually checking everytime?

如何在Dash Python中创建侧边栏

My code:
app.py

import dash
from dash import html
import dash_bootstrap_components as dbc

app = dash.Dash(__name__, use_pages=True, external_stylesheets=[dbc.themes.SPACELAB], suppress_callback_exceptions=True)
sidebar = dbc.Nav(
            [
                dbc.NavLink(
                    [
                        html.Div(page["name"], className="ms-2"),
                    ],
                    href=page["path"],
                    active="exact",
                )
                for page in dash.page_registry.values()
            ],
            vertical=False,
            pills=True,
            className="text-center border",
            justified=True,
            fill=True,
            style={"position": "fixed"}
)

app.layout = dbc.Container([
    dbc.Row(sidebar),
    html.Hr(),
    dbc.Row(dash.page_container)
], fluid=True)


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

page1.py

import dash
from dash import html, dcc
import dash_bootstrap_components as dbc

dash.register_page(__name__, path='/', name='Page 1')


SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 42,
    "left": 0,
    "bottom": 0,
    "background-color": "#f8f9fa",
    'overflowY':'auto'
}

CONTENT_STYLE = {
    "display": "inline-block"
}

content = dcc.RadioItems(['New York City', 'Montreal','San Francisco'], 'Montreal', labelStyle={'display':'block'})

sidebar = html.Div(
    [
        html.H2("Sidebar", className="display-4"),
        html.Hr(),
        html.P(
            "A simple sidebar layout with navigation linkasdasdasdasdassdss"
        ),
        content
    ],
    style=SIDEBAR_STYLE,
)

maindiv = html.Div(
    id="first-div",
    children=[
        # first row
        html.Div([
            html.H2("First Row"),
            html.Hr(),
            html.P(
                "First row stuff", className="lead"
            )
        ]),

        # second row
        html.Div([
            html.H2("Second Row"),
            html.Hr(),
            html.P(
                "Second row stuff", className="lead"
            )
        ]),
    ],
    style=CONTENT_STYLE
)

# Create the Dash app layout
layout = html.Div([
    dbc.Row([
        dbc.Col(sidebar, width=2),
        dbc.Col(maindiv, width=10)
    ])
])

How can these issues be fixed?

答案1

得分: 0

以下是翻译好的部分:

这是一个解决方案。

在您的 page1.py 中,注释掉 "position": "fixed" 并重新调整侧边栏和主要内容的宽度。

如何在Dash Python中创建侧边栏

# ...
# ...
# ...

SIDEBAR_STYLE = {
    # "position": "fixed",  # 注释掉这一行
    "top": 42,
    "left": 0,
    "bottom": 0,
    "background-color": "#f8f9fa",
    "overflowY": "auto",
}

# ...
# ...
# ...

# 创建 Dash 应用程序布局
layout = html.Div([
    dbc.Row([
        dbc.Col(sidebar, width=3),  # 调整宽度
        dbc.Col(maindiv, width=9)  # 调整宽度
    ])
])

英文:

Here is one solution.

In your page1.py, comment out "position": "fixed" and re-adjust sidebar and maindiv width.

如何在Dash Python中创建侧边栏

# ...
# ...
# ...

SIDEBAR_STYLE = {
    # "position": "fixed",  # comment this out
    "top": 42,
    "left": 0,
    "bottom": 0,
    "background-color": "#f8f9fa",
    "overflowY": "auto",
}

# ...
# ...
# ...

# Create the Dash app layout
layout = html.Div([
    dbc.Row([
        dbc.Col(sidebar, width=3),  # adjust width
        dbc.Col(maindiv, width=9)  # adjust width
    ])
])

huangapple
  • 本文由 发表于 2023年1月6日 13:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定