Search code examples
pythonjupyter-notebookipywidgets

Jupyter Notebook - ipywidgets - Automatic feedback from selected


I am trying to create a function in a Jupyter notebook to:

  1. Display with an ipywidget a series of radio buttons from a list of options.
  2. Automatically display the selected value.
  3. Save the selected value in a variable.

So far, I managed step 1 (widget named radio_buttons in the MWE below) and to create a second ipythonwidget to display the result (widget named display_result, not yet displaying said result). I understand I need to bind the selected result to the value of the display_result(attempted with the function bind_selected_to_output) but I do not understand how.

MWE

from ipywidgets import interact, widgets
from IPython.display import display

options=["A","B","C"]

# Tentative function to get the input value into the output widget
def bind_selected_to_output(display): 
    display.value=#The selected value of radio buttons

# The function I am trying to create
def display_radio_buttons(options):
    # Input widget
    radio_buttons=widgets.RadioButtons(
        options=options,
        value=None,
        disabled=False

    # Output widget
    display_result=widgets.Text(description = "Saved as:",
                                   disabled=True)

    # Trying to monitor selection on the input widget
    radio_buttons.observe(bind_selected_to_output(display_result))

    # Show the widgets
    display(radio_buttons)
    display(display_result)

    # Return selected value for use elsewhere
    return display_result.value

# Use function
display_radio_buttons(options)
    )

How can I make this function work?


Solution

  • I found a much more practical solution to my problem. It creates a function display_radio_buttons which generates radio buttons from the parameter options (a list of the choices to display). The selected option is displayed in a Text() widget whose value is linked to the value of the radio buttons. The selected option is also returned for later use (e.g., printing it).

    This function can be easily reused, as all elements are defined within the function.

    from ipywidgets import widgets, link
    from IPython.display import display
    
    options=["A","B","C"]
    
    def display_radio_buttons(options):
        button = widgets.RadioButtons(options=options,description='Radio Selector:')
        out = widgets.Text(disabled=True)
    
        l = link((button, 'value'), (out, 'value'))
    
        display(button,out)
        return out.value
        
    selected = display_radio_buttons(options)
    print(selected)
    

    EDIT following Wayne's comment

    Instead of returning the value of the out which is no longer updated, the button is return and its value is later printed (or utilized), remaining updated.

    from ipywidgets import widgets, link
    from IPython.display import display
    
    options=["A","B","C"]
    
    def display_radio_buttons(options):
        button = widgets.RadioButtons(options=options,description='Radio Selector:')
        out = widgets.Text(disabled=True)
        
        l = link((button, 'value'), (out, 'value'))
    
        display(button, out)
        return button
    
    selected = display_radio_buttons(options)
    print(selected.value)