'IPython.display not showing TextBox in ipywidgets

I'm trying to build such a functionality such that whenever the user clicks in the Add button in my code, it generates a new text box, just under the old one. For example, like this:

enter image description here

Now, if the user were to click on add once again, a 5th text box should appear.

I've tried to achieve the same using this piece of code:

add_button = widgets.Button(description='Add',
                            disabled=False,
                            button_style='',
                            style={'description_width': 'initial', 'button_width': 'auto'},
                            icon='plus'
                        )
display(add_button)
add_button.on_click(add_new)

And my add_new function is simply defined as follows:

def add_new(*args):
    display(widgets.Text(placeholder='Type something',description='String:'))

But this does not seem to be working nothing happens on clicking the button, any help would be appreciated. Also if there is a better way to do this, please help, I'm new to ipywidgets.



Solution 1:[1]

Try like this:

output = widgets.Output()
def add_new(*args):
    with output:
        display(widgets.Text(placeholder='Type something',description='String:'))
add_button = widgets.Button(description='Add',
                            disabled=False,
                            button_style='',
                            style={'description_width': 'initial', 'button_width': 'auto'},
                            icon='plus'
                        )
display(add_button)
add_button.on_click(add_new)
output

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Manimuthu Ayyannan