gpt4 book ai didi

python - 如何在 Dash-Plotly (Dash-Bootstrap-Components) 中折叠侧边栏

转载 作者:行者123 更新时间:2023-12-04 14:37:11 28 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

去年关闭。




Improve this question




我正在使用 following code它在 Dash Plotly 中创建了一个简单的侧边栏使用 Dash-Bootstrap-Components图书馆。我的目标是实现collapsed sidebar如下图gif通过重用上面的代码。
我不是专家 JavaScript , JQuery , CSS或其他允许我执行此操作的工具。我感谢任何帮助我实现我的目标。
提前致谢。

最佳答案

您可以查看 dash-bootstrap-components examples .
我在示例中添加了两个 css 样式并实现了响应式折叠侧边栏。
这是代码:

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

app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

navbar = dbc.NavbarSimple(
children=[
dbc.Button("Sidebar", outline=True, color="secondary", className="mr-1", id="btn_sidebar"),
dbc.NavItem(dbc.NavLink("Page 1", href="#")),
dbc.DropdownMenu(
children=[
dbc.DropdownMenuItem("More pages", header=True),
dbc.DropdownMenuItem("Page 2", href="#"),
dbc.DropdownMenuItem("Page 3", href="#"),
],
nav=True,
in_navbar=True,
label="More",
),
],
brand="Brand",
brand_href="#",
color="dark",
dark=True,
fluid=True,
)


# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
"position": "fixed",
"top": 62.5,
"left": 0,
"bottom": 0,
"width": "16rem",
"height": "100%",
"z-index": 1,
"overflow-x": "hidden",
"transition": "all 0.5s",
"padding": "0.5rem 1rem",
"background-color": "#f8f9fa",
}

SIDEBAR_HIDEN = {
"position": "fixed",
"top": 62.5,
"left": "-16rem",
"bottom": 0,
"width": "16rem",
"height": "100%",
"z-index": 1,
"overflow-x": "hidden",
"transition": "all 0.5s",
"padding": "0rem 0rem",
"background-color": "#f8f9fa",
}

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

CONTENT_STYLE1 = {
"transition": "margin-left .5s",
"margin-left": "2rem",
"margin-right": "2rem",
"padding": "2rem 1rem",
"background-color": "#f8f9fa",
}

sidebar = html.Div(
[
html.H2("Sidebar", className="display-4"),
html.Hr(),
html.P(
"A simple sidebar layout with navigation links", className="lead"
),
dbc.Nav(
[
dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
],
vertical=True,
pills=True,
),
],
id="sidebar",
style=SIDEBAR_STYLE,
)

content = html.Div(

id="page-content",
style=CONTENT_STYLE)

app.layout = html.Div(
[
dcc.Store(id='side_click'),
dcc.Location(id="url"),
navbar,
sidebar,
content,
],
)


@app.callback(
[
Output("sidebar", "style"),
Output("page-content", "style"),
Output("side_click", "data"),
],

[Input("btn_sidebar", "n_clicks")],
[
State("side_click", "data"),
]
)
def toggle_sidebar(n, nclick):
if n:
if nclick == "SHOW":
sidebar_style = SIDEBAR_HIDEN
content_style = CONTENT_STYLE1
cur_nclick = "HIDDEN"
else:
sidebar_style = SIDEBAR_STYLE
content_style = CONTENT_STYLE
cur_nclick = "SHOW"
else:
sidebar_style = SIDEBAR_STYLE
content_style = CONTENT_STYLE
cur_nclick = 'SHOW'

return sidebar_style, content_style, cur_nclick

# this callback uses the current pathname to set the active state of the
# corresponding nav link to true, allowing users to tell see page they are on
@app.callback(
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
[Input("url", "pathname")],
)
def toggle_active_links(pathname):
if pathname == "/":
# Treat page 1 as the homepage / index
return True, False, False
return [pathname == f"/page-{i}" for i in range(1, 4)]


@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
if pathname in ["/", "/page-1"]:
return html.P("This is the content of page 1!")
elif pathname == "/page-2":
return html.P("This is the content of page 2. Yay!")
elif pathname == "/page-3":
return html.P("Oh cool, this is page 3!")
# If the user tries to reach a different page, return a 404 message
return dbc.Jumbotron(
[
html.H1("404: Not found", className="text-danger"),
html.Hr(),
html.P(f"The pathname {pathname} was not recognised..."),
]
)


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

关于python - 如何在 Dash-Plotly (Dash-Bootstrap-Components) 中折叠侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62732631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com