plotly / plotly/dash

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

Open
#2,868 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3 sev-3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in a Jupyter notebook with the DiskcacheManager, then start at dash/long_callback/managers/diskcache_manager.py around line 179 where the callback runs. Compare the notebook callback's execution context with the imported modules, and verify that the callback can use imports such as time or sleep in both the Diskcache and Celery paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.