Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Multiple Download Links to CSV files

$
0
0

@spencer wrote:

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)

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6271

Latest Images

Trending Articles



Latest Images