python - Plotly Dash 中带有过滤器(下拉菜单)的侧边栏
问题描述
我正在尝试使用一些过滤器对侧边栏进行编程,但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)
解决方案
我发现清单更适合这个目的。如果您愿意使用它而不是下拉菜单来过滤数据框,那么下面的完整代码段将生成以下 Plotly Dash 应用程序。
完整代码:
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, ClientsideFunction
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import pandas as pd
import plotly.graph_objs as go
import numpy as np
import plotly.express as px
app = JupyterDash(external_stylesheets=[dbc.themes.SLATE])
df = px.data.stocks()
df = df.set_index('date')
controls = dbc.Card(
[
dbc.FormGroup(
[
dbc.Label("Checklist"),
dcc.Checklist(
id="column_select",
options=[{"label":col, "value": col} for col in df.columns],
value=[df.columns[0]],
labelStyle={'display': 'inline-block', 'width': '12em', 'line-height':'0.5em'}
),
],
),
],
body=True,
style = {'font-size': 'large'}
)
app.layout = dbc.Container(
[
html.H1("Dropdowns and checklists"),
html.Hr(),
dbc.Row([
dbc.Col([controls],xs = 4),
dbc.Col([
dbc.Row([
dbc.Col(dcc.Graph(id="graph"), style={'height': '420px'}),
])
]),
]),
],
fluid=True,
)
@app.callback(
Output("graph", "figure"),
[Input("column_select", "value"),],
)
def make_graph(cols):
fig = px.line(df, x = df.index, y = cols, template = 'plotly_dark')
return fig
app.run_server(mode='external', port = 8982)
推荐阅读
- javascript - 使用 javascript 从网站获取文本
- python - 在 tf.keras 中使用 softmax 作为顺序层和使用 softmax 作为密集层的激活函数有什么区别?
- gem5 - 我可以在 gem5 中更精细地调试吗?
- python - 在 google colab 中使用 datagen.flow_from_directory 并没有提高训练和验证的准确性
- android - Flutter:为 ImagePicker 使用 Listview.builder
- angular - 如何将 ionic 中的 ccavenue 支付网关与 Angular 集成
- html - 如何隔离标签及其内容
- python - 估计文本文件中值的函数
- jquery - 使用 Asp.net Core MVC Bootstrap 4,对于服务器端添加的模型状态错误,我无法让不显眼的验证正常工作
- linux - 如何使用 cURL 发布请求将文件内容作为数据原始有效负载中的字符串发送