Python Dash - 允许用户选择他们想要在图表上看到的变量
有没有办法让用户选择他们想要在图表上显示的内容?
类似的东西:
因此,图表最初加载为 1,然后如果用户单击 2,图表将变为按国家/地区列出的可出租面积,而不是按国家/地区列出的年租金。
有人可以指出我如何通过 dash 实现这一目标的正确方向吗?我尝试使用按钮,但它不起作用,因为在回调中我已经有一个提交按钮来生成此图表。
下面的简化代码:
dbc.Button('Submit', id='submit_button')
dbc.Col([
dbc.Card([
html.Button(className='button btn btn-sm',
id='pie1_change',
n_clicks=0,
children='Rentable Area'),
dcc.Graph(id='pie_chart1', style={'margin-top': '20px'}, figure=blank_fig())], style={'margin-top': '30px'}),
], width=4)
# Pie Chart - Country with Annual Rent
@callback(
Output('pie_chart1', 'figure'),
[Input('submit_button', 'n_clicks')],
[State('country_checklist', 'value'),
State('radios_currency', 'value')]
)
def update_pie_chart_1(n_clicks, selected_country, selected_currency):
if not selected_country and n_clicks is None:
return dash.no_update
elif selected_currency == '$':
df_pie = df[(df['Country'].isin(selected_country))]
fig = px.pie(df_pie, values='Annual Rent', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
return fig
else:
df_pie = df[(df['Country'].isin(selected_country))]
fig = px.pie(df_pie, values='Annual Rent_EUR', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
return fig
现在生成以下输出:
这样,当用户单击“可出租区域”时,我如何添加回调上下文,图表将更新(下面突出显示的更改):
Is there a way to allow users to choose what they want to be displayed on the graph?
Something similar to this:
So the graph initially loads with 1 and then if the user clicks 2 for example, the chart will become Rentable Area by Country rather than Annual Rent by Country.
Can someone please point me to the right direction of how I can achieve that with dash ? I tried using a button but it does not work as in the callback I already have a submit button to generate this graph.
Simplified code below:
dbc.Button('Submit', id='submit_button')
dbc.Col([
dbc.Card([
html.Button(className='button btn btn-sm',
id='pie1_change',
n_clicks=0,
children='Rentable Area'),
dcc.Graph(id='pie_chart1', style={'margin-top': '20px'}, figure=blank_fig())], style={'margin-top': '30px'}),
], width=4)
# Pie Chart - Country with Annual Rent
@callback(
Output('pie_chart1', 'figure'),
[Input('submit_button', 'n_clicks')],
[State('country_checklist', 'value'),
State('radios_currency', 'value')]
)
def update_pie_chart_1(n_clicks, selected_country, selected_currency):
if not selected_country and n_clicks is None:
return dash.no_update
elif selected_currency == '
This now generates this output:
With that how can I add the callback context to now when the user clicks ‘Rentable Area’ the chart will update (change highlighted below):
:
df_pie = df[(df['Country'].isin(selected_country))]
fig = px.pie(df_pie, values='Annual Rent', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
return fig
else:
df_pie = df[(df['Country'].isin(selected_country))]
fig = px.pie(df_pie, values='Annual Rent_EUR', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
return fig
This now generates this output:
With that how can I add the callback context to now when the user clicks ‘Rentable Area’ the chart will update (change highlighted below):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单选按钮是处理此类情况的良好用户控件,因为该值必须是选项之一。回调中的 if/else 逻辑可以为了优雅而重构,但我以这种方式保留它,以便在如何使用 RadioItems 'value' 属性方面尽可能明确。
Radio buttons are a good user control to handle this type of situation since the value must be one of the options. The if/else logic in the callback could be refactored for elegance but I left it in this way to be as explicit as possible in how to use the RadioItems 'value' property.