2

I have a SelectMultiple IpyWidgets.

import ipywidgets as widgets
d = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears',"Mango"],
#rows=10,
description='Fruits',
disabled=False
)

OP:

print (list(d.value))
['Apples', 'Mango']

Irrespective of the order I select, the order in OP is always the same as in the order the list of options. For example, even if I select Mango first and then Apple the OP is still as given.

data_person
  • 4,194
  • 7
  • 40
  • 75

1 Answers1

0

You need a workaround to catch the click order, similar as described here:

import ipywidgets as widgets
d = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears',"Mango"],
description='Fruits',
disabled=False
)

foo = []

def on_change(change):
    if change['type'] == 'change' and change['name'] == 'value':
        for elem in change['new']:
            if elem not in foo:
                foo.append(elem)
        for elem in foo:
            if elem not in change['new']:
                foo.remove(elem)


d.observe(on_change)
d

foo is just a placeholder. Now if you click 'Mango' and then 'Apple', you get:

print('values:', d.value)  # values: ('Apples', 'Mango')
print('click_order:', foo) # click_order: ['Mango', 'Apples']
above_c_level
  • 3,579
  • 3
  • 22
  • 37