Hi community,
i’m trying to upload an xls file, showing it’s contents in a dataTable object.
I found a dash recipe on github:
[dash-recipes/upload-data-to-table.py at master · plotly/dash-recipes · GitHub]
But i’m not able to edit/fix that code.
This is my app code, but it doesn’t work. ( i can’t show the table)
# file upload function
def parse_contents(contents, filename):
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
df = pd.read_excel(io.BytesIO(decoded))
elif 'xlsx' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return None
return df
# callback table creation
@app.callback(Output('table', 'data'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename')])
def update_output(contents, filename):
if contents is not None:
df = parse_contents(contents, filename)
if df is not None:
return df.to_dict('records')
else:
return [{}]
else:
return [{}]
This is my layout code :
html.Div([
html.H5("Upload Files"),
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
multiple=False),
html.Br(),
html.H5("Updated Table"),
html.Div(dt.DataTable(data=[], id='table'))
])
])
What’s wrong with my code?
Thanks