Dash-extensions, Serverside(), cancel of background callback

Hi there,

I have a question concerning Serverside() in combination with background callbacks.

I can’t cancel the background callback of following MRE if using DashProxy(), I suspect that is due to the use of diskcache but I’m not sure. Maybe my setup is wrong.

Could someone perhaps shed some light? Thanks in advance!

import time
from dash_extensions.enrich import DashProxy, Input, Output, ServersideOutputTransform, Serverside
from dash import Dash, html, dcc, DiskcacheManager
import dash_bootstrap_components as dbc
import diskcache


# dummy class
class Model:
    def __init__(self, _id, _type):
        self._id = _id
        self._type = _type

    def to_json(self):
        return f'Model_{self._id}'


# setting up the cache
cache = diskcache.Cache("./cache")

app = DashProxy(
    __name__,
    external_stylesheets=[dbc.themes.BOOTSTRAP],
    background_callback_manager=DiskcacheManager(cache),
    transforms=[ServersideOutputTransform()]
)

# app = Dash(
#     __name__,
#     external_stylesheets=[dbc.themes.BOOTSTRAP],
#     background_callback_manager=DiskcacheManager(cache),
# )

app.layout = html.Div(
    [
        dcc.Dropdown(
            id='model_selector',
            options=[
                {'label': 'UNET', 'value': 'unet'},
                {'label': 'VGG16', 'value': 'vgg'},
                {'label': 'k-means', 'value': 'kmeans'},
            ],
            placeholder='Select model type...',
        ),
        html.Button(
            'cancel',
            id='btn_cancel',
        ),
        dcc.Loading(
            dcc.Store(
                id='model',
                storage_type='session'
            )
        )
    ]
)


@app.callback(
    output=[
        Output("model", "data"),
    ],
    inputs=[
        Input("model_selector", "value")
    ],
    background=True,
    cancel=[
        Input("btn_cancel", "n_clicks"),
    ],
    prevent_initial_call=True
)
def update_progress(model_selection):
    model = Model(1, model_selection)
    time.sleep(5)
    # return model.to_json()
    return Serverside(model)


if __name__ == '__main__':
    app.run(debug=True)

I have encountered the same problem … can anyone let us know whether we could use background_callback_manager with DashProxy?

Hey @MillieWoo, welcome to the forums.

I got the information from the author of dash-extensions, that he’s not using the background-callbacks so he can’t comment on that.

In my case I was just interested whether this is possible without a real use case. This might be different in your case, though.

Thx for the feedback! I looked up this post … but I am not familiar with CeleryManager …