Get updated widget value, which is inside of a class

Hello,
I am working with two Select-widget where one of them (select_2) is inside a class. Whenever I change the select_1, which is not part of the class, select_2 goes back to the first value which has been chosen.

What I want:
I want to achieve that the value of select_2 always keep and does not get affected by changes in select_1

What I tried:
I tried to use the value of select_2 as an input-variable of the class. However this works only until i change select_1 for the first time.

Example:

  1. Change select_2 from ‘first’ to ‘second’
  2. Change select_1 from A to B.
  3. Change select_2 from ‘second’ to ‘first’
  4. Change select_1 from B to A

after step 4 the value of select_2 changes to ‘second’ again.

What can I do so that after step 4 the value of select_2 stays as it is? Thanks in advance for help!

Working example:

from bokeh.io import curdoc
from bokeh.models import Select
from bokeh.layouts import column

col=['A','B']
select_1 = Select(title='select_1', value='A', options=col)

class one():
    def __init__(self, selection):
        self.selection=selection
        self.col=['first','second']
        self.select_2 = Select(title='select_2', value=self.selection, options=self.col)

a=one('first')
select_2=a.select_2

def update():
    print(a.select_2.value)
    layout.children[1] = one(a.select_2.value).select_2

select_1.on_change("value", lambda attr, old, new: update())

layout=column(select_1,select_2)
curdoc().add_root(layout)

The update function refers to a.select which never changes, because a never changes. When the callback is called after the first invocation, it’s referring to a Select object that still exists as an object, but it no longer the Select object that is actually rendered for users to interact with (because you replaced the the item in the layout. To get the current value of the Select that is actually displayed, you should use the Select object that’s inside the layout.

1 Like