Dcc.send_file then delete

In the app - a user clicks a button that performs processing on a temporary file, before being downloaded. I want to then delete the processed file (not to be confused with the downloaded file)

Code summarized below.

Kind regards
Gus

from dash import Dash, dcc, html, Input, Output
import os

app = Dash(__name__, )

app.layout = html.Div([
    html.Button(children='Submit', id='button_submit', n_clicks=0, style={'width':'10%'}, type='button'),
    dcc.Download(id="downloaded_file")
])

@app.callback(
    Output('downloaded_file', 'data'),
    Input('button_submit', 'n_clicks'),
    prevent_initial_call=True)
def update_output(n_clicks, youtube_link):

    if n_clicks > 0:

        # File processing occurs, and it is saved locally
        PROCESSED_FILE_LOCATION = '/path/'

        # User downloads the processed file
        return dcc.send_file(PROCESSED_FILE_LOCATION)

        # How may I delete the processed file located in PROCESSED_FILE_LOCATION


if __name__ == '__main__':
    app.run_server(debug=True)


Hello @GusMontano,

Welcome to the community!

Why do you want to save it locally and then delete it? You can actually write the file to memory and then use send_bytes to achieve the same end:

Good day @jinnyzor - thank you very much for the warm welcome and solution. Indeed, I believe this is the way to go by it! Everyday is a school day.

1 Like

That is also the approach I would recommend. Here’s a bit of background,

1 Like