Hi,
I am a newbie in Dash/programming so please be patient. I want to create an app where the user upload two csv-s (in a predefined form), then I apply some transformation to it and then the user can download this as a csv file.
This is how the callbacks look like:
Upload file func
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')),sep=';',header=0)
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded),sep=';',header=0)
except Exception as e:
print(e)
return None
return df
Upload first .csv
@app.callback(Output(‘file1-output’, ‘data’),
[Input(‘file1-upload’, ‘contents’)],
[State(‘file1-upload’, ‘filename’)])
def update_outputtab1(contents, filename):
if contents is not None:
df = parse_contents(contents, filename)
return df
else:
return [{}]
Upload second .csv
@app.callback(Output(‘file2-output’, ‘data’),
[Input(‘file2-upload’, ‘contents’)],
[State(‘file2-upload’, ‘filename’)])
def update_outputtab2(contents, filename):
if contents is not None:
df = parse_contents(contents, filename)
return df
else:
return [{}]
create new file
@app.callback(Output(‘new-file’, ‘data’),
[Input(‘file1-output’, ‘data’),
Input(‘file2-output’, ‘data’),
Input(‘submit-mp’, ‘n_clicks’)])
def create_mpfile(file1, file2, n_clicks):
if n_clicks is None:
raise PreventUpdate
else:
res = create_new(file1, file2) # returns a pd.DataFrame
return res
Until this point I think it is working as intended, but when I want to add a download link:
@app.callback(
Output(‘download-link’, ‘href’),
[Input(‘new-file’, ‘data’)])
def update_download_link(dff):
csv_string = dff.to_csv(index=False, encoding=‘utf-8’)
csv_string = “data:text/csv;charset=utf-8,%EF%BB%BF” + urllib.parse.quote(csv_string)
return csv_string
It gives “Error loading dependencies”. Could you please help me with this error?
Thank you in advance!