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)