Search code examples
pythonipywidgets

can't suppress the function name to be displayed when using nested ipywidgets interact functions


Using nested widgets interact functions, as shown in minimun case example below, however can't suppress the function name to be displayed. Semi-colon does not work.

import ipywidgets as widgets

def dummy_func(a, b):
    return a + b

def interactive_dummy_func(b=4, a=2):
    return widgets.interact(dummy_func, 
                             a = a,
                             b = b);

@widgets.interact
def wrapped_interactive_dummy_func(condition=True):
    if condition == True:
        return interactive_dummy_func(a=3, b=5);
    else:
        return "Something else"

Output always show the function name and can't manage to suppress it

enter image description here


Solution

  • An interact wrapped function automatically displays, so no need to return anything in your last function (that is where the function name is coming from)

    import ipywidgets as widgets
    
    def dummy_func(a, b):
        return a + b
    
    def interactive_dummy_func(b=4, a=2):
        return widgets.interact(dummy_func, 
                                 a = a,
                                 b = b)
    
    @widgets.interact
    def wrapped_interactive_dummy_func(condition=True):
        if condition == True:
            interactive_dummy_func(a=3, b=5)
        else:
            return "Something else"