plotly / dash-labs

Work-in-progress technical previews of potential future Dash features.
MIT License
139 stars 39 forks source link

Updating page_registry from a callback #85

Closed bigmike36c closed 2 years ago

bigmike36c commented 2 years ago

Hello, I think I found a bug when attempting to update the page_registry from a callback. The main issue I’ve been facing with this approach is the redirects do not seem to work. Inspecting the page_registry shows that the redirect_from parameter has the proper list of values (e.g. in the code below 'redirect_from': ['/callback-page']). But entering this in the url always returns the 404 page. However, when using the redirects with a page defined outside a callback, it works without issue.

I worked up a quick example demonstrating the issue.

import dash
from dash import html, Output, Input
import dash_labs as dl

import dash_bootstrap_components as dbc

app = dash.Dash(__name__, plugins=[dl.plugins.pages])
server = app.server

dash.register_page(
    "historical_analysis",
    path="/historical-analysis",
    redirect_from=["/test"],
    layout=html.Div(["Historical Analysis Page"]),
)
dash.register_page(
    "forecast",
    path="/forecast",
    layout=(html.Button("Button", id="button"), html.Div(id="content")),
)

app.layout = dbc.Container(
    [
        dbc.NavbarSimple(
            [
                dbc.NavItem(dbc.NavLink(page["name"], href=page["path"]))
                for page in dash.page_registry.values()
            ]
        ),
        dl.plugins.page_container,
    ]
)

@app.callback(
    Output(component_id="content", component_property="children"),
    Input(component_id="button", component_property="n_clicks"),
    prevent_initial_call=True,
)
def update_output_div(n_clicks):

    dash.register_page(
        "callback_page",
        path=f"/callback-page/{n_clicks}",
        redirect_from=["/callback-page"],
        layout=html.Div(["Callback Page"]),
    )
    return f"Output: {n_clicks}"

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

And here is a short video demonstrating the unexpected behavior: Screen Recording 2022-03-06 at 6 20 06 PM

My main use case for this is with dash enterprise's dashboard engine. When the user saves a dashboard, it creates a unique snapshot_id and I generate new pages for viewing and editing this dashboard.

Thanks in advance for any help!

AnnMarieW commented 2 years ago

Thanks for reporting @bigmike36c

This has been fixed in the dash PR https://github.com/plotly/dash/pull/1947#pullrequestreview-901967435

AnnMarieW commented 2 years ago

Hey @bigmike36c Thanks for closing this, but I think it's best to keep it open until we decide if dash-labs should be updated as well.

AnnMarieW commented 2 years ago

@bigmike36c I just wanted you to know that this issue will be closed and the change will be removed from the dash PR as well. Even though the change seemed to work locally, it would most likely fail in production in a multi-user environment -- unless (a) these updates, even though initiated by one user, are intended to apply to all users, and (b) you’ve done something to make it work cross-process, like sharing the updates to the other processes via redis.

Feel free to re-open if you have any more questions or comments.