☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

How to cancel calculation with button

Community Support
TaimoPriinits
TaimoPriinits

Hello

I am trying to add a button which would cancel calculation but while the calculation function is running the cancel button is not working (the cancel_calculation_button_callback is not triggering).

def calculate_timesteps():
    for element in elements:
        if cancel_calculation_button.disabled==False:
            //data manipulation to get dictionary elements
    return data

calculate_data_button = Button(label="Calculate data", width=200)
cancel_calculation_button = Button(label="Cancel calculation", width=200, disabled=False, button_type="danger")
data = calculate_timesteps()
cancel_calculation_button.disabled=True
source = ColumnDataSource(data=data)

def calculate_data_button_callback():
    cancel_calculation_button.disabled=False
    calculate_data_button.label = "Loading..."
    calculate_data_button.button_type = "primary"
    calculate_data_button.disabled=True
    def do_calculation():
        data = calculate_timesteps()
        source.data = data
        calculate_data_button.label = "Calculate data"
        calculate_data_button.button_type = "default"
        calculate_data_button.disabled=False
        cancel_calculation_button.disabled=True
    curdoc().add_next_tick_callback(do_calculation)
        
def cancel_calculation_button_callback():
    cancel_calculation_button.disabled=True

calculate_data_button.on_click(cancel_calculation_button_callback)
calculate_data_button.on_click(calculate_data_button_callback)

layout = column(
    calculate_data_button,
    cancel_calculation_button,
)

curdoc().add_root(layout)

How could I make it work?
I guess, I should somehow use add_periodic_callback but I am not really sure how to implement it in the for loop.
Thanks.

⌁   Link to post
Bryan
Bryan

You will probably need to put the long computation into a separate thread, so that the main IOLoop itself is not blocked by it (and also so that there is “something” to cancel).

⌁   Link to post

End of archived discussion · Replies are disabled