Kaszanas
Kaszanas

Reputation: 475

How to reach the key object in a defined dropdown menu in IPywidgets

Currently I am working on a pretty major data project. I use the database query to reach for all the important data. I am using one of the tables to create a dropdown menu which would allow me to further process and visualize. The widget itself is already created and working just fine. But there is a problem reaching the key.

I have tried to reach the 'widgets.Dropdown.value' with success, but I am having problem reaching aforementioned respective key that was selected in the dropdown menu.

The code is as follows:

widget_frame = query_all_players(session)
widget_frame = widget_frame.set_index('nick')['id'].to_dict()
dropdown_menu = widgets.Dropdown(options=widget_frame, description='Choose a player')

All of this results in a perfectly working widget:

(No 10 reputation no image I guess - Cheers)

The result I am aiming for is reaching the key in a string format. I'd like to re-use it in some calculation functions.

Upvotes: 0

Views: 983

Answers (1)

ac24
ac24

Reputation: 5565

I think you are looking for the widget.label variable:

    import ipywidgets as widgets
    w = widgets.Dropdown(
        options=[('One', 1), ('Two', 2), ('Three', 3)],
        value=2,
        description='Number:',
    )
    display(w)
    print(w.label, w.value)

enter image description here

Upvotes: 1

Related Questions