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)
thanks in advance