【问题标题】:Python Plotly Dash dropdown Adding a "select all" for scatterplotPython Plotly Dash 下拉列表为散点图添加“全选”
【发布时间】:2021-10-28 21:37:28
【问题描述】:

我想为我的下拉列表添加一个全选,并在应用程序打开时将其设为默认值,然后能够逐个删除胶囊,也可以取消选择全选按钮并选择一组胶囊.我设法找到了其他人使用全选选项的几种方法,但似乎都不适用于我的情况。谢谢!

数据看起来是这样的。

Dash 看起来像这样。

import pandas as pd
import plotly.express as px  # (version 4.7.0)
import plotly.graph_objects as go

import dash  # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

app = dash.Dash(__name__)
server = app.server

df = pd.read_csv("tcd vs rh.csv")
print(df)

capsuleID = df['Capsule_ID'].unique()
print(capsuleID)

capsuleID_names = list(capsuleID)
print(capsuleID_names)

capsuleID_names_1 = [{'label': k, 'value': k } for k in sorted(capsuleID)]
capsuleID_names_2 = [{'label': '(Select All)', 'value': 'All'}]
capsuleID_names_all = capsuleID_names_1 + capsuleID_names_2

app.layout = html.Div([

    html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),

    dcc.Dropdown(id="capsule_select",
                 options=capsuleID_names_all,
                 optionHeight=25,
                 multi=True,
                 searchable=True,
                 placeholder='Please select...',
                 clearable=True,
                 value=[''],
                 style={'width': "40%"}
                 ),


    html.Div([
        dcc.Graph(id="the_graph")
    ]),

])


# -----------------------------------------------------------
@app.callback(
    Output('the_graph', 'figure'),
    [Input('capsule_select', 'value')]
)
def update_graph(capsule_chosen):
    if capsule_chosen == 'all_values':
        dff = df['Capsule_ID']
    else:
        dff = df[df['Capsule_ID'].isin(capsule_chosen)]  # filter all rows where capsule ID is the capsule ID selected

    scatterplot = px.scatter(
        data_frame=dff,
        x="tcd",
        y="humidity",
        )

    scatterplot.update_traces(textposition='top center')

    return scatterplot


# ------------------------------------------------------------------------------

if __name__ == '__main__':
    app.run_server(debug=True)



【问题讨论】:

    标签: python plotly plotly-dash dashboard plotly-python


    【解决方案1】:

    如果默认情况下您想从df['Capsule_ID'] 中选择所有内容,您只需将其传递给下拉列表的value

    然后,您可以将回调更改为类似“全选”功能的回调:

    @app.callback(
        Output("the_graph", "figure"),
        Output("capsule_select", "value"),
        Input("capsule_select", "value"),
    )
    def update_graph(capsules_chosen):
        dropdown_values = capsules_chosen
    
        if "All" in capsules_chosen:
            dropdown_values = df["Capsule_ID"]
            dff = df
        else:
            dff = df[df["Capsule_ID"].isin(capsules_chosen)]
    
        scatterplot = px.scatter(
            data_frame=dff,
            x="tcd",
            y="humidity",
        )
    
        scatterplot.update_traces(textposition="top center")
    
        return scatterplot, dropdown_values 
    

    除了您的检查不适用于默认下拉列表value,您还这样做了:

    dff = df['Capsule_ID']
    

    这意味着您将dff 设置为单个列。这不是我们想要的,因为 'tcd''humidity' 列在 df['Capsule_ID'] 上不存在。

    【讨论】:

    • 谢谢巴斯!对于 dropdown_values = df['Capsule_ID'],将其更改为 dropdown_values = capsuleID_names 有助于解决选择所有重复的 df capsule_ID(几乎 61k 观察)的问题,因为 capsuleID_names 是唯一列表(140+ 观察)。
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2019-01-23
    • 2018-11-11
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多