Skip to content

Instantly share code, notes, and snippets.

@umdstu
Created April 9, 2020 21:45
Show Gist options
  • Save umdstu/69eef639dbc47a6a62a8de1f60e5fa44 to your computer and use it in GitHub Desktop.
Save umdstu/69eef639dbc47a6a62a8de1f60e5fa44 to your computer and use it in GitHub Desktop.
# current option 1
input1 = ipywidgets.Text(description="Input Label Here")
input1.observe(input_changed)
input2 = ipywidgets.Text(description="Input Label2 Here")
input2.observe(input_two_changed)
input3 = ipywidgets.Text(description="Input Label3 Here")
input3.observe(input_three_changed)
input4 = ipywidgets.Text(description="Input Label4 Here")
input4.observe(input_four_changed)
input5 = ipywidgets.Text(description="Input Label5 Here")
input5.observe(input_five_changed)
def input_changed(field1):
update('f1', field1)
def input_two_changed(field2):
update('f2', field2)
def input_three_changed(field3):
update('f3', field3)
def input_four_changed(field4):
update('f4', field4)
def input_five_changed(field5):
update('f5', field5)
def update(id, change_model):
if id == 'f1':
# something
elif id == 'f2':
# something
elif ...
# vs ...
# current option 2
input1 = ipywidgets.Text(description="Input Label Here")
input1.observe(input_changed, names='value')
input2 = ipywidgets.Text(description="Input Label2 Here")
input2.observe(input_changed, names='value')
input3 = ipywidgets.Text(description="Input Label3 Here")
input3.observe(input_changed, names='value')
input4 = ipywidgets.Text(description="Input Label4 Here")
input4.observe(input_changed, names='value')
input5 = ipywidgets.Text(description="Input Label5 Here")
input5.observe(input_changed, names='value')
def input_changed(field1):
if field1.input.description == 'Input Label Here':
# do something
elif field.input.description == 'Input Label2 Here':
# do something
# do something
elif field.input.description == 'Input Label3 Here':
# do something
# do something
elif field.input.description == 'Input Label4 Here':
# do something
# do something
elif field.input.description == 'Input Label5 Here':
# do something
# vs
# better option not possible
input1 = ipywidgets.Text(description="Input Label Here")
input1.observe(input_changed, names='value', id='f1')
input2 = ipywidgets.Text(description="Input Label2 Here")
input2.observe(input_changed, names='value', id='f2')
input3 = ipywidgets.Text(description="Input Label3 Here")
input3.observe(input_changed, names='value', id='f3')
input4 = ipywidgets.Text(description="Input Label4 Here")
input4.observe(input_changed, names='value', id='f4')
input5 = ipywidgets.Text(description="Input Label5 Here")
input5.observe(input_changed, names='value', id='f5')
def input_changed(field):
if field.id == 'f1':
# do something
elif field.id == 'f2':
# do something
elif field.id == 'f3':
# do something
elif field.id == 'f4':
# do something
elif field.id == 'f5':
# do something
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment