I noticed that CDS.on_js_change is never fired when the CDS is not used in a plot. (This is a follow up to CDS.js_on_change not called, but CDS.on_change is
I have a use case where I have a CDS containing data for multi_line, being dynamically updated from another source. I will let the user decide if the data should be displayed as lines or patches. Therefore I would like to be able to convert the multi_line data into another CDS which is compatible with patches on the fly via on_js_change. If the user chooses to display as patches, the multi_line CDS will not be displayed as it is only an intermediate step.
Here is a minimum example that reproduces the problem when the fig.patches(xs='xs', ys='ys', source=source1)
is commented out (sorry for my use of panel here):
from bokeh.plotting import Figure
import panel as pn
fig = Figure()
button = pn.widgets.Button(name='click me')
source1 = ColumnDataSource(data={'xs': [], 'ys': [], 'color':[]})
source2 = ColumnDataSource(data={'xs': [[1,50,50,1]], 'ys': [[0,0,10,10]], 'color':['#00ff00']})
js_callback = CustomJS(args=dict(source1=source1, source2=source2),
code='''console.log("triggered in js");
source2.data = source1.data;''')
def python_callback(attr, old, new):
print('triggered in python!')
#Setup both python and js callbacks for changes to source1:
source1.js_on_change('data', js_callback)
source1.on_change('data', python_callback)
# Show source2 as patches
fig.patches(xs='xs', ys='ys', fill_color='color', source=source2)
#If this patches is commented out, the js_callback is never fired. Only the python callback:
fig.patches(xs='xs', ys='ys', source=source1)
def update_source(*_):
source1.data = {'xs': [[100,5000,5000,100]], 'ys': [[0,0,10,10]], 'color':[['#000000']]}
button.on_click(update_source)
pn.serve(pn.Column(pn.pane.Bokeh(fig), button))
Actually, I notice that the color of the patch does not correctly change to black in this example. But I think that is an unrelated issue…