How to access data stored in dcc.Store (browser memory) from flask rest api (request)

Hi I’m trying to use retrieve the data stored in dcc.Store from a flask api and then use it to edit a local file.

But I’m unable to retrieve the stored data using flask. Any help is appreciated.

Here I have made a 2 page dash app and give the 2nd page a pathname after some processing (here clicking buttons), based on this pathname (/check) I want my flask app to retrieve the data.

from flask import Flask

'''
make a 2 page app
on the 2nd page submit button should generate a link
on that page there should be dcc.Store
create a flask api to retrieve that data 
'''

from flask import Flask, request, json, jsonify


import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State


server = Flask(__name__)

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']


app = dash.Dash(__name__, server = server, external_stylesheets=external_stylesheets)
app.config['suppress_callback_exceptions'] = True


page_1_layout = html.Div([
    html.Div("Hi this is Page 1"),
    html.Button(id = 'submit-button',children='Submit')
])

page_2_layout = html.Div([
    html.Div("Hi this is Page 2"),
    dcc.ConfirmDialog(id='check'),
    html.Button(id='submit-button2',children='Submit'),
    dcc.Store(id='mem',data={'ponch':'raj'},storage_type='local')
])

app.layout = html.Div([
    dcc.Location(id='url',refresh=False),
    html.Div(id='page-content',children=page_1_layout)
])

@server.route('/check',methods=['POST','GET'])
def check_data():
    if request.method=='POST':
        data = json.loads(request.data)
        print(data)
        return jsonify(data)

@app.callback(
    Output('page-content','children'),
    [Input('submit-button','n_clicks')]
)
def test1(n_clicks):
    if n_clicks:
        return page_2_layout
    raise dash.exceptions.PreventUpdate


@app.callback(
    [Output('check','message'),
     Output('check','displayed')],
    [Input('submit-button2','n_clicks')]
)
def test2(n_clicks):
    if n_clicks:
        msg = 'yes or no'
        return msg,True
    raise dash.exceptions.PreventUpdate

@app.callback(
    [Output('url','pathname')],
    [Input('check','submit_n_clicks')]
)
def test3(snc):
    if snc:
        path = ['/check']
        return path
    raise dash.exceptions.PreventUpdate

if __name__=='__main__':
    app.run_server(debug=True, threaded=True)
1 Like