Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
akunihiro opened this issue May 23, 2024 · 0 comments

Comments

@akunihiro
Copy link

akunihiro commented May 23, 2024

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.

@T4rk1n T4rk1n changed the title [BUG] Background/Long callback does not have access to imported modules [BUG] Background/Long callback does not have access to imported modules when running inside jupyter notebook. May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants