英文:
How to create a sidebar in dash python
问题
Here are the translations of the code comments and issues:
-
我试图创建一个多页面仪表板,其中页面在顶部固定,侧边栏也固定。但我遇到了一些问题:
-
我无法包裹侧边栏中的文本。
-
有一个奇怪的边框横穿我的导航栏。
-
是否有一种方法可以找出我的侧边栏样式的顶部值,而不是每次手动检查?
-
我的代码:
在这里,我提供了主要代码部分的翻译。如果您需要进一步的解释或修复问题,请告诉我。
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:
- I can't wrap my text in the sidebar
- There is a weird border that cuts across my navbar
- Is there a way to find out the top value for my style of the sidebar without manually checking everytime?
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"
并重新调整侧边栏和主要内容的宽度。
# ...
# ...
# ...
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.
# ...
# ...
# ...
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
])
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论