将破折号下拉选择传递给变量
Pass Dash Dropdown Selection To Variable
我在获取参数以继承仪表板中的用户下拉选择时遇到问题。
下面是我的代码的三个部分。我不认为问题出在 layout/graphing 部分,因为如果我删除 Dropdown 对象并显式分配 boro
变量(即取消注释部分中的第一行),我就可以填充图表1).此外,如果我在没有图形的情况下进行测试,而只是尝试打印变量值,则在使用下拉菜单时它仍然显示为空白。
有人可以提供一些帮助吗?我正在处理 Plotly Dash 教程 (https://dash.plot.ly/dash-core-components/dropdown) 中给出的示例,但我不确定我是否正确翻译了它们。
第 1 部分:从 API
中提取数据
#boro = 'Bronx'
def getHealth(boro):
health_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
'$select=health,count(tree_id)' +\
'&$where=boroname=\'' + boro + '\'' +\
'&$group=health').replace(' ', '%20')
health_trees = pd.read_json(health_url)
def getStew(boro):
stew_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
'$select=steward,health,count(tree_id)' +\
'&$where=boroname=\'' + boro + '\'' +\
'&$group=steward,health').replace(' ', '%20')
stew_trees = pd.read_json(stew_url)
第 2 部分:设置布局,创建图表
app.layout = html.Div(children=[
html.H1(children='Trees Overview'),
html.Div(children='''
HEALTH QUALITY
'''),
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in trees.boroname.unique()],
value='Bronx',
clearable=False
),
html.Div(id='table-container'),
])
def generateGraphs(df1,df2,df3):
return dcc.Graph(
id='graph1',
figure={
'data': [
go.Pie(labels=df1['health'],values=df1['count_tree_id']),
],
'layout': {
'title': 'Count By Species'
}
}
),
dcc.Graph(
id='graph2',
figure={
'data': [
go.Bar(x=df2[df2['steward']==i]['health']
,y=df2[df2['steward']==i]['count_tree_id']
,name=i
,marker=go.bar.Marker(
color='rgb(26, 118, 255)'
))
for i in df3['steward'].unique()
]
}
)
第 3 部分:我认为这是问题所在。我是否正确使用了 @app.callback
函数?我是否正确定义了 retHealth()
并调用了 generateGraphs()
?
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('dropdown', 'value')])
def retHealth(value):
health=getHealth(value)
stew=getStew(value)
return generateGraphs(health,stew,trees)
你的回调没问题。
问题是您没有 return getHealth
和 getStew
中的任何内容。这意味着 None
是 return 隐式编辑的。
因此将这些函数更改为 return 用于绘制图形的 DataFames:
def getHealth(boro):
health_url = (
"https://data.cityofnewyork.us/resource/nwxe-4ae8.json?"
+ "$select=health,count(tree_id)"
+ "&$where=boroname='"
+ boro
+ "'"
+ "&$group=health"
).replace(" ", "%20")
return pd.read_json(health_url)
def getStew(boro):
stew_url = (
"https://data.cityofnewyork.us/resource/nwxe-4ae8.json?"
+ "$select=steward,health,count(tree_id)"
+ "&$where=boroname='"
+ boro
+ "'"
+ "&$group=steward,health"
).replace(" ", "%20")
return pd.read_json(stew_url)
我在获取参数以继承仪表板中的用户下拉选择时遇到问题。
下面是我的代码的三个部分。我不认为问题出在 layout/graphing 部分,因为如果我删除 Dropdown 对象并显式分配 boro
变量(即取消注释部分中的第一行),我就可以填充图表1).此外,如果我在没有图形的情况下进行测试,而只是尝试打印变量值,则在使用下拉菜单时它仍然显示为空白。
有人可以提供一些帮助吗?我正在处理 Plotly Dash 教程 (https://dash.plot.ly/dash-core-components/dropdown) 中给出的示例,但我不确定我是否正确翻译了它们。
第 1 部分:从 API
中提取数据#boro = 'Bronx'
def getHealth(boro):
health_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
'$select=health,count(tree_id)' +\
'&$where=boroname=\'' + boro + '\'' +\
'&$group=health').replace(' ', '%20')
health_trees = pd.read_json(health_url)
def getStew(boro):
stew_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
'$select=steward,health,count(tree_id)' +\
'&$where=boroname=\'' + boro + '\'' +\
'&$group=steward,health').replace(' ', '%20')
stew_trees = pd.read_json(stew_url)
第 2 部分:设置布局,创建图表
app.layout = html.Div(children=[
html.H1(children='Trees Overview'),
html.Div(children='''
HEALTH QUALITY
'''),
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in trees.boroname.unique()],
value='Bronx',
clearable=False
),
html.Div(id='table-container'),
])
def generateGraphs(df1,df2,df3):
return dcc.Graph(
id='graph1',
figure={
'data': [
go.Pie(labels=df1['health'],values=df1['count_tree_id']),
],
'layout': {
'title': 'Count By Species'
}
}
),
dcc.Graph(
id='graph2',
figure={
'data': [
go.Bar(x=df2[df2['steward']==i]['health']
,y=df2[df2['steward']==i]['count_tree_id']
,name=i
,marker=go.bar.Marker(
color='rgb(26, 118, 255)'
))
for i in df3['steward'].unique()
]
}
)
第 3 部分:我认为这是问题所在。我是否正确使用了 @app.callback
函数?我是否正确定义了 retHealth()
并调用了 generateGraphs()
?
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('dropdown', 'value')])
def retHealth(value):
health=getHealth(value)
stew=getStew(value)
return generateGraphs(health,stew,trees)
你的回调没问题。
问题是您没有 return getHealth
和 getStew
中的任何内容。这意味着 None
是 return 隐式编辑的。
因此将这些函数更改为 return 用于绘制图形的 DataFames:
def getHealth(boro):
health_url = (
"https://data.cityofnewyork.us/resource/nwxe-4ae8.json?"
+ "$select=health,count(tree_id)"
+ "&$where=boroname='"
+ boro
+ "'"
+ "&$group=health"
).replace(" ", "%20")
return pd.read_json(health_url)
def getStew(boro):
stew_url = (
"https://data.cityofnewyork.us/resource/nwxe-4ae8.json?"
+ "$select=steward,health,count(tree_id)"
+ "&$where=boroname='"
+ boro
+ "'"
+ "&$group=steward,health"
).replace(" ", "%20")
return pd.read_json(stew_url)