I have two questions/problems:
I am trying to generate an interface where the user can select options from combobox/dropdowns, add some detail text then click a button which executes a function using the values initially input by the user. This should be re-runnable ie. if they change the values they can hit the button again to rerun the function using the new values.
Question 1
Currently the function does not run on clicking the button (non-working version below - nothing printed out). In case it is relevant since I believe Jupyter doesn’t handle print from widgets well, the final function won’t be printing output it will be executing other functions to go retrieve data and manipulate it before returning something (likely needing to be displayed).
Question 2
I’d also like the optW text box (or possibly different widget) to appear as needed based on the option selected in the third dropdown but as soon as I add another .observe the first one (which changes the options for the combobox stops working).
Code below, any help appreciated.
Using:
Python 3.9.19 (unable to update due to matching client system)
ipywidgets 8.1.3
JupyterLab 4.0.11
import ipywidgets as widgets
from IPython.display import display
hdrm = {'APPLE BLUE': ['12345852'], 'APPLE BROWN': ['12345684', '12345738', '12345756'], 'APPLE GREEN': ['12345697', '12345841', '12345873'], 'APPLE ORANGE': ['12345826'], 'APPLE PINK': ['12345690', '12345698', '12345872'], 'APPLE PURPLE': ['12345727', '12345784', '12345859', '12345862'], 'APPLE RED': ['12345692', '12345711', '12345777', '12345806'], 'APPLE YELLOW': ['12345767', '12345809', '12345824', '12345855']}
# This function updates the options for the LID combobox based on the user choice from the nickname combobox
def update_lid_options(*args, **kwargs):
try:
lidW.value = ''
lidW.options = hdrm_dict[nicknameW.value]
print(hdrm_dict[nicknameW.value])
except:
lidW.options = ['']
print('Failed to update')
# This function represents further code which should be executed when the button is pressed
def print_lid(idx,task,details):
with output:
print(idx,task,details)
# First combobox (nickname)
nicknameW = widgets.Combobox(
placeholder='APPLE GREEN',
options=list(hdrm_dict.keys()),
description='Name:',
ensure_option=True,
disabled=False
)
# Second combobox (id)
lidW = widgets.Combobox(
placeholder='12345678',
options = [''],
description='ID:',
ensure_option=True,
disabled=False
)
# Dropdown (option)
optW = widgets.Dropdown(
placeholder='Choose option',
options=['Task1','Task2','Task3'],
description='Task',
ensure_option=True,
disabled=False
)
# Text box (details)
detailsW = widgets.Textarea(
value='',
placeholder='Further details',
description='String:',
disabled=False
)
# Button to run function
buttonW = widgets.Button(description='Print LID')
# Output widget
outputW = widgets.Output()
# Display widgets
display(nicknameW,lidW,optW,detailsW,buttonW,outputW)
# Monitors the first combobox and updates the second combobox based on the selection
nicknameW.observe(update_lid_options)
# On click button should execute further code using variables from the value of the widgets
buttonW.on_click(print_lid(lidW.value,optW.value,detailsW.value))
What the widgets currently look like: