question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manual selected change event does not trigger js callbacks

See original GitHub issue

Consider 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:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bryevdvcommented, Oct 11, 2018

@ibestvina Good news/bad news. This code (with my suggestion):

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 = [];
""")

button.js_on_event(events.ButtonClick, callback_click)

source.selected.js_on_change('indices', callback_select)

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.

1reaction
bryevdvcommented, Oct 12, 2018

@npeirson thanks I am updating the property help string in the source code for that .callback property to point to source.selected.on_change

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found