Hello All !,
Question from a real beginner…
I’m trying to use dcc.Store() to share data frame between two pages,
code for app.py:
app.layout = dbc.Container([
html.Div(
dcc.Store(id='data-store', data={})),
dbc.Row([
dbc.Col(
html.Div("Gdp -- Life exp Analysis",
style={'fontsize':100, 'textAlign': 'center', 'font-weight': 'bold'}))]),
html.Hr(),
dbc.Row([
dbc.Col([
sidebar],
xs=4,
sm=4,
md=2,
lg=2,
xxl=2),
dbc.Col([
dash.page_container],
xs=8,
sm=8,
md=10,
lg=10,
xxl=10)])
],
fluid=True)
if __name__ == '__main__':
app.run(debug=True)
Code first app, build the store component. This part works fine,
layout = html.Div([
html.H3('By continent, gdp -- life exp'),
html.Div(
dcc.Store(id='data-store',storage_type='local')
),
html.Div([
dbc.Row([
dbc.Col([
html.Br(),
html.H4('Table, from selected continent'),
html.Br(),
html.Br(),
dcc.Dropdown(id='dropdown', options=df['continent'].unique(), value='Europe'),
dash_table.DataTable(id='data-table',
page_size=10,
fixed_columns={'headers': True, 'data': 1})
]),
dbc.Col([
html.Br(),
html.H4('Graph, from selected continent'),
dcc.Graph(id='graph', figure={}),
]),
]),
]),
])
@callback(
Output(component_id='data-store', component_property='data'),
Input(component_id='dropdown', component_property='value'))
def fn_store(continent):
if not continent:
return df.to_dict('records')
df_continent = df[df['continent'] == continent]
return df_continent.to_dict(orient='records')
@callback(
Output(component_id='data-table', component_property='data'),
Input(component_id='dropdown', component_property='value'))
def fn_table(continent):
df_continent = df[df['continent'] == continent]
return df_continent.to_dict(orient='records')
@callback(
Output(component_id='graph', component_property='figure'),
Input(component_id='dropdown', component_property='value'))
def fn_graph(continent):
df_from_selected_continent = df[df['continent'] == continent]
fig = px.scatter(df_from_selected_continent,
x='gdpPercap',
y='lifeExp',
size='pop',
color='country',
hover_name='continent')
return fig
Code that does not return DataTable as I expect
layout = dbc.Container([
dbc.Row([
html.Div([
html.H1('full table'),
html.Hr()]),
html.Div([
dcc.Store(id='data-store'),
dash_table.DataTable(id='full-table', data=[], page_size=10)]),
]),
])
@callback(
Output('full-table', 'data'),
Input('data-store', 'data'))
def fn_full(data):
return data
There is no error message, but I do not get the table I expect,
Despite searches on the web, I do not see any useful information…
Could you help on this ?
Many thanks !