0

I currently have the following code:

from ipywidgets import Checkbox, Dropdown, interact, widgets

def locate_customer_name(list_of_values: list) -> list:
    db_dropdown = widgets.Dropdown(options=['Blue', 'Green', 'Yellow], description='Color:', value=None)
    unknown_checkbox = widgets.Checkbox(
        value=False, description='Unable to locate Color In list', disabled=False, indent=False
    )

    @interact(db_dropdown_values=db_dropdown, lost_checkbox=unknown_checkbox)
    def _core_func(db_dropdown_values, lost_checkbox):
        dropdown_selection = db_dropdown.value
        checkbox = unknown_checkbox.value
        button = widgets.Button(description='Confirm')
        out = widgets.Output()
        def _on_button_clicked(dropdown_select, checkbox):
            with out:
                print(dropdown_select, checkbox)
        display(button, out)
        button.on_click(_on_button_clicked(dropdown_selection, checkbox))

What I am looking to do is return the values selected in the _on_button_click function (e.g. dropdown_select and checkbox). For now, it seems like I am only able to print those variables out. How would I return them, so I can use them as inputs to another function?

1 Answer 1

1

I don't know how would you return the values from an event like the click of a button. What I usually do is to create a state class to store the values and use in the following function, something like:

from functools import partial
from dataclasses import dataclass
from ipywidgets import Checkbox, Dropdown, interact, widgets

@dataclass
class State:
    color: str = None
    located: bool = False
        
state = State()

def locate_customer_name(list_of_values: list) -> list:
    db_dropdown = widgets.Dropdown(options=['Blue', 'Green', 'Yellow'], description='Color:', value=None)
    unknown_checkbox = widgets.Checkbox(
        value=False, description='Unable to locate Color In list', disabled=False, indent=False
    )

    @interact(db_dropdown_values=db_dropdown, lost_checkbox=unknown_checkbox)
    def _core_func(db_dropdown_values, lost_checkbox):
        dropdown_selection = db_dropdown.value
        checkbox = unknown_checkbox.value
        button = widgets.Button(description='Confirm')

        def _on_button_clicked(dropdown_select, checkbox, event):
            state.color = dropdown_select
            state.located = checkbox
        
        callback = partial(
            _on_button_clicked,
            dropdown_selection, 
            checkbox
        )
        
        button.on_click(callback)
        
        display(button)
        
locate_customer_name(['Briana', 'Mr White'])

PS: I'm using functools to add the arguments to the on_click callback. Check this issue if you want more information about it.

Sign up to request clarification or add additional context in comments.

2 Comments

Running your above code, what do I need to call to access the values selected in the dropdown?
You will be able to get the data by calling the state property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.