How to define a handler / callback for widget Select?

Hello,

I tried to define a select widget and a handler / callback that is executed when the selected item is changed.

from bokeh.client import push_session
from bokeh.plotting import curdoc
from bokeh.layouts import layout, widgetbox
from bokeh.models.widgets import Select
import subprocess
import time

def select_handler(attr, old, new):
print(‘Select value changed from {} to {}’.format(old, new))

if name == ‘main’:
args = [‘python’, ‘-m’, ‘bokeh’, ‘serve’]
p = subprocess.Popen(args)
time.sleep(1)

select = Select(title="Select Test:", value="no1",
                options=["no1", "no2"])
select.on_change('value', select_handler)

layout = layout([[widgetbox(select)]], sizing_mode='scale_width')  
 
session = push_session(curdoc())
session.show(layout)

``

``

Result:

But the callback is not triggered when I change between ‘no1’ and ‘no2’.

Any help is appreciated.

Thanks

Daniel

Ok, I am a step further.

The handler / callback is working when I change the value of the select widget on the python side:

from bokeh.layouts import layout, widgetbox
from bokeh.models.widgets import Select
import subprocess
import time

def select_handler(attr, old, new):
print(‘Select value changed from {} to {}’.format(old, new))

if name == ‘main’:
args = [‘python’, ‘-m’, ‘bokeh’, ‘serve’]
p = subprocess.Popen(args)
time.sleep(1)

select = Select(title="Select Test:", value="no1",
                options=["no1", "no2"])
select.on_change('value', select_handler)

layout = layout([[widgetbox(select)]], sizing_mode='scale_width')  
 
session = push_session(curdoc())
session.show(layout)
select.value = 'no2'

``

``

But it is not reacting to changes in the browser.

The console output:

2016-10-28 21:40:51,144 Starting Bokeh server version 0.12.3
2016-10-28 21:40:51,151 Starting Bokeh server on port 5006 with applications at paths [‘/’]
2016-10-28 21:40:51,151 Starting Bokeh server with process id: 7104
2016-10-28 21:40:51,209 WebSocket connection opened
2016-10-28 21:40:51,210 ServerConnection created
Select value changed from no1 to no2
2016-10-28 21:40:51,481 WebSocket connection closed: code=None, reason=None
2016-10-28 21:40:51,506 200 GET /?bokeh-session-id=6TOg0Ksc3oLaAUvyO0LNOyrni7wLzDagLkqa8QI8z194 (::1) 11.51ms
2016-10-28 21:40:51,974 WebSocket connection opened
2016-10-28 21:40:51,975 ServerConnection created

``

``

···

Am Freitag, 28. Oktober 2016 21:29:10 UTC+2 schrieb Daniel Krause:

Hello,

I tried to define a select widget and a handler / callback that is executed when the selected item is changed.

from bokeh.client import push_session
from bokeh.plotting import curdoc
from bokeh.layouts import layout, widgetbox
from bokeh.models.widgets import Select
import subprocess
import time

def select_handler(attr, old, new):
print(‘Select value changed from {} to {}’.format(old, new))

if name == ‘main’:
args = [‘python’, ‘-m’, ‘bokeh’, ‘serve’]
p = subprocess.Popen(args)
time.sleep(1)

select = Select(title="Select Test:", value="no1",
                options=["no1", "no2"])
select.on_change('value', select_handler)

layout = layout([[widgetbox(select)]], sizing_mode='scale_width')  
 
session = push_session(curdoc())
session.show(layout)

``

``

Result:

But the callback is not triggered when I change between ‘no1’ and ‘no2’.

Any help is appreciated.

Thanks

Daniel

Hi,

Whenever you are using "bokeh.client" and "push_session", that means that the app code is not running in the Bokeh server, it is running the outside process you started that uses "bokeh.client" If you want that process to handle callbacks, etc. then it has to exist and *still be running*. You must make a (blocking) call to session.loop_until_closed() to keep the client process alive so it can service callbacks. All of the examples in the repo that use "bokeh.client" and also have callbacks demonstrate this.

My real suggestion, however, is to write the app to be run directly on the bokeh serve, i.e. in the manner of "bokeh server myapp.py" this has (and always will have) a number of intrinsic benefits over using "bokeh.client".

Thanks,

Bryan

···

On Oct 28, 2016, at 2:45 PM, 'Daniel Krause' via Bokeh Discussion - Public <[email protected]> wrote:

Ok, I am a step further.

The handler / callback is working when I change the value of the select widget on the python side:

from bokeh.layouts import layout, widgetbox
from bokeh.models.widgets import Select
import subprocess
import time

def select_handler(attr, old, new):
    print('Select value changed from {} to {}'.format(old, new))

if __name__ == '__main__':
    args = ['python', '-m', 'bokeh', 'serve']
    p = subprocess.Popen(args)
    time.sleep(1)
    
    select = Select(title="Select Test:", value="no1",
                    options=["no1", "no2"])
    select.on_change('value', select_handler)
    
    layout = layout([[widgetbox(select)]], sizing_mode='scale_width')
     
    session = push_session(curdoc())
    session.show(layout)
    select.value = 'no2'

But it is not reacting to changes in the browser.

The console output:
2016-10-28 21:40:51,144 Starting Bokeh server version 0.12.3
2016-10-28 21:40:51,151 Starting Bokeh server on port 5006 with applications at paths ['/']
2016-10-28 21:40:51,151 Starting Bokeh server with process id: 7104
2016-10-28 21:40:51,209 WebSocket connection opened
2016-10-28 21:40:51,210 ServerConnection created
Select value changed from no1 to no2
2016-10-28 21:40:51,481 WebSocket connection closed: code=None, reason=None
2016-10-28 21:40:51,506 200 GET /?bokeh-session-id=6TOg0Ksc3oLaAUvyO0LNOyrni7wLzDagLkqa8QI8z194 (::1) 11.51ms
2016-10-28 21:40:51,974 WebSocket connection opened
2016-10-28 21:40:51,975 ServerConnection created

Am Freitag, 28. Oktober 2016 21:29:10 UTC+2 schrieb Daniel Krause:
Hello,

I tried to define a select widget and a handler / callback that is executed when the selected item is changed.

from bokeh.client import push_session
from bokeh.plotting import curdoc
from bokeh.layouts import layout, widgetbox
from bokeh.models.widgets import Select
import subprocess
import time

def select_handler(attr, old, new):
    print('Select value changed from {} to {}'.format(old, new))

if __name__ == '__main__':
    args = ['python', '-m', 'bokeh', 'serve']
    p = subprocess.Popen(args)
    time.sleep(1)
    
    select = Select(title="Select Test:", value="no1",
                    options=["no1", "no2"])
    select.on_change('value', select_handler)
    
    layout = layout([[widgetbox(select)]], sizing_mode='scale_width')
     
    session = push_session(curdoc())
    session.show(layout)

Result:

But the callback is not triggered when I change between 'no1' and 'no2'.

Any help is appreciated.

Thanks
Daniel

--
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/1e8fdf80-ad00-44f4-8f3c-8d55b1201dca%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Thanks for the clarification Bryan.

Daniel

···

Am 02.11.2016 12:04 vorm. schrieb “Bryan Van de Ven” [email protected]:

Hi,

Whenever you are using “bokeh.client” and “push_session”, that means that the app code is not running in the Bokeh server, it is running the outside process you started that uses “bokeh.client” If you want that process to handle callbacks, etc. then it has to exist and still be running. You must make a (blocking) call to session.loop_until_closed() to keep the client process alive so it can service callbacks. All of the examples in the repo that use “bokeh.client” and also have callbacks demonstrate this.

My real suggestion, however, is to write the app to be run directly on the bokeh serve, i.e. in the manner of “bokeh server myapp.py” this has (and always will have) a number of intrinsic benefits over using “bokeh.client”.

Thanks,

Bryan

On Oct 28, 2016, at 2:45 PM, ‘Daniel Krause’ via Bokeh Discussion - Public [email protected] wrote:

Ok, I am a step further.

The handler / callback is working when I change the value of the select widget on the python side:

from bokeh.layouts import layout, widgetbox

from bokeh.models.widgets import Select

import subprocess

import time

def select_handler(attr, old, new):

print('Select value changed from {} to {}'.format(old, new))

if name == ‘main’:

args = ['python', '-m', 'bokeh', 'serve']
p = subprocess.Popen(args)
time.sleep(1)
select = Select(title="Select Test:", value="no1",
                options=["no1", "no2"])
select.on_change('value', select_handler)
layout = layout([[widgetbox(select)]], sizing_mode='scale_width')
session = push_session(curdoc())
session.show(layout)
select.value = 'no2'

But it is not reacting to changes in the browser.

The console output:

2016-10-28 21:40:51,144 Starting Bokeh server version 0.12.3

2016-10-28 21:40:51,151 Starting Bokeh server on port 5006 with applications at paths [‘/’]

2016-10-28 21:40:51,151 Starting Bokeh server with process id: 7104

2016-10-28 21:40:51,209 WebSocket connection opened

2016-10-28 21:40:51,210 ServerConnection created

Select value changed from no1 to no2

2016-10-28 21:40:51,481 WebSocket connection closed: code=None, reason=None

2016-10-28 21:40:51,506 200 GET /?bokeh-session-id=6TOg0Ksc3oLaAUvyO0LNOyrni7wLzDagLkqa8QI8z194 (::1) 11.51ms

2016-10-28 21:40:51,974 WebSocket connection opened

2016-10-28 21:40:51,975 ServerConnection created

Am Freitag, 28. Oktober 2016 21:29:10 UTC+2 schrieb Daniel Krause:

Hello,

I tried to define a select widget and a handler / callback that is executed when the selected item is changed.

from bokeh.client import push_session

from bokeh.plotting import curdoc

from bokeh.layouts import layout, widgetbox

from bokeh.models.widgets import Select

import subprocess

import time

def select_handler(attr, old, new):

print('Select value changed from {} to {}'.format(old, new))

if name == ‘main’:

args = ['python', '-m', 'bokeh', 'serve']
p = subprocess.Popen(args)
time.sleep(1)
select = Select(title="Select Test:", value="no1",
                options=["no1", "no2"])
select.on_change('value', select_handler)
layout = layout([[widgetbox(select)]], sizing_mode='scale_width')
session = push_session(curdoc())
session.show(layout)

Result:

But the callback is not triggered when I change between ‘no1’ and ‘no2’.

Any help is appreciated.

Thanks

Daniel

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/1e8fdf80-ad00-44f4-8f3c-8d55b1201dca%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

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/AE571EB0-87C7-45EA-BB27-6724C7ACBAF8%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.