plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.25k stars 2.05k forks source link

Background/Long callback does not have access to imported modules when running inside jupyter notebook #2868

Open akunihiro opened 4 months ago

akunihiro commented 4 months ago

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

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.

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.

async-dash                        0.1.0a1
dash                              2.16.1
dash_ag_grid                      31.2.0
dash-bootstrap-components         1.6.0
dash_canvas                       0.1.0
dash-core-components              2.0.0
dash-dndkit                       0.0.3
dash-extensions                   1.0.15
dash-html-components              2.0.0
dash-iconify                      0.1.2
dash-leaflet                      1.0.15
dash-loading-spinners             1.0.3
dash-mantine-components           0.12.1
dash-paperdragon                  0.1.0
dash-resizable-panels             0.1.0
dash-svg                          0.0.12
dash-table                        5.0.0
dash_treeview_antd                0.0.1

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.