Dash, Unable to download excel file on button click

Hi There,
I am new to the plotly dash, i have created a simple app in which when user dynamically inputs the excel input some pandas caliculations performed finally iam going to download the converted parsed and cleaned dataframe to excel with help of button click. but the data is not downloading with help of button iam getting errors on call back while clicking download button this is my script i can able to upload data dynamically and i can able to plot the graph and struggling with downloading file

import base64
import datetime
import io
import dash
from dash.dependencies import Input, Output, State
from dash import dcc
from dash import html
import plotly.express as px
import plotly.graph_objects as go
import dash_table
import pandas as pd

app = dash.Dash()

app.layout = html.Div([
html.H1(‘Convertion of FMS logdata to parced data’),

dcc.Upload(
    id='upload-data',
    children=html.Div([
        'Drag and Drop or ',
        html.B('Select Files')
    ]),
    style={
        'width': '100%',
        'height': '60px',
        'lineHeight': '60px',
        'borderWidth': '1px',
        'borderStyle': 'dashed',
        'borderRadius': '5px',
        'textAlign': 'center',
        'margin': '10px'
    },
    # Allow multiple files to be uploaded
    multiple=True
),

html.Div(id='output-data-upload'),
html.Div(
    [
        html.Button("Download CSV", id="btn_csv"),
        dcc.Download(id="download-dataframe-csv")])

])
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(‘,’)

decoded = base64.b64decode(content_string)
try:
    if 'csv' in filename:
        # Assume that the user uploaded a CSV file
        df = pd.read_csv(
            io.StringIO(decoded.decode('utf-8')))
    elif 'xls' in filename:
        # Assume that the user uploaded an excel file

        df2 = pd.read_excel(io.BytesIO(decoded), skiprows=8, usecols=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11])
    def func(n_clicks):
      return dcc.send_data_frame(df2.to_csv, "mydf.csv")

except Exception as e:
    print(e)
    return html.Div([
        'There was an error processing this file.'
    ])

return html.Div([
    html.P('Analog Input2 with Respect to Timestamp'),

    dcc.Graph(

    figure=px.scatter(df2, x="Speed", y="Longitude")

    ),

])

@app.callback(Output(‘output-data-upload’,‘children’),
Input(‘upload-data’, ‘contents’),
State(‘upload-data’, ‘filename’),
State(‘upload-data’, ‘last_modified’))
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
if name == ‘main’:
app.run_server(debug=True)

this is the output i got from the above script and data i used for this is the data i used rawdata - Google Sheets
thanks in advance

Hello @aparna1,

It’s really hard to understand what is going on here.

Could you please edit and use the </> button to wrap your code so we can copy and paste it?

1 Like

I saw this question on stackoverflow and it solved :smiley:

@aparna1 and @hoatran,

I’d shy away from the global use, what happens if two people are using it at the same time? They both upload and get their desired chart, but when they click download, only one person gets their desired file in return the other will get the other persons file.

I’d recommend that you return the fixed data, either in a dcc.Store or a dash_table. Then on your button click, you either pass the dcc.Store or the dash_table data back to the callback as a state.

Benefit of the dash_table is you can allow for automatically downloading of the table. :grin:

Yes,solved this question