2

First, great site, great people. You all have helped me a lot with my learning. Thanks!

I am having trouble with Bokeh and browsers. In particular, I am trying to get a Javascript callback to work in Bokeh.

I downloaded this example code from this website

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html.

The website contains an example of using the lasso tool.

The code works perfectly on the website, but when I copy the code into Python and run it myself, the JS callbacks don't work. The lasso tool part works fine. I have tried running this on IE, Chrome, Firefox, work computer and home computer.

I am not very good with Javascript in general, so any insight into this problem will be greatly appreciated.

Cheers and thanks in advance.

Code from the website below:

from random import random

from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show

output_file("callback.html")

x = [random() for x in range(500)]
y = [random() for y in range(500)]

s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6)

s2 = ColumnDataSource(data=dict(x=[], y=[]))
p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1),
            tools="", title="Watch Here")
p2.circle('x', 'y', source=s2, alpha=0.6)

s1.callback = CustomJS(args=dict(s2=s2), code="""
        var inds = cb_obj.selected['1d'].indices;
        var d1 = cb_obj.data;
        var d2 = s2.data;
        d2['x'] = []
        d2['y'] = []
        for (i = 0; i < inds.length; i++) {
            d2['x'].push(d1['x'][inds[i]])
            d2['y'].push(d1['y'][inds[i]])
        }
        s2.change.emit();
    """)

layout = row(p1, p2)

show(layout)
6
  • Do you have bokeh version 0.12.6 installed? I copied your code and it works with google chrome. Commented Aug 31, 2017 at 2:06
  • Hi. I have Bokeh 0.12.4 installed at work. It isn't easy for me to get the office version updated, so I will try it at home. I suspect it has something to do with my settings in Chrome. Thank you for your advice. Commented Aug 31, 2017 at 2:43
  • i think in 0.12.4 change.emit() is not the correct syntax to register a change to source data. That would be my guess, unless javascript is disabled which you would likely be aware of. Try replace s2.change.emit() with s2.trigger('change') and retry it (dont use this with 0.12.6 though). Commented Aug 31, 2017 at 2:48
  • Yes! Thank you Thank you thank you! Commented Aug 31, 2017 at 3:01
  • I can't seem to state that you answered this question... Commented Aug 31, 2017 at 3:03

1 Answer 1

4

The issue is a difference in bokeh versions, you are using version 0.12.4. In bokeh version 0.12.4, to register a change in a column data source you need to use the syntax source.change('trigger').

The examples on the latest version of the documents (which is where the example you have referenced originates from) uses version 0.12.6. As of bokeh version 0.12.6 this is depreciated, and the syntax has now becomesource.change.emit().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.