How to 'flush' server events

I am sorry for probably confusing title but I am not sure how to descibe it better. And that is also why I was not able to do much research prior to asking for help. I am sorry for that as well.

However, here is example app:

from bokeh.plotting import curdoc
from bokeh.models   import Button, Div, PreText
from bokeh.layouts  import row

def setBusyState(is_busy):['background-color'] = '#ff0000' if is_busy else '#0000ff'

def buttonClicked(event):
    pretext.text += "x"

def textChanged(attr, old, new):
    for i in range(int(1e8)): pass # this represents some demanding work on server side

div     = Div(text="div_text", style={'background-color': '#0000ff'})
pretext = PreText(text="x")
button  = Button(label="button_text")

pretext.on_change('text', textChanged)

layout = row([div, pretext, button])

The idea is following:

User will trigger some action (click on the button in this example). This action will eventually lead to some work on server side, before the server return some response (in reality it will manipulate ColumnDataSource and as consequence replot the data).

I would like to inform the user that the server is working and he/she should wait via parameters of (in this case) div element.

What I would expect is that the elemet changes background color to red immediatelly after the button is pressed and changes back to blue aftre the work is done. What I see is that the element (if ever) changes color for only very brief time interval. I expect it has something to do with how are the events on server side flushed to client side.

Anyway, could you point me to direction how to do those things right? Maybe there is some recommended way / framework how to show user that something is actully happening. BTW, this is actually part of a broader issue, where I would like to inform user not only about things happening on server side, but on client side as well - like that the renderes are being rendered (plot received new data and now are being reploted), that is other thing I am trying to solve.

Note: I understand that background color of that Div could be set directly by background property and that changing this attribute may propagate faster than just changing vaue in some dictionary. However I tried this also and the result is the same. I left the code as it is becuase that is how is my app written (and there is a reason to do so).

Thank you.

Bokeh state only synchronizes at the end of callbacks, so if you want updates sooner you will need to put the remaining work in a different callback that you schedule with add_next_tick_callback before exiting the current callback. See this SO answer for details:

Thank you!