I'm having problems creating widgets from a function -- specifically using observe
to perform the function callback. It must be related to the observe, because a simple test case works fine:
from ipyfilechooser import FileChooser
import sys
import os
sys.path.append("shared/heminger/lib/dcdat")
sys.path.append("shared/heminger/lib/combustion_functions")
import converter
import DataViz
import ipywidgets as widgets
from ipywidgets import TwoByTwoLayout, Layout
def create_sensors_list_widget():
sensor_list = widgets.Textarea(
value='tag,tag2',
placeholder='Type something',
description='List of Sensors (separated by comma)',
disabled=False
)
display(sensor_list)
create_sensors_list_widget()
This code cell will properly display the textarea widget. However, when a similar function is called by function which is called by widget.RadioButtons.observe
it all falls apart. Here is what I'm working with at the moment. The intention is that an appropriate widget will be displayed depending on the selection of the ratio button (note, I do not have code to remove/delete widgets if the choice changes, but will add later).
It seems that the functions are being called... if I open my log, I can see the output showing up there (I know that in order to output normally via ipwidgets, I need to create an Output object). So, the code is entering the functions
from ipyfilechooser import FileChooser
import sys
import os
import ipywidgets as widgets
from ipywidgets import TwoByTwoLayout, Layout
output_radio_selected = widgets.Text()
button_all_sensors = widgets.RadioButtons(
options=['Read all sensors', 'Sensors from File', 'Manual Entry'],
value='Read all sensors',
description='Read all sensors?:',
disabled=False
)
def create_sensor_option_widgets():
print('test')
create_sensors_list_widget()
if selected_option == 'Sensors from File':
print(' from file' )
create_sensors_from_file_widget()
elif selected_option == 'Manual Entry':
create_sensors_list_widget()
print(' selected manual entry')
else:
print('oops')
def create_sensors_from_file_widget():
sensor_file = FileChooser('/')
sensor_file.title = '<b>File List Containing Sensors</b>'
sensor_file
def create_sensors_list_widget():
sensor_list = widgets.Textarea(
value='test1, test2',
placeholder='Type something',
description='List of Sensors (separated by comma)',
disabled=False
)
display(sensor_list)
def bind_selected_to_output(sender):
global selected_option # Global variable to hold the user input for reuse in your code
output_radio_selected.value = button_all_sensors.value
selected_option = output_radio_selected.value # Example variable assigned the selected value
print('Selected option set to: ' + selected_option) # For test purposes
create_sensor_option_widgets()
button_all_sensors.observe(bind_selected_to_output, names=['value'])
display(button_all_sensors)
Note that the original function to pass the value of the radio button into a global variable and call a function on state change was pulled from Why is my ipywidget observe being call multiple times on a single state change?