How to get data from Store object

Hi everyone,

As a lawyer interested in Python, I’ve been playing with Dash for a while and there is something I’ve been trying to achieve, with no results.

The short question is: How do I get data directly from the Store object without using the @app.callback operator? Is there a way to obtain the int() or str() from the Store(“memory”, “data”) object directly?


In my case, I have a Dropdown object that, depending on the selected option, the @callback Output returns a different amount of dcc.Inputs (a function is applied, assigning different id’s) as so:

def create_variables(content):

    layout = []

    for i in range(0, len(content)):
        layout.append(html.Br())
        layout.append(dbc.Row([dbc.Col(html.P(content[i]), width = 2),
                               dbc.Col(dcc.Input(placeholder=f"{content[i].replace('<', '').replace('>', '')}",
                                                 id=f"input-{i}", type="text")),
                               dbc.Col(html.Div(id=f"label-{i}"))]))

    layout.append(html.P(id="length"))

    layout_tuple = tuple(layout)

    return layout_tuple

The first @callback also Stores the amount (len) of dcc.Inputs in “memory”.

Next, the idea is for a user to enter data onto the newly created Inputs and, on the click of a button, process that information.

My problem is the following: since I have an undetermined number of Outputs and Inputs (as it varies with every option from the Dropdown object), I need my second @callback to be flexible. However, I’m having trouble creating a function that allows me to get the Stored data and use it in the second callback.

The code for the second @callback looks like this:

@app.callback(
    [*create_outputs(5)], #5 is the variable I need to change with the Store data
    [Input("crear-cto-unico", "n_clicks"),
    Input("memory", "modified_timestamp")],
    [State("memory", "data"),
     *create_states(5)])

The functions (create_outputs) and (create_states) look like this:

def create_outputs(length):

    outputs = []

    for i in range(0, length):
        outputs.append(Output(f'label-{i}', 'children'))

    outputs_tuple = tuple(outputs)    

    return outputs_tuple

I don’t know if it’s possible what I’m asking for, or perhaps there’s a better way to work this around.

I’d appreciate any feedback from your part and I’ll be glad to show more code or clarify some points if it wasn’t clear enough.

Many thanks, Rafael.

Hi Raffael,

I believe what you are looking for is pattern-matching callbacks. Your create_variables function would return Input objects with ids like dict(type="something", index=0), dict(type="something", index=1), … And your callback signature would be something like,

from dash.dependencies import ALL
...
@app.callback(
    [Input("crear-cto-unico", "n_clicks"),
    Input("memory", "modified_timestamp"),
    Input(dict(type="something", index=ALL), "property")],
    [State("memory", "data"), State(dict(type="something", index=ALL), "property"])

Does it make sense? :slight_smile:

1 Like

Thanks Emil! I’m still learning and I think this may solve my issue :slight_smile:

I still have to test it out, though, but it seems to be what I’m looking for.

Thanks a lot!