This is probably simple, but how can I get a callback to run when someone refreshes the page?
I had a previous app that once it was deployed on Docker wouldn’t refresh the current date until I put the function as a callback with dcc.Interval. Prior to dcc.Interval, it was only showing the current date when the container was built.
Now I’m working on a new app and we are hoping that we can run the SQL query only when someone opens the app or refreshes the page. Thoughts on this?
Hello @bpolasek,
You can do this by having the query associated with the layout as a function, upon the layout rendering, the query will rerun.
def layout():
df = getData() # this is your query function
return yourLayout with df
Now, if you are trying to have the query be stored for multiple people, say it is a costly query, you could have the above query run from a cache of the table and have a time interval that would update the cache accordingly.
Okay that makes sense. However I’m having trouble figuring out where it goes. I can’t put it in the div at the top because it throws an error so where would it go? I haven’t used the layout as a function yet so this is unfamiliar to me.
nav = navbar.navbar()
side = sidebar.sidebar_component()
spot = spotlight.spotlight_component()
def serve_layout():
layout = html.Div([
nav,
dbc.Container([
dbc.Row([
dbc.Col([
# sidebar
html.Div(
side,
id='sidebar'
)
],
width=2
),
dbc.Col([
# main area
dbc.Row([
spot,
spot,
spot,
spot
],
style={'display': 'flex', 'justify-content': 'space-around', 'margin-top': '70px'}
),
dbc.Row([
dcc.Graph(
figure={}
)
]),
dbc.Row([
# data table
])
],
width=10
)
],
className='g-0'
),
],
fluid=True,
style={'margin-left': '0px'},
)
])
return layout
app.layout = serve_layout
Okay maybe I got it. I put it in the table. This should allow it to auto-update on page refresh?
nav = navbar.navbar()
side = sidebar.sidebar_component()
spot = spotlight.spotlight_component()
df = pod.get_water_chem_data()
def serve_layout():
layout = html.Div([
# df,
nav,
dbc.Container([
dbc.Row([
dbc.Col([
# sidebar
html.Div(
side,
id='sidebar'
)
],
width=2
),
dbc.Col([
# main area
dbc.Row([
spot,
spot,
spot,
spot
],
style={'display': 'flex', 'justify-content': 'space-around', 'margin-top': '70px'}
),
dbc.Row([
dcc.Graph(
figure={}
)
]),
dbc.Row([
dash_table.DataTable(df.to_dict('records'), [{'name': i, 'id': i} for i in df.columns])
])
],
width=10
)
],
className='g-0'
),
],
fluid=True,
style={'margin-left': '0px'},
)
])
return layout
app.layout = serve_layout
You’ll need to requery the info (df) inside of the function as well.
Have it outside of the layout =
.
Ah yes makes sense. Thank you!
1 Like