CustomJS accepts arguments that are passed to javascript during callback.
Though, I observed that if I use a variable as argument to a CustomJS callback, then modify the value of the variable before triggering the callback, the callback uses an outdated value of the variable.
This is different from python callbacks, in which the current value of inputs variables are used.
Here is a short example comparing the behavior of Python and CustomJS callbacks, in which I also introduced what I see as a workaround for CustomJS to use an up to date argument, using a paragraph object to store the input (launch with bokeh serve --show
):
from bokeh.io import curdoc
from bokeh.models import Button, Paragraph, CustomJS
from bokeh.layouts import column
js_code = """
alert("Input is: " + input +
", Input from paragraph is: " + paragraph.text)
"""
button_py = Button(label="Start python-callback")
button_js = Button(label="Start js-callback")
input = 1
paragraph = Paragraph(text=str(input))
def py_callback():
print(f"Input is {input}")
print(f"Input from paragraph is {paragraph.text}")
# Callbacks:
button_py.on_click(py_callback)
button_js.js_on_click(CustomJS(args=dict(input=input, paragraph=paragraph), code=js_code))
input = 2
paragraph.text = str(input)
curdoc().add_root(column(button_py, button_js))
The Python callback leads as expected to the print log:
“Input is 2
Input from paragraph is 2”
The CustomJS callback leads to “Input is: 1, Input from paragraph is: 2”, I had expected “Input is: 2, Input from paragraph is: 2”.
Is there another more straightforward way to dynamically update the arguments of CustomJS callbacks than passing them to a bokeh model?
My system:
Windows 10
Python 3.12.0
bokeh 3.3.0