Python 将破折号下拉选择传递给变量
我在获取参数以继承仪表板中的用户下拉选择时遇到问题 下面是我代码的三部分。我认为问题不在布局/图形部分,因为如果我删除下拉对象并显式地分配Python 将破折号下拉选择传递给变量,python,api,flask,plotly-dash,Python,Api,Flask,Plotly Dash,我在获取参数以继承仪表板中的用户下拉选择时遇到问题 下面是我代码的三部分。我认为问题不在布局/图形部分,因为如果我删除下拉对象并显式地分配boro变量(即取消注释第1部分中的第一行),我就能够得到要填充的图形。此外,如果我在没有图形的情况下进行测试,并且只是尝试打印变量值,那么在使用下拉列表时,它仍然显示为空 有人能提供一些帮助吗?我正在学习Plotly Dash教程()中给出的示例,但我不确定是否正确翻译了它们 第1部分:从API中提取数据 #boro = 'Bronx' def getHea
boro
变量(即取消注释第1部分中的第一行),我就能够得到要填充的图形。此外,如果我在没有图形的情况下进行测试,并且只是尝试打印变量值,那么在使用下拉列表时,它仍然显示为空
有人能提供一些帮助吗?我正在学习Plotly Dash教程()中给出的示例,但我不确定是否正确翻译了它们
第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()
]
}
)
第三部分:我认为这就是问题所在。我是否正确使用了@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)
我无法重现这个错误。唯一不同的是,我没有在您的
选项部分使用列表理解,因为我没有这些数据。您是否能够在打印过程中打印值并查看失败的地方?@Carlos要清楚,我没有收到任何错误,只是没有收到任何输出(“树概览”标题下的空白页)。我尝试使用print(boro)
进行调试,但它没有返回任何结果,这使我相信下拉列表没有填充该参数。不确定是否可能,但您能pastebin
一个更完整的代码吗?我测试了此代码(删除了无法解析的项),但行为不存在。我无法重现此错误。唯一不同的是,我没有在您的选项部分使用列表理解,因为我没有这些数据。您是否能够在打印过程中打印值并查看失败的地方?@Carlos要清楚,我没有收到任何错误,只是没有收到任何输出(“树概览”标题下的空白页)。我尝试使用print(boro)
进行调试,但它没有返回任何结果,这使我相信下拉列表没有填充该参数。不确定是否可能,但您能pastebin
一个更完整的代码吗?我测试了这段代码(删除了无法解析的项),但行为并不存在。