Manual selected change event does not trigger js callbacks
See original GitHub issueConsider the following example which has a plot, div and a button. Div should show the number of selected points, and the button should clear the selection. However, although the selection is cleared in the plot, div is not updated (i.e. source.js_on_change
callback is not triggered).
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import widgetbox, gridplot
from bokeh.models import ColumnDataSource
from bokeh.models import Div, Button
from bokeh import events
from bokeh.models.callbacks import CustomJS
from bokeh.sampledata.iris import flowers
output_file('clear_selection_button.html')
div = Div(text='Len: 0', width=200, height=20)
source = ColumnDataSource(flowers)
button = Button(label='Clear selection', button_type='success')
callback_select = CustomJS(args={'div':div, 'source':source}, code="""
div.text = 'Len: ' + source.selected.indices.length;
""")
callback_click = CustomJS(args={'source':source}, code="""
source.selected.indices = [];
source.change.emit();
""")
button.js_on_event(events.ButtonClick, callback_click)
source.js_on_change('selected', callback_select)
p1 = figure(plot_height=300, plot_width=300, tools=['box_select', 'reset'])
p1.circle(x='petal_length', y='petal_width', source=source, color='black')
show(gridplot([[p1], [widgetbox(div)], [button]]))
Tried on Firefox (61), Chrome (69) and Edge (42). Windows 10 64bit. Bokeh 0.13.
As a side question: it would seem to me that, idiomatically, the correct thing to use here would be source.selected.change.emit()
, which seems to exist (at least JS doesn’t complain), but it produces neither of the two expected effects: updating the plot and updating the div. Is it even supposed to work?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
javascript - Solution for on change doesn't work when input is ...
The onchange event only fires if the user changes the value of the input. It isn't supposed to fire if the input is...
Read more >Ajax callback of select form field not triggered when changed ...
In views-admin.js file of the Views module I found .trigger('change') for overriding a select. Maybe that's it.
Read more >.change() | jQuery API Documentation
A function to execute each time the event is triggered. ... the value of an input element using JavaScript, using .val() for example,...
Read more >JavaScript callbacks — Bokeh 2.4.3 Documentation
These CustomJS callbacks can be attached to property change events on any Bokeh ... allows CustomJS callbacks to be triggered by specific interaction...
Read more >Advanced Callbacks | Dash for Python Documentation | Plotly
In certain situations, you don't want to update the callback output. You can achieve this by raising a PreventUpdate exception in the callback...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ibestvina Good news/bad news. This code (with my suggestion):
Works the #8310 Pull Request branch. #8310 should be merged later today, and will be part of the next release.
#8310 fixes an unfortunate mistake made earlier, namely that BokehJS would itself sometimes replace the source.selected value with an entirely new Selection. That has a number of bad consequences, including what you see here (i.e. the callbacks you added were to a specific Selection object that the plot started with, then it disappeared when it BokehJS replaced it) However, #8310 fixes things so that Bokeh never replaces source.selected, it only ever updates properties on the existing selection, e.g. source.selected.indices This leads to much more comprehensible semantics (and is now also able to be maintained under test since our CI infrastructure for selenium tests has been restored).
So, that’s good news for the very near future. Bad news is that I am not sure I have any good workaround to advise for current releases, so I can only suggest to wait until a future release with #8310 merged. In any event, this specific same problem covered by the linked issues in #8310 so I am closing this as a
duplicate
.@npeirson thanks I am updating the property help string in the source code for that
.callback
property to point tosource.selected.on_change