Downloading data refreshed by intervals

I have a Dash app featuring a dcc.Graph in a dbc.Container (dbc for dash_bootstrap_components) which is updated every 2 min through dcc.Interval. I’d like users to be able to download the data in the chart. I tried to deliver this functionality using an html.Button and dcc.Download and it works (data downloaded upon button clicking) but I get an undesired behavior: every time layout is updated so is the data and that triggers that I get a file downloaded even if button is not clicked; instead, the download should only happen upon clicking the button.

The app consists of one callback that queries data and refreshes layout and outputs data to be downloaded; all based on (only) interval input. Then another callback allows data download by button clicking. To make the data available to the second callback, dcc.Store is used in first callback which outputs the data as JSON. Then the second callback has the button clicking and the data from the first callback as inputs.

I believe the issue that I’m getting is described in the following link (to note, I do have prevent_initial_call=True ), but cannot seem to find the solution for it…

First callback:

Second callback:

Your callback is triggering because the “pred_data” input is changing; thus, that callback is getting triggered and the data is getting downloaded

So you were right to say your issue/solution is in this link below:

Scroll down to " Determining which Input Has Fired with dash.callback_context"

Use that example as a guide for how to structure the “update_download_data” callback

  1. Use the dash.callback_context
  2. use “if” statement relying on callback context

Something like this:

ctx = dash.callback_context
button_id = ctx.triggered[0]['prop_id'].split('.')[0]

if button_id == "btn_xlsx":
# do download

# return nothing / do nothing

I implemented what @matt1 suggested and it works! Thank you!!!

1 Like