Skip to content

[BUG] Long Callbacks: dash.no_update not supported with Celery/Redis implementations #1974

@sazzamac

Description

@sazzamac

Environment

python               3.9.7
dash                 2.3.0
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0

Describe the bug

Cannot return dash.no_update from a celery/redis implementation of Long Callbacks. Raises TypeError: Object of type NoUpdate is not JSON serializable (seen in celery logs).

Expected behavior

Can return dash.no_update as an Output, as in DiskLongCallbackManager.

Minimum Reproducible Example

import dash
from celery import Celery
from dash import html, no_update
from dash.dependencies import Input, Output
from dash.long_callback import CeleryLongCallbackManager

celery_app = Celery(broker='redis://0.0.0.0:6379/0', backend='redis://0.0.0.0:6379/0')
long_callback_manager = CeleryLongCallbackManager(celery_app)

application = dash.Dash(__name__, long_callback_manager=long_callback_manager)

application.layout = html.Div(
    [
        html.Div([html.P(id="paragraph_id", children=["Callback has not updated"])]),
        html.Button(id="button_id", children="Run Job!"),
        html.Div([html.P(id="no-update", children=["Testing_no_update"])]),
    ]
)


@application.long_callback(
    output=[Output("paragraph_id", "children"),
                   Output("no-update", "children")],
    inputs=Input("button_id", "n_clicks")
)
def callback(n_clicks):
    return [f"Clicked {n_clicks} times"], no_update


if __name__ == "__main__":
    application.run_server(debug=True)

In the above example we expect the app to render Clicked None times on initial load, as with DiskLongCallbackManager. Instead we see Callback has not updated i.e. the long callback has not ran.

Logged messages:

Traceback (most recent call last):
  File "celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "dash/long_callback/managers/celery_manager.py", line 134, in job_fn
    cache.set(result_key, json.dumps(user_callback_output, cls=PlotlyJSONEncoder))
  File "json/__init__.py", line 234, in dumps
    return cls(
  File "_plotly_utils/utils.py", line 59, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "_plotly_utils/utils.py", line 136, in default
    return _json.JSONEncoder.default(self, obj)
  File "json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type NoUpdate is not JSON serializable
[2022-03-15 16:41:23,112: INFO/MainProcess] Task long_callback_9c77f4d8f656d9655d05d7238e789c68040815b5[601c3d29-60a5-4dfe-8925-16da1ab020cc] received
[2022-03-15 16:41:23,116: ERROR/ForkPoolWorker-8] Task long_callback_9c77f4d8f656d9655d05d7238e789c68040815b5[601c3d29-60a5-4dfe-8925-16da1ab020cc] raised unexpected: TypeError('Object of type NoUpdate is not JSON serializable')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions