Slider.js_link

Hello! I am new at programming and recently I’ve been trying to create an interactive with bokeh. I’ve created the code to display a softmax function. I want to use bokeh to allow the user to change the beta parameter with a slider and see how the plot changes.

I read that a way to connect my code with the slider is with the comand .js_link . I have the following:

In line 77 I undestand I have to introduce the beta parameter instead of the ‘line_width’ but is not possible, I am confused here :sweat: :sweat:

Hi @GabrielaFacio

The js_link method is a convenience mechanism to bind properties of two bokeh models so that the chosen properties remain synchronized automatically by bokeh without any additional user code.

A slider is a bokeh model; your softmax function is not. And this is the reason why what you are attempting will not work via js_link.

You have a few options to easily accomplish your objective. In general it requires writing a callback that is run when the value of the slider changes to update the beta parameter of your softmax function that is plotted.

OPTION 1: See the slider.py example in the bokeh gallery. This example is conceptually very similar to what you have described as your example.

Sliders are used to update parameters of a mathematical function; in that case amplitude, frequency, phase, and offset are controlled by their own dedicated sliders. Here, the callback is written in JavaScript using js_on_change methods and CustomJS as the JavaScript code that gets invoked when any of the sliders are manipulated.

NOTE bokeh uses the prefix js_ and suffix JS throughout its API as a convention to indicate that things are written in JavaScript.

The benefit of this option is that you can run your function as a standalone application.

OPTION 2: Write a python callback and use the on_change method that invokes it whenever a slider value changes. The implication here is that you need to run your program via the bokeh server, i.e., bokeh serve ..., since this is the mechanism to keep the python code and what is running in the browser synchronized.

See the user’s guide for a description of the bokeh server https://docs.bokeh.org/en/latest/docs/user_guide/server.html.

The user’s guide section on interactions also has more discussion about callbacks, the server, and the on_change method mentioned above. See https://docs.bokeh.org/en/latest/docs/user_guide/interaction/widgets.html.

1 Like