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 …

Hi! I am wondering the same thing. Did you get any update as to whether DashProxy is compatible with background callbacks?

Hey @tcharland13 , unfortunately I did not get any further information on that.

I actually interpreted the answer from back then as: “it’s possible that this does not work, if I find the time I look into it”

Since he is not using background callbacks anyways, I think the chances are quite low that this gets addressed :wink:

I see, thanks!

1 Like

At the top of my head, I think the serialization step would need to be moved to the background processing step. But I havent had the time/need to look into it yet :slight_smile:

1 Like

I was trying to implement a progress bar for large file unzipping that I store in Serverside(). I ended up splitting the unzipping step into batches of files with one callback per batch to update the progress bar. The use of background callbacks would definitely simplify this process, but it is not necessary!