如何 select 仅从字典中为图表标题添加标签(Plotly Dash)
How to select only label from a dict for graph title (Plotly Dash)
我有一本带有标签和键的字典,如下所示。
country = pd.read_csv('assets/countries.csv')
country = country.set_index('Code')
country_options = [{'label': f'({cod}) {country.loc[cod, "Country"]} ',
'value': f'{cod}'} for cod in country.index]
[{'label': '(AFG) 阿富汗', 'value': 'AFG'}, {'label': '(ALB) 阿尔巴尼亚', 'value': 'ALB'}, {'label': '(DZA) 阿尔及利亚'.....
我还有一个更新图表的回调,并想根据从下拉列表中选择的选项添加标题。
def update_graph(indi=indicator_options,coun=country_options):
df = indicator_select(coun, indi)
graph_content = dict(data=line_plot(df),
layout=go.Layout(
title=coun,
yaxis=dict(hoverformat='.1f'),
)
)
return graph_content
然而,title=coun 将 return 我的字典的 3 个字母符号(值),但我需要带有符号和名称的标签。是否可以?
下拉列表的 value
属性只会 return options
字典的 value
部分,而不是 label
部分。您可以做的是为您需要的值创建一个映射,如下所示:
country_labels = [{f'{cod}': f'({cod}) {country.loc[cod, "Country"]}'} for cod in country.index]
然后title=country_labels[coun],
我有一本带有标签和键的字典,如下所示。
country = pd.read_csv('assets/countries.csv')
country = country.set_index('Code')
country_options = [{'label': f'({cod}) {country.loc[cod, "Country"]} ',
'value': f'{cod}'} for cod in country.index]
[{'label': '(AFG) 阿富汗', 'value': 'AFG'}, {'label': '(ALB) 阿尔巴尼亚', 'value': 'ALB'}, {'label': '(DZA) 阿尔及利亚'.....
我还有一个更新图表的回调,并想根据从下拉列表中选择的选项添加标题。
def update_graph(indi=indicator_options,coun=country_options):
df = indicator_select(coun, indi)
graph_content = dict(data=line_plot(df),
layout=go.Layout(
title=coun,
yaxis=dict(hoverformat='.1f'),
)
)
return graph_content
然而,title=coun 将 return 我的字典的 3 个字母符号(值),但我需要带有符号和名称的标签。是否可以?
下拉列表的 value
属性只会 return options
字典的 value
部分,而不是 label
部分。您可以做的是为您需要的值创建一个映射,如下所示:
country_labels = [{f'{cod}': f'({cod}) {country.loc[cod, "Country"]}'} for cod in country.index]
然后title=country_labels[coun],