Share variables between 2 pages in dash application

This code located in pg2.py

def parse_contents_1(contents, filename, date, n_clicks_b):
    global nfile
    content_type, content_string = contents.split(',')
    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')), skiprows=7)
            nfile[filename] = df.to_dict('records')
        elif 'xls' in filename:
            # Assume that the user uploaded an excel file
            df = pd.read_excel(io.BytesIO(decoded), skiprows=7)
            nfiles[filename] = df.to_dict('records')
        # return nfile
    except Exception as e:
        print(e)
        return html.Div([
            'Yüklədiyiniz fayl formatı dəstəklənmir.'
        ])
    fName = filename.split('_')[0] + ' ' + filename.split('_')[1]
    return dbc.Col([html.Div([
        html.H5(children = [fName], id= {"type":"dynamic-name", "index": n_clicks_b }), dcc.Slider(id={"type":"id_1", "index": n_clicks_b },step=1)])], width = 5)




@callback([Output('output-dataBS', 'children'),
               Output('store-data-BS', 'data')],
              [Input('upload-data-BS', 'contents'),
               Input("dynamic-add-filter", "n_clicks"),
               State('upload-data-BS', 'filename'),
               State('upload-data-BS', 'last_modified'),
               State("output-dataBS", "children")])
def update_output(list_of_contents, n_clicks_b, list_of_names, list_of_dates, children_bs):
    if list_of_contents is not None:
        new_children = [
            parse_contents_1(c, n, d, n_clicks_b) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)]
        return children_bs.append(new_children), nfiles
    raise PreventUpdate





@callback(
    Output({'type': 'id_1', 'index': MATCH}, 'min'),
    Output({'type': 'id_1', 'index': MATCH}, 'max'),
    Output({'type': 'id_1', 'index': MATCH}, 'marks'),
    Input({'type': 'dynamic-name', 'index': MATCH}, 'children'),
    Input({"type":'store-data-BS', "index":MATCH}, "data"),
    Input("slider-date", 'value'),
    State({'type': 'store-data-BS', 'index': MATCH}, 'id'),
    State({'type': 'dynamic-name', 'index': MATCH}, 'id'),
)
def display_output(children_nm, t_data, d_value, id_bs, id_nm):
    for k, v in t_data.items():
        if "XXXXXXX" in k:
            dbts = pd.DataFrame.from_dict(v)
            gdfs = pgd.GeoDataFrame(dbts)
    snx = str(np.datetime64(unixToDatetime(d_value)))
    dr_df = gdf[gdf["Tarix"].astype('datetime64[D]') == snx]
    in_min = dr_df.index.min()
    in_max = dr_df.index.max()
    range_dt = range(in_min, (in_max + 1), 1)
    kl = pd.to_datetime(dr_df["Tarix"])
    mrk_t = getMarks_times(kl, range_dt)
    return in_min, in_max, mrk_t



But this code located in my app.py script

dcc.Store(id={"type":'store-data-BS', "index": pages.pg2.n_clicks_b},storage_type="session")

When I run my application and start to upload my data to the dcc.Store but

I get this error code:

AttributeError: module ‘pages.pg2’ has no attribute ‘n_clicks_b’

I think I can solve this error by creating class.

Please, if you have any solution for it, help me.

Thanks

Hi,

do you intent to use pattern matching callbacks?

1 Like

Yes, I want to apply Pattern-Matching Callbacks.

But

dcc.Store(id={"type":'store-data-BS', "index": pages.pg2.n_clicks_b},storage_type="session")

My dcc.Store located in app.py.

But my main callback located in pg2.py.

So I cannot get this “n_clicks_b” variable from pg2 to app.py

I don’t quite understand what you are trying to do.

The first function creates content dynamically, right?

On each click of a component you want the callback to output into a different dcc.Store?

1 Like

I upload data and store to dcc.Store and then I want load date and time data from my uploaded data to dcc.Sliders. This steps repeats when I upload new data.

In front side of my web application I have sliders with value of datetime. So when I upload new files (data) It increase of slider’s number.

For example

I upload file_1 it creates first slider (1) for itself, if I upload file_2 it will create a new slider (2) for itself.

Yes, but the dcc.Store is always the same, isn’t it?so you could give this store a fixed index and use ALL instead of MATCH here:

State({'type': 'store-data-BS', 'index': MATCH}, 'id'),

BTW, I’m on my phone so perhaps I’m missing something here…

1 Like

But I must give this variable pages.pg2.n_clicks_b to value of index key

dcc.Store(id={“type”:‘store-data-BS’, “index”: pages.pg2.n_clicks_b},storage_type=“session”)

OK, I’m sure I’m missing something. Try assigning 0 to the index of the store and use ALL for the input/output/state addressing this store.

1 Like

Ok,thank you

I will try

But can you give me any suggestion for learning this relations about in background of working variables in web application. In dash documentation wasn’t written in detail