<jupyter_start><jupyter_text>V4A Example 07 - Modifying Widget LayoutsHere are three strategies for modifying widget layouts on the fly during Voila's Interactive Phase.<jupyter_code>import ipywidgets as widgets
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Adding and removing childrenAs long as you place a container widget during the Rendering Phase, you can add and remove child widgets later.In this example, you can click buttons to add or remove text boxes from a container widget.<jupyter_code>text_boxes = widgets.VBox(children=[widgets.Text(value='0')])
add_button = widgets.Button(description='Add')
remove_button = widgets.Button(description='Remove')

def on_add(b):
    new_box = widgets.Text(value=str(len(text_boxes.children)))
    text_boxes.children = list(text_boxes.children) + [new_box]

def on_remove(b):
    text_boxes.children = text_boxes.children[:-1]
    
add_button.on_click(on_add)
remove_button.on_click(on_remove)
widgets.HBox([add_button, remove_button, text_boxes])<jupyter_output><empty_output><jupyter_text>Toggling visibilityCreate all the widgets you might possibly need, then keep them hidden if you don't.In this example, you can move the slider to change how many of the buttons are visible.<jupyter_code>def update_visibility(change):
    for i, b in enumerate(buttons):
        if i < change.new:
            b.layout.visibility = 'visible'
        else:
            b.layout.visibility = 'hidden'
    
N = 5
buttons = [widgets.Button(description=str(i+1)) for i in range(0, N)]
slider = widgets.IntSlider(value=N, min=0, max=N)
slider.observe(update_visibility, names='value')
buttons_box = widgets.HBox(buttons)
widgets.VBox([buttons_box, slider])<jupyter_output><empty_output><jupyter_text>Using an Output widgetPlace an Output widget during the Rendering Phase, then display new widgets inside it.In this example, you can create new buttons by typing a name in the text box and pressing enter.  The new buttons are displayed in an Output widget, and they are connected to a callback to show when they are clicked.<jupyter_code>button_holder = widgets.Output()
click_output = widgets.Output()
name = widgets.Text(placeholder='Enter a new button name')

@click_output.capture()
def on_click(button):
    print(button.description)
    
@button_holder.capture()
def on_submit(text):
    new_button = widgets.Button(description=text.value)
    new_button.on_click(on_click)
    display(new_button)
    text.value = ''

name.on_submit(on_submit)
left = widgets.VBox([name, button_holder])
right = widgets.VBox([widgets.Label(value='Buttons clicked:'), click_output])
widgets.HBox([left, right])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0971449758991472, 'fraction_numerical': 0.0033370411568409346, 'mean_word_length': 5.661728395061728, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '562816', 'license': 'MIT', 'n_tokens_mistral': 843, 'n_tokens_neox': 820, 'n_words': 237, 'path': 'V4A Example 07 - Modifying Widget Layouts.ipynb', 'repo_name': 'nbgallery/Voila4Analysts', 'size': '2697'}