dash icon indicating copy to clipboard operation
dash copied to clipboard

[BUG] Error duplicate output callbacks when defining layout as a function

Open angel007sya opened this issue 5 years ago • 6 comments

Describe your context[X] I am developing a Dashboard using Dash in python 3.8.6 and PyCharm everything in .py files

  • replace the result of pip list | grep dash below [X]
dash                 1.18.1
dash-core-components 1.14.1
dash-html-components 1.1.1
dash-renderer        1.8.3
dash-table           4.11.1
jupyter-dash         0.3.1

Describe the bug [X]

When creating app layout and assign it to a function with app parameter, every callbacks fail saying that there are duplicate callbacks from same output but there aren't. It only fails with app callbacks

server.py:

import dash
from layout import layout # layout is where i define all layout logic/ components

def serve_layout():
    return layout(app)

app = dash.Dash(__name__)

app.layout = serve_layout
app.title = 'Example app'
if __name__ == "__main__":
    app.run_server(debug=True,
                   host='0.0.0.0')

NOTE: If I assign app.layout = layout(app) there aren't any errors and everything works fine. But, this is not an option because I want the app to reload the data every time user reloads the page.

Error:

In the callback for output(s):
  information-container.style
Output 0 (information-container.style) is already in use.
Any given output can only have one callback that sets it.
To resolve this situation, try combining these into
one callback function, distinguishing the trigger
by using `dash.callback_context` if necessary.

Expected behavior [X] The expected behaviour is to work without those app.callback duplicate output errors. NOTE: Graphs which don't have callbacks work okay, but graphs which have callbacks stop working.

angel007sya avatar Jan 05 '21 16:01 angel007sya

I have found the root of the error. The problem is that I am passing app as a parameter to my components. Let's say I have information.py component where there is a function named information that expects the app so it can use app.callbacks, doing that somehow is causing to conflict with duplicate output callback error

import dash_html_components as html
from dash.dependencies import Input, Output
import dash


def information(app):
    # hide/show modal
    @app.callback(Output('information-container', 'style'),
                  [Input('information-open-button', 'n_clicks',),
                   Input('information-close-button', 'n_clicks')])
    def manage_information_container(n_open, n_close):
        ctx = dash.callback_context
        if ctx.triggered:
            button_id = ctx.triggered[0]['prop_id'].split('.')[0]
            if button_id == 'information-open-button':
                return {"display": "block"}
            elif button_id == 'information-close-button':
                return {"display": "none"}

    return [  # modal div
        html.Div([  'SOME INFORMATION',
            html.Button('X',
                        id='information-close-button',
                        className='information-close-button')
        ],
            id='information-container',
            className='information-container',
            style={"display": "none"},
        ),
        html.Button('Información',
                    id='information-open-button',
                    className='information-open-button')
    ]

Is there any workaround for this to work. My app is structured like React components, so every component (html and dash ) is in a .py file. This make development easier when projects is big (my case).

angel007sya avatar Jan 05 '21 17:01 angel007sya

I've made a workaround by following documentation guidelines for multi paged apps https://dash.plotly.com/urls

angel007sya avatar Jan 05 '21 18:01 angel007sya

The workarounds suggested in the post unfortunately do not help when creating libraries of dash components which need to take app in order to define internal callbacks. They cannot import a statically defined app since they are defined in a library, and not in the end user application. This is quite a blocker!

reem avatar Feb 10 '21 22:02 reem

The workarounds suggested in the post unfortunately do not help when creating libraries of dash components which need to take app in order to define internal callbacks. They cannot import a statically defined app since they are defined in a library, and not in the end user application. This is quite a blocker!

@reem Maybe, could you pass app as a parameter to the function component?

Like this: app.layout = layout(app)

angel007sya avatar Feb 11 '21 08:02 angel007sya

Having the exact same problem as stated above. :( It seems to happen when I go to the page with the problem layout and callback and then go away from it and then come back to it. Like the figure is already set and now rejecting it trying to be set again.

justinalberta avatar May 06 '22 17:05 justinalberta

You should not define callbacks after the app started, if layout is a function it will be called on every page load. I'd suggest adding a setup_callbacks(app) function the user can call in the global scope.

T4rk1n avatar Mar 13 '23 18:03 T4rk1n