Issue
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?
Solution
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)
Answered By - Sergio Gao
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.