3

I would like to use widgets.Text() to specify multiple variables (year, month, day) that can be passed into an API request.

Based on the answer to this question I am able to successfully save the input from a single text box to a single variable. But I would like to display multiple text boxes at the same time and save their input values to three different output variables. I'm unsure how to generalize from the example given.

This code works for a single variable:

# Create text widget for output
year_output = widgets.Text()

# Create text widget for input
year_input = widgets.Text(
    placeholder="example '2017'",
    description='Year:',
    disabled=False
    )

# Define function to bind value of the input to the output variable 
def bind_input_to_output(sender):
    year_output.value = year_input.value

# Tell the text input widget to call bind_input_to_output() on submit
year_input.on_submit(bind_input_to_output)

# Display input text box widget for input
display(year_input)

I would like to be able to do something like this, as efficiently as possible:

year_output = widgets.Text()
month_output = widgets.Text()
day_output = widgets.Text()

year_input = widgets.Text(
    placeholder="example '2017'",
    description='Year:',
    disabled=False
    )

month_input = widgets.Text(
    placeholder="example '04'",
    description='Month:',
    disabled=False
    )

day_input = widgets.Text(
    placeholder="example '30'",
    description='Day:',
    disabled=False
    )

#make this a generic function so that I don't have to repeat it for every input/output pair
def bind_input_to_output(sender): #what is 'sender'?
    output_var.value = input_var.value

year_input.on_submit(bind_input_to_output)
mont_input.on_submit(bind_input_to_output)
day_input.on_submit(bind_input_to_output)

display(year_input)
display(month_input)
display(day_input)

Apologies if this isn't clear enough! I can clarify as needed. Any guidance is greatly appreciated. Thank you!

Community
  • 1
  • 1
J-Mo
  • 63
  • 1
  • 5

1 Answers1

3

I was able to do what I wanted by adapting the instructions from this question. My code is below, for reference:

import ipywidgets as widgets
from IPython.display import display

class date_input():
    def __init__(self, 
                 year = "e.g. '2017'", 
                 month = "e.g. '05'", 
                 day = "e.g. '21'"
                ):
        self.year = widgets.Text(description = 'Year',value = year)
        self.month = widgets.Text(description = 'Month',value = month)
        self.day = widgets.Text(description = 'Day',value = day)        
        self.year.on_submit(self.handle_submit)
        self.year.on_submit(self.handle_submit)
        self.year.on_submit(self.handle_submit)
        display(self.year, self.month, self.day)

    def handle_submit(self, text):
        self.v = text.value
        return self.v

print("enter the year, month and day above, then press return in any field")
f = date_input()

To view the output, in the next cell run:

print("Date input captured: " + "/".join([f.year.value, f.month.value, f.day.value]))
Community
  • 1
  • 1
J-Mo
  • 63
  • 1
  • 5