ReferenceError in multipage application

I have a very simple application as follows:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State


external_stylesheets = [
    'https://codepen.io/chriddyp/pen/bWLwgP.css', 
    'https://codepen.io/chriddyp/pen/brPBPO.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True


def generate_dropdown(id):
    values = [dict(label=x, value=x) for x in ['A', 'B', 'C']]
    return dcc.Dropdown(id=id, options=values, value=None) 


layout = html.Div([
    html.H5('TEST'),
    generate_dropdown('dropdown_1'),
    html.Div(id='dropdown_2_placeholder'),
    html.Div(id='dropdowns_value')
])
app.layout = layout


@app.callback(
    Output('dropdown_2_placeholder', 'children'),
    [Input('dropdown_1', 'value')])
def render_dropdown2(value):
    if value is not None:
        return generate_dropdown('dropdown_2')
    else:
        return None


@app.callback(
    Output('dropdowns_value', 'children'),
    [Input('dropdown_2', 'value')], 
    [State('dropdown_1', 'value')])
def render_dropdown_values(value2, value1):
    if value1 is not None and value2 is not None:
        return '%s %s' % (value1, value2)
    else:
        return None


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

Everything seems to be ok. Please notice that dropdown_2 is being created dynamically when dropdown_1 value changes and dropdown_2 is an input for render_dropdown_values callback - itā€™s dynamic thatā€™s the whole purpose of having suppress_callback_exceptions :wink:

Unfortunately when I try to use this app in a multi-page app (with dcc.Link & dcc.Location) i see a following error message in a browser console:
Uncaught (in promise) ReferenceError: An invalid input object was used in an `Input` of a Dash callback. The id of this object is `dropdown_2` and the property is `value`. The list of ids in the current layout is `[url, header-container, page-container, dropdown_1, dropdown_2_placeholder, dropdowns_value]` at dash_renderer.min.js:1 at Array.map (<anonymous>) at K (dash_renderer.min.js:1) at dash_renderer.min.js:1 at Array.forEach (<anonymous>) at dash_renderer.min.js:1 at <anonymous>
It seems that in this case dash_renderer is trying to invoke/validate all the callbacks defined in an sub-app.

[EDIT]:
In order to make it easier to reproduce I took an example of multipage app from here URL Routing and Multiple Apps | Dash for Python Documentation | Plotly and replaced page_2 layout with above:

import dash
import dash_core_components as dcc
import dash_html_components as html

print(dcc.__version__) # 0.6.0 or above is required

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

# Since we're adding callbacks to elements that don't exist in the app.layout,
# Dash will raise an exception to warn us that we might be
# doing something wrong.
# In this case, we're adding the elements through a callback, so we can ignore
# the exception.
app.config.suppress_callback_exceptions = True

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])


index_page = html.Div([
    dcc.Link('Go to Page 1', href='/page-1'),
    html.Br(),
    dcc.Link('Go to Page 2', href='/page-2'),
])

page_1_layout = html.Div([
    html.H1('Page 1'),
    dcc.Dropdown(
        id='page-1-dropdown',
        options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
        value='LA'
    ),
    html.Div(id='page-1-content'),
    html.Br(),
    dcc.Link('Go to Page 2', href='/page-2'),
    html.Br(),
    dcc.Link('Go back to home', href='/'),

])

@app.callback(dash.dependencies.Output('page-1-content', 'children'),
              [dash.dependencies.Input('page-1-dropdown', 'value')])
def page_1_dropdown(value):
    return 'You have selected "{}"'.format(value)


def generate_dropdown(id):
    values = [dict(label=x, value=x) for x in ['A', 'B', 'C']]
    return dcc.Dropdown(id=id, options=values, value=None) 


page_2_layout = html.Div([
    html.H5('TEST'),
    generate_dropdown('dropdown_1'),
    html.Div(id='dropdown_2_placeholder'),
    html.Div(id='dropdowns_value')
])


@app.callback(
    dash.dependencies.Output('dropdown_2_placeholder', 'children'),
    [dash.dependencies.Input('dropdown_1', 'value')])
def render_dropdown2(value):
    if value is not None:
        return generate_dropdown('dropdown_2')
    else:
        return None


@app.callback(
    dash.dependencies.Output('dropdowns_value', 'children'),
    [dash.dependencies.Input('dropdown_2', 'value')], 
    [dash.dependencies.State('dropdown_1', 'value')])
def render_dropdown_values(value2, value1):
    if value1 is not None and value2 is not None:
        return '%s %s' % (value1, value2)
    else:
        return None


# Update the index
@app.callback(dash.dependencies.Output('page-content', 'children'),
              [dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/page-1':
        return page_1_layout
    elif pathname == '/page-2':
        return page_2_layout
    else:
        return index_page
    # You could also return a 404 "URL not found" page here


if __name__ == '__main__':
    app.run_server(debug=True)
5 Likes

Also encountered this problemļ¼Œ In multipage, I have some components that are generated by callbackļ¼Œ but when I use this component as an input in a callback, It will cause this error. donā€™t know how to solve this.

4 Likes

Also having this issue ā€¦ my understanding was that in a dynamic layout, if components registered in a callback are not in the layout then the callback is not fired, yet in some cases it seems like the callback is trying to be fired anyway but since the component is not in the layout an error is raised:

Unhandled Promise Rejection: ReferenceError: An invalid input object was used in an `Input` of a Dash callback.
2 Likes

bump ! Any advice from dash community on this?

having the same issue - has anyone figured out what to do?

It ends up freezing the browser window.

Thank you

Hi,

I have the same problemā€¦
Did anyone find a solution yet?

I am also experiencing this issue. Has anyone figured out how to fix it?

While not a real solution to the problem (which i have not yet understood fully; it seems to happen only in some cases), i have come up with a workaround using the new Dash pattern matching callbacks in šŸ“£ Dash v1.11.0 Release - Introducing Pattern-Matching Callbacks. Letā€™s say that the problematic callback looks like this,

    @app.callback(Output(SITE_HEIGHT_CACHE, 'data'), 
                  [Input(SITE_HEIGHT_SLIDER, 'value')])
    def update_site_height(value):
        return value

with the error related to SITE_HEIGHT_SLIDER, which is a dynamically generated component. The error looks something like,

react_devtools_backend.js:6 ReferenceError: A nonexistent object was used in an Input of a Dash callback.

To silence the error, change the callback to accept willcards; since no-components-present is OK in a wildcard context, the error will disappear. The modified callback would be,

    @app.callback(Output(SITE_HEIGHT_CACHE, 'data'), 
                  [Input({"type": SITE_HEIGHT_SLIDER, "index": ALL}, 'value')])
    def update_site_height(value):
        if not value:
            raise PreventUpdate
        return value[0]
1 Like

@Emil, can this also be done with the output being a dynamically generated component?

For instance if I have a div callback registered to a global store, as below, can I use this wild card fix to not update that div if it has not been generated?

@app.callback(Output({'type': div, 'index': ALL}, 'children'), 
                  [Input('store', 'data')

When I do that in my app, I get the following javascript error

TypeError: Cannot read property ā€˜idā€™ of undefined

Yes, that should be possible. Note that the id of the component(s) that you are targeting should be defined as a dict also (i canā€™t see if this is what you have done based on the code posted).

I actually figured out the issue with the id and it was unrelatedā€¦all fixed on my previous TypeError. Iā€™m still getting the nonexistent object error which brought me to this fix based on your post in: Is suppress_callback_exceptions no longer supported in Dash v1.11.0

This error shows up in my browser window, but I noticed that it only applies in debug mode, as this error silently fails and everything works as intended when in production. I guess it is just a minor inconvenience for the benefits of pattern matching callbacks.

Regarding my use case, I have a multi-page design when the dcc.Location pathname creates a dynamic layout. Iā€™m guessing this is the main issue since I only get this ā€œnonexistent objectā€ error when the dcc.Location callback is triggered.

Full error text:

A nonexistent object was used in an Outputof a Dash callback. The id of this object is {"module":"ki","submodule":{"wild":"MATCH"}} with MATCH values ["hours_and_salaries"] and the property ischildren. The wildcard ids currently available are logged above.

oh, and I get three versions of this error, but I have four divs that should match the dash callback pattern. Not sure what is going on there, but thought it was peculiar enough to mention.

Hi all, Hi Emil,

I have a multi-page app and added tabs in one of the page. The id of the Graph in the non-selected tab (generated dynamically with a callback) is not recognized and generate the following error:

A nonexistent object was used in an `Output` of a Dash callback. The id of this object is ....

The callback reads data from a hidden div, does some data processing and return multiple figures to different Graphs objects in the layout.

How could I adapt the suggested solution / workaround to this case? I am using Dash 1.14

Thank you for your help

can you define ALL? I cannot understand the solution without seeing the rest of the code.

In answer to Nyckā€™s question and For future readers: It is necessary to import ALL (and/or MATCH and/or etc) from dash.dependencies, e.g.
from dash.dependencies import Input, Output, State, ALL