Dash Long Callbacks Not Working

kev
kev Registered Posts: 2

Hi all,

I'm struggling to get long callbacks to work in Dataiku. When I initalize the app = dash.Dash() instance, the application does not run at all. When I remove it, the application runs but the callback does not work at all. Currently, it only works with a regular callback but I need it to work with a long callback. Thanks in advance!

Best,

import time
import dash
from dash import html, Input, Output, callback
from dash.long_callback import DiskcacheLongCallbackManager
# import dataiku

## Diskcache
import diskcache
import logging

### python-lib function
from long_callback_function import long_callback_function

# logger = logging.getLogger(__name__)


cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(cache) app = dash.Dash(__name__, long_callback_manager=long_callback_manager)




app.layout = html.Div(
[
html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
html.Button(id="button_id", children="Run Job!"),
html.Button(id="cancel_button_id",children="Cancel Running Job")
]
)


@app.long_callback(
Output("paragraph_id", "children"),
Input("button_id", "n_clicks"),
manager=long_callback_manager,
running = [
(Output("button_id","disabled"),True,False),
(Output("cancel_button_id","disabled"),False,True),
],
cancel=[Input("cancel_button_id","n_clicks")],
)
def long_callback(n_clicks):

return long_callback_function(n_clicks) ######################### Long callback function import time

def long_callback_function(n_clicks):
time.sleep(10)
return [f"Clicked {n_clicks} times PLEASE"]

Comments

  • Sarina
    Sarina Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 319 Dataiker

    Hi @kev,

    Thank you for your question about using long callbacks within a dash app! You'll want to use the following structure utilizing project libraries to define your long callbacks.

    So in your webapp, you'll define your diskcache like so:

    import diskcache
    cache = diskcache.Cache(".")
    long_callback_manager = DiskcacheLongCallbackManager(cache)

    Now, in your project library you'll define your callback code instead of in your webapp code  (as Python self-inspection capabilities require the code to be in a full-fledged file) like so:

    Then, in your webapp you can call the project library callback code. Here's a full example.

    [webapp code]

    import time
    import dash
    from dash import html, Input, Output
    from dash.long_callback import DiskcacheLongCallbackManager
    from my_dash.callbacks import define_callback

    import diskcache
    cache = diskcache.Cache(".")
    long_callback_manager = DiskcacheLongCallbackManager(cache)

    app.layout = html.Div(
    [
    html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
    html.Button(id="button_id", children="Run Job!"),
    ]
    )

    define_callback(app, long_callback_manager)

    Project library code stored in a folder named my_dash and a file named callbacks.py [project library (lib > python > my_dash > callbacks.py)]

    import time
    import dash
    from dash import Input, Output

    def define_callback(app, long_callback_manager):
    @app.long_callback(
    output=Output("paragraph_id", "children"),
    inputs=Input("button_id", "n_clicks"),
    manager=long_callback_manager,
    )
    def callback(n_clicks):
    time.sleep(20)
    return [f"Clicked {n_clicks} times"]

    I hope that's helpful!

    Thank you,
    Sarina

  • kev
    kev Registered Posts: 2

    It works, thank you Sarina!