✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Multiple Download Links to CSV files

Having an issue setting up links that download csv files. The code snippet that is shown below works for the “forecast” download just fine, however, when adding any additional links with the same logic just different id’s and functions they do not work. Is there something I’m missing here?

Inputs are coming from hidden DIVs with JSON data.

@app.callback(
    Output('export_sales', 'href'),
    [Input('sales_base_cache', 'children')])
def update_sales_link(value, pathname):
    return '/sales/url_to_download?value={}'.format(value)


@app.server.route('/sales/url_to_download')
def download_sales():
    value = flask.request.args.get('value')
    data = pd.read_json(value, orient='split')
    str_io = io.StringIO()
    data.to_csv(str_io)
    mem = io.BytesIO()
    mem.write(str_io.getvalue().encode('utf-8'))
    mem.seek(0)
    str_io.close()
    return flask.send_file(mem, mimetype='text/csv', attachment_filename='sales.csv', as_attachment=True)


@app.callback(
    Output('export_forecast', 'href'),
    [Input('forecast_cache', 'children')])
def update_forecast_link(value):
    return '/forecast/url_to_download?value={}'.format(value)


@app.server.route('/forecast/url_to_download')
def download_forecast():
    value = flask.request.args.get('value')
    data = pd.read_json(value, orient='split')
    str_io = io.StringIO()
    data.to_csv(str_io)
    mem = io.BytesIO()
    mem.write(str_io.getvalue().encode('utf-8'))
    mem.seek(0)
    str_io.close()
    return flask.send_file(mem, mimetype='text/csv', attachment_filename='forecast.csv', as_attachment=True)

Initial app setup below:

server = Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], server=server)
app.config.suppress_callback_exceptions = True