Repository: bokeh/bokeh

Issue:
"CustomJS for Selections" Example in Docs Broken
In the latest version of the docs, it appears [this example]( https://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-selections ) is broken. This is also true of the example in the Bokeh 1.0.0 docs. Selecting points in the plot on the left does not result in points being shown in the right plot. Compare this to [the same plot using Bokeh 0.13.0]( https://bokeh.pydata.org/en/0.13.0/docs/user_guide/interaction/callbacks.html#customjs-for-selections ), which seems to work without issues.


Discussion:
I think this is related to the LassoSelectTool. I was trying to use it for a project with version 1.0.1, however I saw that the `cb_obj.selected.indices;` was not returning anything at all. Furthermore, I suspect that the callback is not being executed when lasso select is used. In version 0.13.0 this does not appear to be a problem, as mentioned by @jakirkham.
It’s not limited to the lasso tool. Can get this with the tap tool as well.
The examples merely need to be updated, the way selections work were updated quite a while ago and backward compatibility was fully dropped in 1.0, e.g. the lasso select example should now read something like this:

```python
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.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2), code="""
        var inds = cb_obj.indices;
        var d1 = s1.data;
        var d2 = s2.data;
        d2['x'] = []
        d2['y'] = []
        for (var 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)
```
Thank you very much, @philippjfr! I was not aware of the change starting bokeh 1.0. If no one beats me to it, I will submit a PR updating the docs in the coming days 😄.  
Thanks @philippjfr. Should that code work with Bokeh 0.13.0 as well or only with 1.0.0+?
I think that code should work since bokeh 0.12.15.
Hmm...interesting...for some reason the callback doesn't seem to ever be called for me on Bokeh 0.13.0.
Odd, I'll try to track down what caused the change in 1.0.0.
Have another case that is similar to this example that runs the callback the first time, but then doesn't run it again. I'll try to put it together in some form that can be played with more easily and share it.
>example that runs the callback the first time

Does that issue persist with 1.0.x? If not I doubt there's anything that can and will be done about it. There were some unfortunate issues introduced by the change in selections. Specifically 0.12.15 introduced an explicit ``Selection`` model but unfortunately I think instead of using that model correctly the initial commit seems to have replaced the model each time which meant that listening to the ``cds.selected.indices`` did not immediately work. This was fixed some time more recently which seemingly caused this disruption. I don't have a very solid grasp of the history of these changes and @bryevdv is out all of this month.
Thanks for pointing that out. Indeed it works fine with Bokeh 1.0.0+.

That should be fine for our needs. Just wanted to make sure there wasn't something else we need to change too. ;)
Also, for clarity, it appears the updated example works fine for Bokeh 1.0.0+. So there must be some other issues that were still present in 0.13.0 that caused the callback to not work.
Bokeh 0.13 had a unfortunate regression around this, there is not much to do about it now except upgrade (or downgrade, I suppose), or temporarily change code just for 0.13. The version of above is correct for 1.0 and, more importantly, is what is now maintained under new continuous integration tests so that it will continue to always work in the future.

Can someone on this issue submit the small PR to update the example? The relevant file is here:

https://github.com/bokeh/bokeh/blob/master/sphinx/source/docs/user_guide/examples/interaction_callbacks_for_selections.py

Resolving patch:
diff --git a/sphinx/source/docs/user_guide/examples/interaction_callbacks_for_selections.py b/sphinx/source/docs/user_guide/examples/interaction_callbacks_for_selections.py
--- a/sphinx/source/docs/user_guide/examples/interaction_callbacks_for_selections.py
+++ b/sphinx/source/docs/user_guide/examples/interaction_callbacks_for_selections.py
@@ -18,9 +18,9 @@
             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.indices;
-        var d1 = cb_obj.data;
+s1.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2), code="""
+        var inds = cb_obj.indices;
+        var d1 = s1.data;
         var d2 = s2.data;
         d2['x'] = []
         d2['y'] = []
@@ -30,6 +30,7 @@
         }
         s2.change.emit();
     """)
+)
 
 layout = row(p1, p2)
 


Test patch: