Refreshing layout in multi page Dash app due to watchdog file event

111 Views Asked by At

I have a multi page Dash app.

app.py:

app = dash.Dash(__name__, use_pages=True, suppress_callback_exceptions=True)
...

I then have a page:

pages/resources.py:

dash.register_page(__name__, order=4, title='️ Resources')

Layout is defined as (note table which is a variable containing a HTML table):

def layout(**other_unknown_query_strings):
    return dbc.Container([... + table...])

I put an observer on a file (watchdog):

event_handler = FileChangeHandler()
observer = Observer()
observer.schedule(event_handler, path='resources_reserve.yaml', recursive=False)
observer.start()

Now in FileChangeHandler I change table and then want to trigger a refresh of the layout.

Is there any elegant way of achieving this?

1

There are 1 best solutions below

0
EricLavault On BEST ANSWER

Not sure if it's elegant but one idea would be to :

  1. Keep track of the table last modification timestamp (global variable updated in FileChangeHandler).

  2. In the layout, add :

    • A dcc.Store to keep track of the last time the layout refreshed.
    • A dcc.Interval component.
    • If you need to call layout() when refreshing, a container whose children is the output of that function.
  3. Add a callback with the interval component as input, the store data as state, and the layout container children as output: compare the (global) table-changed timestamp and the (client) layout-refreshed timestamp and refresh if necessary, otherwise raise PreventUpdate.