ipywidgets Textarea on_submit()

Viewed 609

I need to set up a certain workflow where the input text field needs to be more than one-liner, therefore a simple Text widget cannot be used. This is why the best fit is Textarea widget. However... this widget does not have an on_submit() method. Is there a way that I can add an on_submit() method to Textarea so it will work like a regular Text widget where I can submit entered text with an Enter key stroke. The preference is to keep to ipywidgets. Thanks!

import ipywidgets as widgets
text = widgets.Text()
display(text)

def handle_submit(sender):
    print(text.value)

text.on_submit(handle_submit)
1 Answers

As a workaround you can create a "Submit" button with ipywidgets and set up the on_button_clicked function to print the value of your text area. See code below:

import ipywidgets as widgets
text_area = widgets.Textarea()
submit_button=widgets.Button(description='Submit text',button_style='success')
def on_button_clicked(b):
  print(text_area.value)


display(text_area,submit_button)
submit_button.on_click(on_button_clicked)

And the output looks like that:

enter image description here

Related