'How do I get voila to display the output (ipywidgets) of nested functions?

My goal is to get this simplified example to work. If I run execute(), widget 1 should be displayed along with a button to add widget 2. If the button is clicked, the whole execute() should be run again and now display widgets 1 and 2. If I call this in my Jupyter notebook, it works fine - however, with voila - only the direct execution of execute() is displayed. Is there any way I can fix this?

import ipywidgets as widgets
from ipywidgets import HBox, VBox
from IPython.display import clear_output

w2 = False

def add_w2(b1):
    
    global w2
    
    w2 = True
    clear_output(wait=False)
    execute()

def execute():
    
    global w2
    
    widget1 = widgets.Text('Widget 1')
    b1 = widgets.Button(description='Add widget 2')
    b1.on_click(add_w2)

    wdgts = HBox([widget1, b1])

    if w2 == True:
        widget2 = widgets.Text('Widget 2')
        widget1.value = 'sss'
        wdgts = VBox([widget1, widget2])
    
    return display(wdgts)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source