Partial chained callbacks using plotly dash

I’m trying to filter data using multiple dropdown bars within a plotly dashboard. There are 5 dropdown options in total. I want the first 3 to operate indepently, while the last two should be chained, both ways, to the first 3.

Specifically, the features that I’m aiming to implement are:

  1. A default of all values should always be the initial starting point

  2. The first 3 options (Year, Season and Month) should act independently. As in, any combination of these 3 can be added to the output. If one item is selected, the output should be updated with those values. However, if an item is selected from another dropdown, those values should be added to the output. Example below in i).

  3. Option 4-5 (temp and prec) should be chained, both ways, to the first three dropdown options (Year, Season and Month). This should be reversible or both ways too. If one of the first 3 dropdown options is selected, the table output should be updated with those values and the dropdown lists should be reduced to only allow the user to pick from those values. Example below in ii).

To provide concrete examples;

i) 2012 is selected from Year in the first dropdown option. The table output displays the relevant values. The user should be able to select any subsequent values in the Year dropdown list (functional). However, if the user wants to also see Spr values from the second dropdown option, that data should be added to the output.

ii) For the 4-5 dropdown options which should be chained to first 3, if Hot and Mild are selected in temp and Wet is selected in prec, then the dropdown lists in the first three options should be reduced to: Year = 2013, 2015; Season = Spr, Fall; Month = Apr, Jun, Oct, Dec.

import pandas as pd
from dash import Dash, dcc, html, Input, Output, dash_table
import dash_bootstrap_components as dbc
from itertools import cycle
import random

Year = cycle(['2012','2013','2014','2015'])
Season = cycle(['Win','Spr','Sum','Fall'])
Month = cycle(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'])
temp_group = cycle(['Hot','Cold','Mild'])
prec_group = cycle(['Dry','Wet'])

df = pd.DataFrame(index = range(20))

df['option1'] = [next(Year) for count in range(df.shape[0])]
df['option2'] = [next(Season) for count in range(df.shape[0])]
df['option3'] = [next(Month) for count in range(df.shape[0])]
df['option4'] = [next(temp_group) for count in range(df.shape[0])]
df['option5'] = [next(prec_group) for count in range(df.shape[0])]

option1_list = sorted(df['option1'].unique().tolist())
option2_list = df['option2'].unique().tolist()
option3_list = df['option3'].unique().tolist()
option4_list = sorted(df['option4'].unique().tolist())
option5_list = sorted(df['option5'].unique().tolist())

app = Dash(__name__)

app.layout = html.Div([
    dbc.Card(
        dbc.CardBody([
            dbc.Row([
                dbc.Col([
                    html.P("Option 1"),
                        html.Div([
                                dcc.Dropdown(id='option1_dropdown',
                                                 options=option1_list,
                                                 value=[],
                                                 placeholder='All',
                                                 multi=True,
                                                 clearable=True),
                                ],
                                style={'width': '100%', 'display': 'inline-block'})
                ]),
                dbc.Col([
                    html.P("Option 2"),
                        html.Div([
                                dcc.Dropdown(id='option2_dropdown',
                                                 options=option2_list,
                                                 value=[],
                                                 placeholder='All',
                                                 multi=True,
                                                 clearable=True),
                                ],
                                style={'width': '100%', 'display': 'inline-block'})
                ]),
                dbc.Col([
                    html.P("Option 3"),
                        html.Div([
                                dcc.Dropdown(id='option3_dropdown',
                                                 options=option3_list,
                                                 value=[],
                                                 placeholder='All',
                                                 multi=True,
                                                 clearable=True),
                                ],
                                style={'width': '100%', 'display': 'inline-block'})
                ]),
                dbc.Col([
                    html.P("Option 4"),
                        html.Div([
                                dcc.Dropdown(id='option4_dropdown',
                                                 options=option4_list,
                                                 value=[],
                                                 placeholder='All',
                                                 multi=True,
                                                 clearable=True),
                                ],
                                style={'width': '100%', 'display': 'inline-block'})
                ]),
                dbc.Col([
                    html.P("Option 5"),
                        html.Div([
                                dcc.Dropdown(id='option5_dropdown',
                                                 options=option5_list,
                                                 value=[],
                                                 placeholder='All',
                                                 multi=True,
                                                 clearable=True),
                                ],
                                style={'width': '100%', 'display': 'inline-block'})
                ]),
            ], align='center'),
        ]), color='dark'
    ),
    dbc.Card(
        dbc.CardBody([
            dbc.Row([
                    html.Div([
                        html.Div(id='dd-output-container')
                    ])
            ], align='center'),
        ]), color='dark'
    ),
    dbc.Card(
        dbc.CardBody([
            dbc.Row([
                    html.Div([
                        dash_table.DataTable(
                            id='table_container',
                            data=df.to_dict('records')
                        )
                    ])
            ], align='center'),
        ]), color='dark'
    )
])

@app.callback(
    Output('table_container', 'data'),
    [Input('option1_dropdown', 'value'),
     Input('option2_dropdown', 'value'),
     Input('option3_dropdown', 'value'),
     Input('option4_dropdown', 'value'),
     Input('option5_dropdown', 'value')
     ])
 
def set_dropdown_options(value1, value2, value3, value4, value5):
    if not value1 or value1 == 'All':
        value1 = option1_list
    if not value2 or value2 == 'All':
        value2 = option2_list
    if not value3 or value3 == 'All':
        value3 = option3_list
    if not value4 or value4 == 'All':
        value4 = option4_list
    if not value5 or value5 == 'All':
        value5 = option5_list
    
    ddf = df.query('option1 == @value1 and '
                   'option2 == @value2 and '
                   'option3 == @value3 and '
                   'option4 == @value4 and '
                   'option5 == @value5',
                   engine='python')
    
    return ddf.to_dict('records')

# ====== Using this as a way to view the selections
@app.callback(
    Output('dd-output-container', 'children'),
    [Input('option1_dropdown', 'value'),
     Input('option2_dropdown', 'value'),
     Input('option3_dropdown', 'value'),
     Input('option4_dropdown', 'value'),
     Input('option5_dropdown', 'value')
     ])

def selection(value1, value2, value3, value4, value5):
    #  If value lists are empty or equal to the default of 'All', use the initial df values
    if not value1 or value1 == 'All':
        value1 = option1_list
    if not value2 or value2 == 'All':
        value2 = option2_list
    if not value3 or value3 == 'All':
        value3 = option3_list
    if not value4 or value4 == 'All':
        value4 = option4_list
    if not value5 or value5 == 'All':
        value5 = option5_list
    
    ddf = df.query('option1 == @value1 and '
                   'option2 == @value2 and '
                   'option3 == @value3 and '
                   'option4 == @value4 and '
                   'option5 == @value5',
                   engine='python')    

    return 

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật