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):

        'Download Zipped Folder',

    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 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([

        'Download Zip',
        download = "",
        n_clicks = 0

    Output('download-zip', 'href'),
    [Input('download-zip', 'n_clicks')])

def generate_report_url(n_clicks):

	return '/dash/urldownload'


def generate_report_url():

	return send_file('', attachment_filename = '', as_attachment = True)

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

I am dealing with similar problem.
Did someone find solution to this problem with the original framework (the one that in the post)?
Alternative, did someone try using the solution suggested above? because it doesn’t work for me.

Anyone? sorry for the bouncing

Here is the way I download my zip of excel files, with the zip file being created on-the-fly. In this example, the individual excel files reside in my AWS instance.

NOTE: This entire process must be achieved within 30 seconds if hosting on Heroku due to the web process time limit; unless you implement this asynchronously.

The href assoc. with the button to invoke the download looks similar to the following:

Here is the code that handles the downloading of these files from my AWS instance, creating a zip to contain them, and then downloading to the user.

import flask
import io
import zipfile

def download_zip_file():
    # Name of the zip file
    filename = flask.request.args.get('filename')
    # Num of files to process
    num_files = flask.request.args.get('num_files')

    # All files passed in via url string are of the format file_<filename>
    files = []
    for file in range(int(num_files)):
        files.append(flask.request.args.get('file_' + str(file)))

    # Loop thru files and add to zip
    zip_buffer = io.BytesIO()
    with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED, False) as zip_file:
        for file in files:
            # is an internal function to download the file from my AWS S3 instance
            file_obj =
            zip_file.writestr(file.split('/')[-1], file_obj['Body'].read())

    return flask.Response(
        headers={"Content-Disposition": "attachment;filename={}".format(filename)}

Here is the function:

import boto3

# Download a file from the bucket
def download(key):
    # Create connection to the bucket
    s3_resource = boto3.resource('s3')
    bucket = s3_resource.Bucket(S3_BUCKET)

    # Return file object
    return bucket.Object(key).get()

Hope this helps and you can adapt to your needs.

The above solutions did not work for me. But I finally found a way to do this using dash_core_components.Download and dash_html_components.Button along with the normal Dash callbacks

Here is a reproducible example that downloads a zip folder with some dummy csv files in it.

import dash
from dash.dependencies import Output, Input
import dash_html_components as html
import dash_core_components as dcc
import os
import zipfile
import tempfile

# helper function for closing temporary files
def close_tmp_file(tf):

# app layout
app = dash.Dash(prevent_initial_callbacks=True)

app.layout = html.Div(
        html.Button("Download ZIP folder with dummy csv", id="btn_zip"),

# sample dataframes for zipping
import pandas as pd

df1 = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2, 1, 5, 6], "c": ["x", "x", "y", "y"]})
df2 = pd.DataFrame({"d": [6, 2, 3, 9], "e": [3, 2, 7, 8], "f": ["t", "v", "s", "b"]})
df_dict = {"df1":df1,"df2":df2}

# app callbacks

    Output("download-dataframe-zip", "data"),
    Input("btn_zip", "n_clicks"),
def func(n_clicks):
    zip_dict = {}
    # add dataframes to zip file using temporary files
    for name,df in df_dict.items():
        df_temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv')
        zip_dict[name] =

    zip_tf = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
    zf = zipfile.ZipFile(zip_tf, mode='w', compression=zipfile.ZIP_DEFLATED)
    for name,fn in zip_dict.items():


    # close uploaded temporary files
    [close_tmp_file(_tf) for _tf in zip_dict]

    return dcc.send_file(,filename="")

if __name__ == "__main__":