Is there a way to update the options in a selection menu from bokeh (python), by using JS Callbacks?

Viewed 114

So I've been trying to update the options in a selection menu by changing the list that I use to determine the possible options for the selected city. (Part of the code will be shown at the end of this tekst) I've been trying to update this plot using JS Callbacks but the only thing that works is when I update a source with source.change.emit(). So what I'm asking is: Is there a way to update a list instead of a source using JS Callbacks?

The code below is just a small bit of my actual code but should be enough to bring the message across. This is because my university looks down on fraud and I don't want to give them any reason to think that I committed something like that.

user_list = ["User1", "User2", "User3"]

callback = CustomJS(args=dict(users=user_list), code= """

users = [];
users.push("User2");
users.push("User3");

users.change.emit(); //This line does not work and I am wondering if it can be done differently
"""

I hope that this is enough explanation of my current dilemma and I hope you guys can help a fellow out!

Thanks in advance!

UPDATED VERSION, in response to the answer given to solve this problem The following code should be the correct code:

#Import of libraries here

select = MultiSelect(title="Users:", value=["User1"],
                        options=["User1", "User2", "User3"] )

callback = CustomJS(args=dict(users=select), code= """

users.options=["User2", "User3"];
users.value=["User2"];
"""

If everything is right, this should work.

1 Answers

It depends on where you use user_list. For example, if you pass it as options to Select, then you can just do select.options = ["User2", "User3"] in your JS code.

In your particular example, users = [] does not clear the array, it just creates a completely new array that the Bokeh model instance (not in the code) doesn't know about. A JS array doesn't have a change attribute, and that's why the last line doesn't work. If you just assign BokehJS models instance's attributes (as opposed to changing them in-place), then you don't even need to call instance.change.emit().

Related