回调函数display_popup
从mapbox graph
的customdata
参数迭代json字符串传递并读取值。当我在jupyter notebook中没有函数的情况下运行代码时,它不会抛出错误
@app.callback([Output("modal","is_open"),
Output("name","children"),
Output("address","children")],
[Input("map-graph1","clickData")]
)
def display_popup(clickData):
res = json.dumps(clickData, indent=2)
Tenant = res['points'][0]['customdata']['Tenant']
Address = res['points'][0]['customdata']['Address']
print(Tenant, Address)
clickData = {
"points": [
{
"curveNumber": 64,
"pointNumber": 0,
"pointIndex": 0,
"lon": -112.07451978751655,
"lat": 33.459071231389125,
"customdata": {
"Tenant": "HeartFlow",
"Industry": "Advertising",
"Space": 65195,
"Property": "Retail",
"Bldg. class": "A",
"Lease Type": "Flex",
"Rent": 36.28,
"Address": "100 W Roosevelt St, Phoenix, AZ 85003, USA"
}
}
]
}
或者,我也尝试了使用键、值对的字典
for x in res['points']:
cd = x.get('customdata')
name = cd.get('Tenant')
address = cd.get('Address')
仍然抛出一个TypeError: string indices must be integers
下面是修复它的代码:
相关问题 更多 >
编程相关推荐