Files
cief-dashboard/apps/upload_data.py
T
2020-09-23 10:33:26 +08:00

47 lines
1.3 KiB
Python

import base64
import io
from app import *
def load_data(contents):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in content_type:
# Assume that the user uploaded a CSV file
df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
df.to_csv('data/test0.csv')
elif 'xls' in content_type:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
return('file loaded')
except Exception as e:
print(e)
return('There was an error processing this file.')
layout = html.Div([
html.H1('Upload Data'),
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'}),
html.Div(id='output-data-upload'),
])
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')])
def update_output(contents):
if contents is not None:
children = load_data(contents)
return(children)