Anwaya Rath
Anwaya Rath

Reputation: 65

Enabling Multi-Select in ipython-widgets

I have a development problem. I need to be able to select multiple items at the same time from a drop-down menu in python using ipython-widgets. So far, I have been able to choose an option a single-option widget menu, which upon selection will plot its corresponding statistics. I have pasted my code below, and I would greatly appreciate it if you could help me out.

import numpy as np
import pandas as pd
import ipywidgets as widgets
import matplotlib.pyplot as plt
import panel as pn
pn.extension()


classes= widgets.Dropdown(
description='Products:',
options= list(output_table.class_m.unique())
)


start_year = widgets.BoundedFloatText(
    value=output_table.year.min(),
    min=output_table.year.min(),
    max=output_table.year.max(),
    step=1,
    description='Start Year:',
    disabled=False,
    color='black'
)
end_year = widgets.BoundedFloatText(
    value=output_table.year.max(),
    min=output_table.year.min(),
    max=output_table.year.max(),
    step=1,
    description='End Year:',
    disabled=False,
    color='black'
)


output=widgets.Output()

def response(name, start, end):
    name = classes.value
    output.clear_output()
    df2 = output_table.copy()
   # Filter between min and max years (inclusive)
    df2 = df2[(df2.year >= start) & (df2.year <= end)]
    with output:
        if len(df2) > 0:
            plot1 = df2[df2['class_m'] == name].groupby(['month']).agg({'units':'sum'}).reset_index()
            plot1.plot(x='month', y='units')
            clear_output(wait=True)
            plt.show()
        else:
            print("No data to show for current selection")
def event_handler(change):
    response(change.new, start_year.value, end_year.value)

def start_year_handler(change):
    response(classes.value, change.new, end_year.value)

def end_year_handler(change):
    response(classes.value, start_year.value, change.new)

classes.observe(event_handler, names='value')
start_year.observe(start_year_handler, names = 'value')
end_year.observe(end_year_handler, names = 'value')

display(classes)
display(start_year)
display(end_year)

Upvotes: 5

Views: 21280

Answers (2)

Shovalt
Shovalt

Reputation: 6766

I also recommend the new TagsInput widget - it can serve as a more modern multi-select widget, allowing you to add a selection by starting to type text, and remove a selection by clicking the X on the tag. You can of course "observe" its value as with other widgets.

Example:

tags = widgets.TagsInput(
    value=['pizza', 'fries'],
    allowed_tags=['pizza', 'fries', 'tomatoes', 'steak'],
    allow_duplicates=False
)
tags

Result: enter image description here

Upvotes: 1

ac24
ac24

Reputation: 5565

Have a look at the SelectMultiple widget.

w = widgets.SelectMultiple(
    options=['Apples', 'Oranges', 'Pears'],
    value=['Oranges'],
    #rows=10,
    description='Fruits',
    disabled=False
)

You can shift-click to choose multiple selections, then call w.value to get the list of selected values.

Upvotes: 9

Related Questions