pythonpandaswidgetipywidgets

How to create dynamically updated SelectMultiple widget in Python?


I am trying to create a bundle of SelectMultiple widgets that will work as a filter for Pandas df.

Below is used as a sample data. The idea is that it's a distinct combination of unique values available in all columns.

import pandas as pd
data = {
    'category1': ['A', 'A', 'B', 'B', 'C', 'B'],
    'category2': ['X', 'Y', 'X', 'Z', 'Y', 'K'],
    'country': ['USA', 'USA', 'UK', 'UK', 'Canada', 'Germany'],
    'department': ['Dept1', 'Dept2', 'Dept1', 'Dept2', 'Dept1', 'Dept4'],
    'category3': ['Cat1', 'Cat2', 'Cat1', 'Cat2', 'Cat1', 'Cat2'],
    'gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
    'brand': ['Brand1', 'Brand2', 'Brand3', 'Brand4', 'Brand5', 'Brand2']
}
df = pd.DataFrame(data)
category1 category2 country department category3 gender brand
0 A X USA Dept1 Cat1 Male Brand1
1 A Y USA Dept2 Cat2 Female Brand2
2 B X UK Dept1 Cat1 Male Brand3
3 B Z UK Dept2 Cat2 Female Brand4
4 C Y Canada Dept1 Cat1 Male Brand5
5 B K Germany Dept4 Cat2 Male Brand2

I want to have one-way dependencies (from left to right), meaning I always select category1 first. This choice limits all other options. Then I select category2 and so on. At each step choices down the way are updated.

This is what I have closest to the solution:

import ipywidgets as widgets
from IPython.display import display

def update_options(category1, category2, country, department, category3, gender):
    filtered_df = df.copy()
    if category1:
        filtered_df = filtered_df[filtered_df['category1'].isin(category1)]
    if category2:
        filtered_df = filtered_df[filtered_df['category2'].isin(category2)]
    if country:
        filtered_df = filtered_df[filtered_df['country'].isin(country)]
    if department:
        filtered_df = filtered_df[filtered_df['department'].isin(department)]
    if category3:
        filtered_df = filtered_df[filtered_df['category3'].isin(category3)]
    if gender:
        filtered_df = filtered_df[filtered_df['gender'].isin(gender)]
   
    category2_options = filtered_df['category2'].unique().tolist()
    country_options = filtered_df['country'].unique().tolist()
    department_options = filtered_df['department'].unique().tolist()
    category3_options = filtered_df['category3'].unique().tolist()
    gender_options = filtered_df['gender'].unique().tolist()
    brand_options = filtered_df['brand'].unique().tolist()
   
    # Update options for dropdowns
    category2_dropdown.options = category2_options
    country_dropdown.options = country_options
    department_dropdown.options = department_options
    category3_dropdown.options = category3_options
    gender_dropdown.options = gender_options
    brand_dropdown.options = brand_options

# Define initial options for category1 and category2
category1_options = df['category1'].unique().tolist()
category2_options = df['category2'].unique().tolist()

# Create dropdown widgets
category1_dropdown = widgets.SelectMultiple(options=category1_options, description='Category 1:')
category2_dropdown = widgets.SelectMultiple(options=category2_options, description='Category 2:')
country_dropdown = widgets.SelectMultiple(description='Country:')
department_dropdown = widgets.SelectMultiple(description='Department:')
category3_dropdown = widgets.SelectMultiple(description='Category 3:')
gender_dropdown = widgets.SelectMultiple(description='Gender:')
brand_dropdown = widgets.SelectMultiple(description='Brand:')

# Use interact to dynamically update options
widgets.interact(update_options,
                 category1=category1_dropdown,
                 category2=category2_dropdown,
                 country=country_dropdown,
                 department=department_dropdown,
                 category3=category3_dropdown,
                 gender=gender_dropdown)

# Display widgets
display(brand_dropdown) # the others are displayed with widgets.interact() above

It works only partially: choices for subsequent columns are updated but if there is more than one, I cannot choose only one, only all of them. For example, if I choose category1=B then on widget for category2 I see correctly options X Z K but clicking on just one doesn't work (it doesnt' get selected), only draging through them all works.

Anyone has an idea what I am missing?

Many thanks!


Solution

  • I managed to get it right. The issue was that widgets get confused if it's all in one function (a sort of a loop reference). They need to be distributed downwards.

    import pandas as pd
    import ipywidgets as widgets
    from IPython.display import display
    
    # Sample DataFrame
    data = {
        'category1': ['A', 'A', 'B', 'B', 'C'],
        'category2': ['X', 'Y', 'X', 'Z', 'Y'],
        'country': ['USA', 'USA', 'UK', 'UK', 'Canada'],
        'department': ['Dept1', 'Dept2', 'Dept1', 'Dept2', 'Dept1'],
        'category3': ['Cat1', 'Cat2', 'Cat1', 'Cat2', 'Cat1'],
        'gender': ['Male', 'Female', 'Male', 'Female', 'Male'],
        'brand': ['Brand1', 'Brand2', 'Brand3', 'Brand4', 'Brand5']
    }
    
    df = pd.DataFrame(data)
    
    # Function to update options of category2_dropdown based on category1 selection
    def update_category2_options(change):
        if change.new:        
            filtered_df = df[df['category1'].isin(change.new)]
            options = filtered_df['category2'].unique()
            category2_dropdown.options = options
        else:
            category2_dropdown.options = []
    
    # Function to update options of country_dropdown based on category1 and category2 selections
    def update_country_options(change):
        if change.new:
            filtered_df = df[df['category1'].isin(category1_dropdown.value)]
            if category2_dropdown.value:
                filtered_df = filtered_df[df['category2'].isin(category2_dropdown.value)]
            options = filtered_df['country'].unique()
            country_dropdown.options = options
        else:
            country_dropdown.options = []
    
    # Function to update options of department_dropdown based on category1, category2, and country selections
    def update_department_options(change):
        if change.new:
            filtered_df = df[df['category1'].isin(category1_dropdown.value)]
            if category2_dropdown.value:
                filtered_df = filtered_df[df['category2'].isin(category2_dropdown.value)]
            if country_dropdown.value:
                filtered_df = filtered_df[df['country'].isin(country_dropdown.value)]
            options = filtered_df['department'].unique()
            department_dropdown.options = options
        else:
            department_dropdown.options = []
    
    # Function to update options of category3_dropdown based on category1, category2, country, and department selections
    def update_category3_options(change):
        if change.new:
            filtered_df = df[df['category1'].isin(category1_dropdown.value)]
            if category2_dropdown.value:
                filtered_df = filtered_df[df['category2'].isin(category2_dropdown.value)]
            if country_dropdown.value:
                filtered_df = filtered_df[df['country'].isin(country_dropdown.value)]
            if department_dropdown.value:
                filtered_df = filtered_df[df['department'].isin(department_dropdown.value)]
            options =  filtered_df['category3'].unique()
            category3_dropdown.options = options
        else:
            category3_dropdown.options = []
    
    # Function to update options of gender_dropdown based on category1, category2, country, department, and category3 selections
    def update_gender_options(change):
        if change.new:
            filtered_df = df[df['category1'].isin(category1_dropdown.value)]
            if category2_dropdown.value:
                filtered_df = filtered_df[df['category2'].isin(category2_dropdown.value)]
            if country_dropdown.value:
                filtered_df = filtered_df[df['country'].isin(country_dropdown.value)]
            if department_dropdown.value:
                filtered_df = filtered_df[df['department'].isin(department_dropdown.value)]
            if category3_dropdown.value:
                filtered_df = filtered_df[df['category3'].isin(category3_dropdown.value)]
            options = filtered_df['gender'].unique()
            gender_dropdown.options = options
        else:
            gender_dropdown.options = []
    
    def update_brand_options(change):
        if change.new:
            filtered_df = df[df['category1'].isin(category1_dropdown.value)]
            if category2_dropdown.value:
                filtered_df = filtered_df[df['category2'].isin(category2_dropdown.value)]
            if country_dropdown.value:
                filtered_df = filtered_df[df['country'].isin(country_dropdown.value)]
            if department_dropdown.value:
                filtered_df = filtered_df[df['department'].isin(department_dropdown.value)]
            if category3_dropdown.value:
                filtered_df = filtered_df[df['category3'].isin(category3_dropdown.value)]
            if gender_dropdown.value:
                filtered_df = filtered_df[df['gender'].isin(gender_dropdown.value)]
            options = filtered_df['brand'].unique()
            brand_dropdown.options = options
        else:
            brand_dropdown.options = []
    
    # Create dropdown widgets
    category1_dropdown = widgets.SelectMultiple(description='Category 1:', options=df['category1'].unique())
    category2_dropdown = widgets.SelectMultiple(description='Category 2:', options=[])
    country_dropdown = widgets.SelectMultiple(description='Country:', options=[])
    department_dropdown = widgets.SelectMultiple(description='Department:', options=[])
    category3_dropdown = widgets.SelectMultiple(description='Category 3:', options=[])
    gender_dropdown = widgets.SelectMultiple(description='Gender:', options=[])
    brand_dropdown = widgets.SelectMultiple(description='Brand:', options=[])
    
    # Use observe to dynamically update options
    category1_dropdown.observe(update_category2_options, names='value')
    category1_dropdown.observe(update_country_options, names='value')
    category1_dropdown.observe(update_department_options, names='value')
    category1_dropdown.observe(update_category3_options, names='value')
    category1_dropdown.observe(update_gender_options, names='value')
    category1_dropdown.observe(update_brand_options, names='value')
    
    category2_dropdown.observe(update_country_options, names='value')
    category2_dropdown.observe(update_department_options, names='value')
    category2_dropdown.observe(update_category3_options, names='value')
    category2_dropdown.observe(update_gender_options, names='value')
    category2_dropdown.observe(update_brand_options, names='value')
    
    country_dropdown.observe(update_department_options, names='value')
    country_dropdown.observe(update_category3_options, names='value')
    country_dropdown.observe(update_gender_options, names='value')
    country_dropdown.observe(update_brand_options, names='value')
    
    department_dropdown.observe(update_category3_options, names='value')
    department_dropdown.observe(update_gender_options, names='value')
    department_dropdown.observe(update_brand_options, names='value')
    
    category3_dropdown.observe(update_gender_options, names='value')
    category3_dropdown.observe(update_brand_options, names='value')
    
    gender_dropdown.observe(update_brand_options, names='value')
    
    # Display widgets
    display(category1_dropdown, category2_dropdown, country_dropdown, department_dropdown, category3_dropdown, gender_dropdown, brand_dropdown)