If the client side (browser) keeps open without refresh, and the app is deployed with changes and restarted, the client side is not aware of the changes, and then just fail or show blanks for some components. One solution is asking the client side to always refresh the browser after a deployment. Another solution here is periodically ping the server side and check if the version / timestamp is still the same as the one on the server, if not, automatically refresh the page in the browser.
The main component here is the dcc.Interval, which periodically pings the server, so a callback is required for handling the check.
The dcc.Location is for refreshing the page. When it's url property is sent with '/', it refreshes the whole page.
On the server side, it simply creates a server_version using current timestamp. The page also keeps a copy of the version timestamp in a Div element.
In the callback function, it compares the copy of the version with the curren t server, if not the same, simply send a '/' to the dcc.Location to refresh.
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
from datetime import datetime
app = dash.Dash(__name__)
# Set a global timestamp or version number on the server
# once created, never change it until restarted
server_version = str(datetime.now())
app.layout = html.Div([
# Location component to trigger page refresh
dcc.Location(id='url', refresh=True),
# Interval component to periodically check for version updates
dcc.Interval(
id='interval-component',
interval= 10 * 1000, # Check every 10 seconds
n_intervals=0
),
# Display / store the current server version
# this can be any element, e.g. label, datastore, etc.
# doesn't have to be visible
html.Div(id='server-version-display', children=f'Server version: {server_version}')
])
# Callback to check the server version and trigger page refresh
@app.callback(
Output('url', 'href'),
Output('server-version-display', 'children'),
Input('interval-component', 'n_intervals'),
State('server-version-display', 'children'),
)
def check_for_update(n_intervals, version):
# Compare the current version on client side with the version on the server side
if version != server_version:
# If the version has changed, refresh the page
return '/', f'Server version: {server_version}'
else:
return dash.no_update, f'Server version: {server_version}'
if __name__ == '__main__':
app.run_server(debug=True)