Hi comunity,
I’m currently facing a challenging but probably easy to solve problem. I have read many threads, tested many different codes, but I simply can’t solve my issue.
Context : I am writing a survey in python dash and I would like to record the answer. As the survey is quite long I use a multi pages approach (I provide a dummy code here).
the app.py looks like this:
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
import pandas as pd
app = dash.Dash(__name__, use_pages=True)
app.prevent_initial_callbacks='initial_duplicate'
app.layout = html.Div([
dbc.Container(
[dash.page_container],
fluid=True
),
dcc.Store(id='record_answers', data= {}}, storage_type='local')
])
if __name__ == "__main__":
app.run_server(debug=True)
The page 1 code is:
import dash
dash.register_page(__name__, path='/')
from dash import Dash, dcc, html, Input, Output, callback, State
layout = html.Div([
html.H1('Question 1'),
html.Label(children = 'How many unicorns have you seen in the past 5 years ?'),
html.Br(),
dcc.RadioItems(options=['0','1', '2', '3'], value = '0',id='1'),
html.Br(),
html.H1('Question 2'),
html.Label(children = 'How many vampires have you seen in the past 5 years ?'),
html.Br(),
dcc.RadioItems(options=['0','1', '2', '3'], value = '0', id='2'),
html.Br(),
dcc.Link('Next page', href='/page-2'),
html.H1(children='', id='toprint')
], style={'border': '2px solid black', 'padding': '20px'}
)
@callback(
Output(component_id='toprint', component_property='children'),
[Input(component_id='record_answers', component_property='data'),
Input(component_id='1', component_property='value'),
Input(component_id='2', component_property='value')],
prevent_initial_call=True
)
def update_dic(data, Q1, Q2):
data['Q1'] = Q1
data['Q2'] = Q2
return str(data)
Page 2 code is:
import dash
dash.register_page(__name__, path='/page-2')
from dash import Dash, dcc, html, Input, Output, callback, State
layout = html.Div([
html.H1('Question 3'),
html.Label(children = 'How many ETs have you seen in the past 5 years ?'),
html.Br(),
dcc.RadioItems(options=['0','1', '2', '3'], value = '0', id='3'),
html.Br(),
html.H1('Question 4'),
html.Label(children = 'How many Yetis have you seen in the past 5 years ?'),
html.Br(),
dcc.RadioItems(options=['0','1', '2', '3'], value = '0', id='4'),
html.Br(),
#dcc.Link('Next page', href='/page-3'),
html.H1(children='', id='toprint2')
], style={'border': '2px solid black', 'padding': '20px'}
)
@callback(
Output(component_id='toprint2', component_property='children'),
[Input(component_id='record_answers', component_property='data'),
Input(component_id='3', component_property='value'),
Input(component_id='4', component_property='value')],
prevent_initial_call=True
)
def update_dic(data, Q3, Q4):
data['Q3'] = Q3
data['Q4'] = Q4
return str(data)
Problem : I know this code is dummy but I provide it as a basis for further corrections. I have tested all the memory types in the dcc.Store(). I also tried to provide the dcc.Store data as Ouput in the different callbacks but nothing works properly.
In this specific case, I would like to get a final dictionary like : {‘Q1’ : 0, ‘Q2’ : 1, ‘Q3’ : 3, ‘Q4’ :, 0} at the end of page2.
Could you give me some help ? Thanks in advance !