Skip to content

Infinite Looping using Store as an Input for a layout generator function #745

Closed

Description

@mmartinsky

Is your feature request related to a problem? Please describe.
Using a Store leads to Infinite loops in Callbacks.

I'm trying to create something like https://codepen.io/mau5FN/pen/FGAsa, in which the current state of the array [{name, year}] is stored in a dcc.Store.

I have a function using this store as an Input that generates the appropriate children for another element.

I'm running into an infinite loop where an update to either the name or year fields triggers an update to the Store data, which then triggers a re-render, which triggers the initial Store dropdown value update, and so on.

I can try to make a minimal repro later, but in essence:

app.layout

dcc.Store(id='store', data=[])
html.Div(children=[], id='generated-content')

generate callback

@app.callback(
Output('generated-content', 'children'),
[Input('store', 'data')]
)
def generate_layout(data):
    return [html.Div([
                    dcc.Dropdown(id=f'er-year-dropdown-{idx}', value=item['year']),
                    dcc.Button(id=f'er-add-button-{idx}'),
                    dcc.Button(id=f'er-remove-button-{idx}'),
                ]) for idx, item in enumerate(data)]
       # plus some code for filling out data up to the length for callback declaration purposes

update callback

@app.callback(
    Output("store", "data"),
    [
        Input(f"er-{key}-{idx}", value)
        for idx in range(5)
        for key, value in {
            "add-button": "n_clicks",
            "remove-button": "n_clicks",
            "year-dropdown": "value",
        }.items()
    ],
    [State("store", "data")],
)
def er_add_row(*args):
    ctx = dash.callback_context
    data = ctx.states.get("store.data")
    action = re.search("er-(.*)-\d+", ctx.triggered[0]["prop_id"]).group(1)
    idx = int(re.search("(\d+)", ctx.triggered[0]["prop_id"]).group(1))
    if action == "add-button":
        if ctx.triggered[0]["value"] is not None and len(data) < ITEMS_LIMIT:
            data.append({"year":2019 })
    elif action == "remove-button":
        if ctx.triggered[0]["value"] is not None:
            del data[idx]
    elif action == "year-dropdown":
        if (
            ctx.triggered[0]["value"] is not None
            and ctx.triggered[0]["value"] != data[idx]["year"]
        ):
            data[idx]["year"] = ctx.triggered[0]["value"]

    return data

Describe the solution you'd like
I would like a way to specify on a component a property that prevents the callback on render. Alternatively, maybe first class memoization support where it does not trigger the callback if no data has changed in any of the inputs

Describe alternatives you've considered
I tried memoization, but that only caches the return value rather than skipping the callback

Activity

  1. mmartinsky commented on May 24, 2019

    @mmartinsky
    Author
  2. changed the title [-][Feature Request][/-] [+]Infinite Looping using Store as an Input for a layout generator function[/+] on May 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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