Skip to content

[BUG] dcc.Patch() wipes persistence of sibling pattern-matching elements #3937

Description

@Aaron-Wrote-This

Sibling of: #3681
Environment

dash                 4.4.0

When patching a new element to list of elements with pattern matched ids, persistence is wiped from all sibling IDs.

Minimal example:

from dash import ALL, Dash, Input, Output, Patch, dcc, html, clientside_callback, callback

app = Dash(__name__)

def make_input(index):
    return dcc.Input(
        id={"type": "inp", "index": index},
        value="initial",
        persistence=True,
        persistence_type="local",
    )


app.layout = html.Div(
    [
        html.Button("add", id="add"),
        html.Button("clear", id="clear"),
        html.Div([make_input(0), make_input(1)], id="container"),
        html.Div(id="display"),
        html.Pre(id="storage"),
        dcc.Interval(id="local_storage_poll", interval=250),
        dcc.Input("Hello, I don't wipe", id='non-pattern-matching-id', persistence=True, persistence_type='local')
    ]
)


@callback(
    Output("container", "children"),
    Input("add", "n_clicks"),
    prevent_initial_call=True,
)
def add_input(n):
    patch = Patch()
    patch.append(make_input(n + 1))
    return patch


@callback(Output("display", "children"), Input({"type": "inp", "index": ALL}, "value"))
def show(values):
    return "|".join(values)


# Debug info, show local storage to make it easier to noticed when it's wiped
clientside_callback(
    """
    function display_persisted_vals() {
        return Object.keys(window.localStorage)
            .filter(k => k.startsWith('_dash_persistence.'))
            .map(k => k + ' = ' + window.localStorage.getItem(k))
            .join('\\n') || '(nothing persisted)';
    }
    """,
    Output("storage", "children"),
    Input("local_storage_poll", "n_intervals"),
)

# Debug helper, clear local storage to do a clean test
# (helpful after the fix when the storage doesn't wipe constantly, lol)
clientside_callback(
    """
    function clear_persistence() {
        Object.keys(window.localStorage)
            .filter(k => k.startsWith('_dash_persistence.'))
            .forEach(k => window.localStorage.removeItem(k));
        window.location.reload();
        return window.dash_clientside.no_update;
    }
    """,
    Output("clear", "n_clicks"),
    Input("clear", "n_clicks"),
    prevent_initial_call=True,
)


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

Here is a run of the minimal example app

Persistence works without a patch.
When a patch is applied persistence is wiped from siblings, but not from unrelated elements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions