I am trying to run a background callback in my Dash app but I keep getting errors that modules already imported are not available, e.g.,
LongCallbackError Traceback (most recent call last)
LongCallbackError: An error occurred inside a long callback: name 'sleep' is not defined
Traceback (most recent call last):
File "[c:\Git\ia_lmd_utils\.venv\Lib\site-packages\dash\long_callback\managers\diskcache_manager.py](file:///C:/Git/ia_lmd_utils/.venv/Lib/site-packages/dash/long_callback/managers/diskcache_manager.py)", line 179, in run
user_callback_output = fn(*maybe_progress, *user_callback_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[C:\Users\********\AppData\Local\Temp\ipykernel_26488\2338839739.py](file:///C:/Users/********/AppData/Local/Temp/ipykernel_26488/2338839739.py)", line 423, in start_analysis
NameError: name 'sleep' is not defined
import time
import os
from dash import Dash, DiskcacheManager, CeleryManager, Input, Output, html, callback
if 'REDIS_URL' in os.environ:
# Use Redis & Celery if REDIS_URL set as an env variable
from celery import Celery
celery_app = Celery(__name__, broker=os.environ['REDIS_URL'], backend=os.environ['REDIS_URL'])
background_callback_manager = CeleryManager(celery_app)
else:
# Diskcache for non-production apps when developing locally
import diskcache
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)
app = Dash(__name__, background_callback_manager=background_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!"),
]
)
@callback(
output=Output("paragraph_id", "children"),
inputs=Input("button_id", "n_clicks"),
background=True,
running=[
(Output("button_id", "disabled"), True, False),
],
)
def update_clicks(n_clicks):
time.sleep(2.0)
return [f"Clicked {n_clicks} times"]
if __name__ == "__main__":
app.run_server(port=8885, jupyter_mode="external", debug=True)
which results in:
LongCallbackError Traceback (most recent call last)
LongCallbackError: An error occurred inside a long callback: name 'time' is not defined
Traceback (most recent call last):
File "[c:\Git\ia_lmd_utils\.venv\Lib\site-packages\dash\long_callback\managers\diskcache_manager.py](file:///C:/Git/ia_lmd_utils/.venv/Lib/site-packages/dash/long_callback/managers/diskcache_manager.py)", line 179, in run
user_callback_output = fn(*maybe_progress, *user_callback_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[C:\Users\********\AppData\Local\Temp\ipykernel_26488\3124709327.py](file:///C:/Users/********/AppData/Local/Temp/ipykernel_26488/3124709327.py)", line 36, in update_clicks
NameError: name 'time' is not defined
Please provide us your environment, so we can easily reproduce the issue.
I am trying to run a background callback in my Dash app but I keep getting errors that modules already imported are not available, e.g.,
I checked with an example from https://dash.plotly.com/background-callbacks and get the same error so I don't think it is my code.
which results in:
Please provide us your environment, so we can easily reproduce the issue.
Describe the bug
Background/long callback cannot find imported modules.
Expected behavior
I expect the previously imported modules to be available within the background/long callback.