Solution for downloading a zipped folder/directory?

I’m looking to create a link to be able to download a zipped folder that already exists in the same directory as the dash application. I’ve seen solutions to download raw csv data (Download raw data), but am not quite sure how to change this to be able to download a .zip folder.

Here is the basic framework I’m trying to modify (from link above):

html.A(
        'Download Zipped Folder',
        id='download-link',
        download="folder.zip",
        href="",
        target="_blank"
    )

@app.callback(
    dash.dependencies.Output('download-link', 'href'),
    [dash.dependencies.Input('field-dropdown', 'value')])
def update_download_link(filter_value):
    dff = filter_data(filter_value)
    csv_string = dff.to_csv(index=False, encoding='utf-8')
    csv_string = "data:text/csv;charset=utf-8," + urllib.quote(csv_string)
    return csv_string

Thanks in advance!

What does the zip contain? One/many CSV files that you want to use in a dataframe?

The zip contains a variety of different files (.txt, .bedgraph, .bed) for the user to download after performing data analysis with the Dash App.

See Allowing users to download CSV on click for a more general solution for serving larger files and https://stackoverflow.com/a/27337047/4142536 to adapt that example to a zip file.

If you get this to work, please post your solution here too!

I was able to get this to work:

from flask import send_file
app = dash.Dash()

app.layout = html.Div([

	html.A(
        'Download Zip',
        id='download-zip',
        download = "example.zip",
        href="",
        target="_blank",
        n_clicks = 0
	    )
	])

@app.callback(
    Output('download-zip', 'href'),
    [Input('download-zip', 'n_clicks')])

def generate_report_url(n_clicks):

	return '/dash/urldownload'

@app.server.route('/dash/urldownload')

def generate_report_url():

	return send_file('example.zip', attachment_filename = 'example.zip', as_attachment = True)

if __name__ == '__main__':
	app.run_server(debug = True)
2 Likes
three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online