【发布时间】:2021-11-01 16:22:12
【问题描述】:
我正在尝试使用一些过滤器对侧边栏进行编程,但此下拉列表的回调 update_output 不起作用(没有抛出错误,只是什么都不做)。
我认为是因为这个下拉菜单不在主布局中,因为我的布局是由侧边栏和内容组成的。我在此侧边栏中的下拉菜单将是过滤器,这些过滤器将应用于为dashboard 和dashboard2 布局提供图形的数据框。
我的问题是我应该如何或在哪里对这些回调进行编程,以便为我的侧边栏下拉菜单提供功能?
sidebar = html.Div(
[
html.H2("Sidebar", className="display-4"),
html.Hr(),
html.P(
"Sidebar", className="lead"
),
dbc.Nav(
[
dbc.NavLink("Home", href="/", active="exact"),
dbc.NavLink("Page 1", href="/page-1", active="exact"),
dbc.NavLink("Page 2", href="/page-2", active="exact"),
],
vertical=True,
pills=True,
),
dcc.Dropdown(id='dropdown',value="City"),
html.Br(),
dcc.Dropdown(id='dropdown2',options=[])
],
style=SIDEBAR_STYLE,
)
content = html.Div(id="page-content", children=[], style=CONTENT_STYLE)
app.layout = html.Div([
dcc.Location(id="url"),
sidebar,
content
])
@app.callback(
Output('dropdown2', 'options'),
Input('dropdown', 'value')
)
def update_output(value):
return df[df["cities"].isin(value)]
@app.callback(
Output("page-content", "children"),
[Input("url", "pathname")]
)
def render_page_content(pathname):
if pathname == "/":
return dashboard.layout
elif pathname == "/page-1":
return dashboard.layout
elif pathname == "/page-2":
return dashboard2.layout
# return a 404 message when user tries to reach a different page
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=8000)
【问题讨论】:
标签: python plotly-dash