Force update of bokeh widgets?

后端 未结 1 537
长发绾君心
长发绾君心 2021-01-28 04:45

I am new to bokeh and writing a small bokeh server app, which has plot and a button. When the button is pressed, data is recalculated and plot updates. The idea is that as soon

相关标签:
1条回答
  • 2021-01-28 05:39

    Bokeh can send data updates only when the control is returned back to the server event loop. In your case, you run the computation without every yielding the control, so it sends all of the updates when the callback is already done.

    The simplest thing to do in your case is to split the callback into blocks that require synchronization and run each block in a next tick callback:

    def callback(arg):
        global variable
        global process_markup
    
        variable = not variable
    
        # change button style
        if variable:
            switchButton.update(label = 'Anticrossing ON',
                                  button_type = 'success')
        else:
            switchButton.update(label = 'Anticrossing OFF',
                                  button_type = 'default')
        # show "calculating..."
        process_markup.update(visible=True)
    
        def calc():
            # do long calculations
            x, y = calculate_data(variable)
            source.data = {"x":x, "y":y}
    
            # hide "calculating..."
            process_markup.update(visible=False)
    
        curdoc().add_next_tick_callback(calc)
    

    Note however that such a solution is only suitable if you're the only user and you don't need to do anything while the computation is running. The reason is that the computation is blocking - you cannot communicate with Bokeh in any way while it's running. A proper solution would require some async, e.g. threads. For more details, check out the Updating From Threads section of the Bokeh User Guide.

    0 讨论(0)
提交回复
热议问题