How to add python callback using the bokeh client and server

Hi,

I would like to have a button that trigger a python callback on my plot. But the button does nothing.

The plot is correctly plotted and when I change the values of r1 or r2 in the console, the synchronisation works. It’s only the other way around that is not working.

import numpy as np

from numpy import pi

from bokeh.client import push_session

from bokeh.driving import cosine

from bokeh.plotting import figure, curdoc

from bokeh.models import Button

x = np.linspace(0, 4*pi, 80)

y = np.sin(x)

p = figure()

r1 = p.line([0, 4*pi], [-1, 1], color=“firebrick”)

r2 = p.line(x, y, color=“navy”, line_width=4)

open a session to keep our local document in sync with server

session = push_session(document=curdoc())

def update():

r1.data_source.data[‘x’]=[0,100]

add a button widget and configure with the call back

button = Button(label=“Press Me”)

button.on_click(update)

put the button and plot in a layout and add to the document

curdoc().add_root(vplot(button, p))

session.show()

``

Can someone guide me for the right thing to do.

Thanks a lot,

Robin

Hi Robin,

When using the bokeh.client interface, if you want the script with callbacks to service events, then it has to continue running. As it is, your script immediately terminates after the call to "session.show()" so there is no python process left to call "update". In order to make this work you need to add the blocking call "session.loop_until_closed()" to the end of your script, to make it keep running and responding to events as they come in. The following code works:

    import numpy as np
    from numpy import pi

    from bokeh.client import push_session
    from bokeh.plotting import figure, curdoc
    from bokeh.models import Button
    from bokeh.layouts import column

    x = np.linspace(0, 4*pi, 80)
    y = np.sin(x)

    p = figure()
    r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
    r2 = p.line(x, y, color="navy", line_width=4)

    # open a session to keep our local document in sync with server
    session = push_session(document=curdoc())

    def update():
        print()
        r1.data_source.data['x']=[0,100]

    # add a button widget and configure with the call back
    button = Button(label="Press Me")
    button.on_click(update)

    # put the button and plot in a layout and add to the document
    curdoc().add_root(column(button, p))

    session.show()

    session.loop_until_closed()

Thanks,

Bryan

···

On Jul 15, 2016, at 10:14 AM, Robin Duqué <[email protected]> wrote:

Hi,

I would like to have a button that trigger a python callback on my plot. But the button does nothing.
The plot is correctly plotted and when I change the values of r1 or r2 in the console, the synchronisation works. It's only the other way around that is not working.

import numpy as np
from numpy import pi

from bokeh.client import push_session
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc
from bokeh.models import Button

x = np.linspace(0, 4*pi, 80)
y = np.sin(x)

p = figure()
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)

# open a session to keep our local document in sync with server
session = push_session(document=curdoc())

def update():
    r1.data_source.data['x']=[0,100]

# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(update)

# put the button and plot in a layout and add to the document
curdoc().add_root(vplot(button, p))

session.show()

Can someone guide me for the right thing to do.

Thanks a lot,

Robin

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/6be4b593-a2e7-40da-971a-edfb46cf7fcc%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.