A nonexistent object was used in an `Output` of a Dash callback.?

I implemented what you suggested and it worked, thanks a lot for your help!

Hi,

I’m encountering this error in my app when passing information between two pages with dcc.Store.

See MRE below. The app works correctly, but the error is always shown.

I noticed that the culprit here is Output("store2", "data") in page1.py. If I remove it, the error disappears (but I need the 2nd store to be updated in my real app).

Is this a bug or am I doing something incorrectly?

Thanks.

app.py

from dash import Dash, page_container, html, dcc
import dash_bootstrap_components as dbc

app = Dash(
    use_pages=True,
)

def layout_func():
    return html.Div(
        [
            dcc.Store(id="store", data=None),
            dcc.Store(id="store2", data=None),
            dcc.Location(id="location", refresh="callback-nav"),
            dbc.Nav(
                children=[
                    dbc.NavLink("Page 1", href="/"),
                    dbc.NavLink("Page 2", href="/page2"),
                ],
            ),
            page_container
        ]
    )

app.layout = layout_func

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

page1.py

import dash
from dash import dcc, callback, Input, Output

dash.register_page(__name__, path="/")

def layout_func():
    return dcc.Dropdown(id="dropdown", options=[1,2,3])

layout = layout_func()

@callback(
    Output("dropdown", "value"),
    Output("store2", "data"),
    Input("store", "data"),
)
def update_dropdown(data):
    return [data, data]

page2.py

import dash
from dash import dcc, callback, Input, Output
from dash.exceptions import PreventUpdate

dash.register_page(__name__, path="/page2")

def layout_func():
    return dcc.Dropdown(id="dropdown2", options=[1,2,3])

layout = layout_func

@callback(
    Output("store", "data"),
    Output("location", "href", allow_duplicate=True),
    Input("dropdown2", "value"),
    prevent_initial_call=True,
)
def update_store(value):
    if value:
        return [value, "/"]
    else:
        raise PreventUpdate

Hi @valsorim
You might find this post above helpful: A nonexistent object was used in an `Output` of a Dash callback.? - #18 by AnnMarieW

Thanks for the reply, @AnnMarieW . I’ve read the posts above, however I still can’t wrap my head around why in my example the error (nonexistent dropdown) is raised with

@callback(
    Output("dropdown", "value"),
    Output("store2", "data"),
    Input("store", "data"),
)
def update_dropdown(data):
    return [data, data]

but not with

@callback(
    Output("dropdown", "value"),
    Input("store", "data"),
)
def update_dropdown(data):
    return data

Try preventing the first callback at startup. I would also use

            dcc.Store(id="store", data={}),
            dcc.Store(id="store2", data={}),

for initiation instead of None

Why do you call the layout_function on page1, but not on page2 or app.py?