Python Dash - 允许用户选择他们想要在图表上看到的变量

发布于 2025-01-13 05:35:49 字数 1794 浏览 3 评论 0原文

有没有办法让用户选择他们想要在图表上显示的内容?

类似的东西:

在此处输入图像描述

因此,图表最初加载为 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:

enter image description here

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:

enter image description here

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2025-01-20 05:35:49

单选按钮是处理此类情况的良好用户控件,因为该值必须是选项之一。回调中的 if/else 逻辑可以为了优雅而重构,但我以这种方式保留它,以便在如何使用 RadioItems 'value' 属性方面尽可能明确。

dcc.RadioItems(
    id='chart-dataset-selector',
    options=[
        {'label': 'Annual Rent Per Country', 'value': 'annual-rent'},
        {'label': 'Rentable Area Per Country', 'value': 'rentable-area'}
    ],
    value='annual-rent',
)

# Pie Chart - Country with Annual Rent
@callback(
    Output('pie_chart1', 'figure'),
    Input('submit_button', 'n_clicks'),
    Input('chart-dataset-selector', 'value'),
    State('country_checklist', 'value'),
    State('radios_currency', 'value')
)
def update_pie_chart_1(n_clicks, selected_country, selected_currency, selected_dataset):
    if not selected_country and n_clicks is None:
        return dash.no_update
    elif selected_currency == '
:
        if selected_dataset == 'annual-rent':
            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)
        else:
            assert selected_dataset == 'rentable-area'
            df_pie = your_code_here_to_get_data()
            fig = px.pie(df_pie, values='Rentable Area', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
    else:
        if selected_dataset == 'annual-rent':
            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)
        else:
            assert selected_dataset == 'rentable-area'
            df_pie = your_code_here_to_get_data()
            fig = px.pie(df_pie, values='Rentable Area_EUR', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
    return fig

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.

dcc.RadioItems(
    id='chart-dataset-selector',
    options=[
        {'label': 'Annual Rent Per Country', 'value': 'annual-rent'},
        {'label': 'Rentable Area Per Country', 'value': 'rentable-area'}
    ],
    value='annual-rent',
)

# Pie Chart - Country with Annual Rent
@callback(
    Output('pie_chart1', 'figure'),
    Input('submit_button', 'n_clicks'),
    Input('chart-dataset-selector', 'value'),
    State('country_checklist', 'value'),
    State('radios_currency', 'value')
)
def update_pie_chart_1(n_clicks, selected_country, selected_currency, selected_dataset):
    if not selected_country and n_clicks is None:
        return dash.no_update
    elif selected_currency == '
:
        if selected_dataset == 'annual-rent':
            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)
        else:
            assert selected_dataset == 'rentable-area'
            df_pie = your_code_here_to_get_data()
            fig = px.pie(df_pie, values='Rentable Area', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
    else:
        if selected_dataset == 'annual-rent':
            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)
        else:
            assert selected_dataset == 'rentable-area'
            df_pie = your_code_here_to_get_data()
            fig = px.pie(df_pie, values='Rentable Area_EUR', names='Country', color_discrete_sequence=px.colors.sequential.Aggrnyl)
    return fig
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文