Is it possible to have the updated value of the Bokeh widget in the Python?

Hi everyone, I have two unsolid thoughts, where I need some confirmation/help:

First:
If you are using the standalone version of Bokeh, and you want to use a widget, which when the widget value was modified from the GUI (eg.: the user select a new value from the select), this new value is only available for the Javascript, and there is no way to have this new value in the Python layer.

Second:
The solution is to get the value of the widget in the Python layer is the Bokeh server approach.

Thank You, and have a nice day!

Additional information: (not necessary to read)

I am a new in Bokeh.
I would like to use Bokeh for modeling with widgets.
I currently tried the Standalone version of Bokeh with Jupyter notebook.

My goal is to get the values of the widgets on the Python layer, so for example:

import pandas as pd
import datetime as dt
from bokeh.plotting import figure, show, ColumnDataSource
from bokeh.io import output_notebook
from bokeh.models import BoxSelectTool, HoverTool, RangeSlider, RadioGroup, CustomJS
from bokeh.models.widgets import Select
from bokeh.models.callbacks import CustomJS
import numpy as np
from bokeh.layouts import column, row

select = Select(title="Features", value="SpO2", options=["SpO2", "HRV", "Cpnn", "CMCardiacEjectionTime", "CMBottomToPeakTime"])

def callback(value):
    select.value = value

select.js_on_change("value", callback)

so then if I choose another value from the dropdown eg: Cpnn, I can use this value in the Python code, and not just in the JS layer as:

select.js_on_change('value', CustomJS(args=dict(select=select),
             code="""
             console.log(this.value);
             var select = select
             select.value = this.value;
             select.change.emit()
             alert(select.value)
             """
    ))

Summarized:
I want to use JS event listeners on widgets to call Python functions with their values.

Any help is greatly appreciated!
THX

If by “standalone version of Bokeh” you mean static HTML files created with save/show, then yeah, you cannot communicate with the Python side there at all. Simply because there’s no Python running when the plot is displayed.

What you want absolutely requires Bokeh server. And then you just use on_change instead of js_on_change. There are multiple examples on this page: Adding widgets — Bokeh 2.4.2 Documentation

2 Likes

Alright, thank’s a lot:)
Understand

Using the examples, I can update colors and hover values of a plot based on a dropdown widget selection (using JS callbacks). But if I try to change the labels using citation = Label(…), I cannot change the label based on the widget selection.

Is it safe to assume that p-himik’s response to this question applies to this case too? Do I need to have a bokeh server running?

If you don’t need to run Python code to get the data necessary to update those labels, then you can do it with JS. If that’s the case, please ask a new question and provide a minimal reproducible example.

Thanks. Resolved it using Select Widget instead of Dropdown.