Changes on widgets have no effect within a callback

Hi I have developed callback functions that take some time to complete.
I wanted to change the label of the button that activates them at the beginning the execution and then return it to the initial value at the end of the function.
The problem is that whatever action is taken within the callback no effect on curdoc () is obtained.
I attach an example
Can someone help me?
Mauro
import time
from functools import partial
from bokeh.layouts import row, layout
from bokeh.plotting import curdoc
from bokeh.models import Button
def changeLabel(button):
if button.label == ‘Do’:
button.label = ‘Stop’
else:
button.label = ‘Do’
print(“start time.sleep”)
time.sleep(10)
print(“stop time.sleep”)
return
buttonDo = Button(label=“Do”, button_type=“success”)
buttonDo.on_click(partial(changeLabel,button=buttonDo))
mainLayout=layout(row(buttonDo,name=“buttonDo”),sizing_mode=“scale_both”,name=“mainLayout”)
curdoc().add_root(mainLayout)

Hi @mauroDia please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

import time
from functools import partial
from bokeh.layouts import row, layout
from bokeh.plotting import curdoc
from bokeh.models import Button
def changeLabel(button):
if button.label == ‘Do’:
button.label = ‘Stop’
else:
button.label = ‘Do’
print(“start time.sleep”)
time.sleep(10)
print(“stop time.sleep”)
return
buttonDo = Button(label=“Do”, button_type=“success”)
buttonDo.on_click(partial(changeLabel,button=buttonDo))
mainLayout=layout(row(buttonDo,name=“buttonDo”),sizing_mode=“scale_both”,name=“mainLayout”)
curdoc().add_root(mainLayout)

Kind reagrds

import time
from functools import partial
from bokeh.layouts import row, layout
from bokeh.plotting import curdoc
from bokeh.models import Button
def changeLabel(button):
   if button.label == 'Do':
       button.label = 'Stop'
   else:
       button.label = 'Do'
   print("start time.sleep")
   time.sleep(10)
   print("stop time.sleep")
   return
buttonDo = Button(label="Do", button_type="success")
buttonDo.on_click(partial(changeLabel,button=buttonDo))
mainLayout=layout(row(buttonDo,name="buttonDo"),sizing_mode="scale_both",name="mainLayout")
curdoc().add_root(mainLayout)

kind regards

Sounds like you’re hitting this: https://stackoverflow.com/q/61212240/564509

Sorry

I have tryed but I have only errors......

import time
from functools import partial
from functools import partial
from bokeh.layouts import row, layout
from bokeh.plotting import curdoc
from bokeh.models import Button
def doChange(button):
    button.label = 'Stop'
    return
def changeLabel(button):
    global document
    print("start time.sleep")
    document.add_next_tick_callback(partial(doChange, button))
    time.sleep(10)
    print("stop time.sleep")
    return
buttonDo = Button(label="Do", button_type="success")
buttonDo.on_click(partial(changeLabel,button=buttonDo))

mainLayout=layout(row(buttonDo,name=“buttonDo”),sizing_mode=“scale_both”,name=“mainLayout”)
document=curdoc().add_root(mainLayout)

C:\Eclipse\eclipse-workspace\BiDia64>bokeh serve

C:\Eclipse\eclipse-workspace\BiDia64 diaBITest.py
2020-04-20 22:18:22,769 Starting Bokeh server version 2.0.1 (running
on Tornado 6.0.4)
2020-04-20 22:18:22,770 User authentication hooks NOT provided
(default user enabled)
2020-04-20 22:18:22,774 Bokeh app running at:
2020-04-20 22:18:22,774 Bokeh app running at:
2020-04-20 22:18:22,774 Starting Bokeh server with process id: 17128
2020-04-20 22:18:26,652 WebSocket connection opened
2020-04-20 22:18:26,653 ServerConnection created
start time.sleep
2020-04-20 22:18:28,805 error handling message
message: Message ‘PATCH-DOC’ content: {‘events’: [{‘kind’:
‘MessageSent’, ‘msg_type’: ‘bokeh_event’, ‘msg_data’: {‘event_name’:
‘button_click’, ‘event_values’: {}}}], ‘references’: }
error: AttributeError("‘NoneType’ object has no attribute
‘add_next_tick_callback’")
Traceback (most recent call last):
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\server\protocol_handler.py”,
line 90, in handle
work = await handler(message, connection)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\server\session.py”,
line 67, in _needs_document_lock_wrapper
result = func(self, *args, **kwargs)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\server\session.py”,
line 261, in _handle_patch
message.apply_to_document(self.document, self)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\protocol\messages\patch_doc.py”,
line 100, in apply_to_document
doc._with_self_as_curdoc(lambda:
doc.apply_json_patch(self.content, setter))
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\document\document.py”,
line 1150, in _with_self_as_curdoc
return f()
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\protocol\messages\patch_doc.py”,
line 100, in
doc._with_self_as_curdoc(lambda:
doc.apply_json_patch(self.content, setter))
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\document\document.py”,
line 398, in apply_json_patch
self._trigger_on_message(event_json[“msg_type”],
event_json[“msg_data”])
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\document\document.py”,
line 687, in _trigger_on_message
cb(msg_data)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\document\document.py”,
line 354, in apply_json_event
model._trigger_event(event)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\util\callback_manager.py”,
line 85, in _trigger_event
self._document._with_self_as_curdoc(invoke)
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\document\document.py”,
line 1150, in _with_self_as_curdoc
return f()
File
“C:\Python\Python_3_8_2_64\lib\site-packages\bokeh\util\callback_manager.py”,
line 72, in invoke
callback()
File “C:\Eclipse\eclipse-workspace\BiDia64\diaBITest.py”, line 14,
in changeLabel
document.add_next_tick_callback(partial(doChange, button))
AttributeError: ‘NoneType’ object has no attribute
‘add_next_tick_callback’
Can You help meeee.
Kind reagds
Mauro

I think you want:

curdoc().add_next_tick_callback(partial(doChange, button))

HI Bryan
now all works very fine.
Thanks YOU

1 Like