0

I have a function that looks like this that takes a dictionary as an argument. The dictionary looks like this:

d = {'skill_1':3, 'skill_2':2, 'skill_5':5, 'skill_7':3}

The function i have is a function that takes this dictionary, and then looks into a dataframe for the people that have a value greater or equal that the one in the dictionary, for the skills mentioned. It looks like this:


   skill_0  skill_1  skill_2  skill_3  skill_4  skill_5  skill_6  skill_7  
0        5        3        1        4        2        3        4        2   
1        3        2        1        3        2        2        5        1   
2        3        1        3        3        3        1        3        4   
3        3        5        4        5        5        5        3        3   
4        5        4        3        2        4        3        4        1   

I want to receive this argument from a widget(or using 2 widgets and then combining the values of the two widgets into a dictionary).

I have tried something like

s = ['skill_0', 'skill_1', 'skill_2', 'skill_3', 'skill_4', 'skill_5', 'skill_6', 'skill_7']
widgets.SelectMultiple(options=[f'{i}:{n}' for i in s for n in range(1,6)], value=('skill_3:1',))

But i cant manage to retrive the value from the widget and make a dictionary (i tried with eval too.). Also, the list like this looks visually ugly. Is there any way to achieve this?

Yolao_21
  • 765
  • 1
  • 4
  • 10

1 Answers1

2

Attempt to read the widget List from ipywidgets and specially the observe section. You can made a HBox (Horizontal Box) with a VBox (Vertical Box), in the vertical, create a Label and the Selector that you want, observe when selector change to automatic change values to dict with de function, like this:

import ipywidgets as widgets
from functools import partial
s = ['skill_0', 'skill_1', 'skill_2', 'skill_3', 'skill_4', 'skill_5', 'skill_6', 'skill_7']

values = {}
def update_value(label, widget):
    values[label.value] = widget.new
    print(values)

horizontal = []
for skill in range(7):
    title = widgets.Label(f"skill_{skill}")
    multiple = widgets.Select(options=[n for n in range(0, 5)])
    multiple.observe(partial(update_value, title), names=["value"], type="change")
    new_vertical = widgets.VBox([title, multiple])
    horizontal.append(new_vertical)

line = widgets.HBox(horizontal)
display(line)

A 4x6 matrix of numbers with dict of clicked values

Sergio Gao
  • 311
  • 3
  • 5
  • Thanks a lot fot the solution! Is this dictionary being stored somewhere? I have seen that i can access ```horizontal``` and i can find it there. Is there a quick way to retrive all the values and build a dictionary out of these inputs? – Yolao_21 Mar 31 '23 at 14:01
  • 1
    The dict is stored in "values" variable, you can use this var in any scope. – Sergio Gao Mar 31 '23 at 14:20
  • When i try to use ```line``` or HBox as part of widgets.interact, it throws an error though. What i do is: ```widgets.interact(func, desired_skills=line)```. Do you have any idea why? – Yolao_21 Mar 31 '23 at 14:28
  • I think it's because Box is not a interactive widget, the box don't change when you click, who change is the children: https://stackoverflow.com/questions/52980565/arranging-widgets-in-ipywidgets-interactive – Sergio Gao Mar 31 '23 at 15:10