If I have a simple widget with a select drop-down menu, a text box and a button:
I would like to enter text into the text box and it appear in the select drop down when I press the button. If I do this via Python, as follows, I cannot get the DOM to update even though the print statement shows the correct value for options
:
from bokeh.plotting import curdoc
from bokeh.layouts import column
from bokeh.models import Button, CustomJS, Select, TextInput
from bokeh.events import ButtonClick
def add_entry(event):
options.append(text_input.value)
select.options = options
select.value = text_input.value
text_input.value = ''
print(f'\n{select.options}\n')
options = ['first']
# Site selector
select = Select(title = 'Choose Option:', options = options)
text_input = TextInput(value = '', title = 'Enter Name:')
button = Button(label = 'Add New Entry')
# Listen for events
button.on_event(ButtonClick, add_entry)
# put the button and plot in a layout and add to the document
curdoc().add_root(column(select, text_input, button))
However, if I do the same thing via a Javascript callback I get the intended behaviour:
# %% Imports
# Bokeh imports
from bokeh.plotting import curdoc
from bokeh.layouts import column
from bokeh.models import Button, CustomJS, Select, TextInput
from bokeh.events import ButtonClick
options = ['first']
# Site selector
select = Select(title = 'Choose Option:', options = options)
text_input = TextInput(value = '', title = 'Enter Name:')
button = Button(label = 'Add New Entry')
# Listen for events
button.js_on_event(ButtonClick, CustomJS(args = {'options': options, 'select': select, 'text_input': text_input}, code = """
options.push(text_input.value);
select.options = options;
select.value = text_input.value;
text_input.value = '';
"""))
# put the button and plot in a layout and add to the document
curdoc().add_root(column(select, text_input, button))
Is this expected or am I doing something wrong?