Div not updating immediately

Hi,

I have some data i/o that can take a while so I’m trying to implement a log and/or spinner to tell the user what is going on. However I’m finding that (when running from a button.onclick()) any updates I do to a div.text are only written after the whole callback function is finished.

I’ve included a minimal example where, on a button click, print to log, wait 3s, print to log. You can (should) see that the time strings are 3s apart (as they should be), but when you run it there is no change in the div for 3s then all output is written at once.

Is there something obvious I’m missing or something different I can do to get around this?

Using Bokeh 1.3.4 on Windows 10, anaconda Python 3.7.4

Thanks!
Jack

Example:
#test.py
import time
from datetime import datetime

from bokeh.models.widgets import Button, Div
from bokeh.io import curdoc
from bokeh.layouts import layout


def button_click():
    div.text = div.text + f"start waiting {datetime.now()}<br>"
    time.sleep(3)
    div.text = div.text + f"end waiting {datetime.now()}<br>"

div = Div(text="log<br>")

div_test_button = Button(label="Import Data")
div_test_button.on_click(button_click)

curdoc().add_root(layout(div_test_button, div))

Bokeh documents are only synced when the callback functions return. You should look at the use of next_tick_callback in the User Guide section Updating From Threads

Thanks Bryan, thought something like that could be the case, I’ll have a look.

Working perfectly, thanks a lot!

1 Like

Hi Jack, I have the same issue, could you please update your example code above with the next_tick_callback solution? Thanks in advance!

I just noticed your SO question - I flagged it as a duplicate of an already answered question. Follow the link there and you’ll get the answer with some example code.

Yes I saw and resolved my issue, many thanks!