Upload 2 spreadsheets and merge datasets in python dash

I have 2 spreadsheets containing lists of names (and other information in columns that I don’t care about). Essentially, I want to be able to upload both lists and output a single table that contains names that appear in both lists. This is my error message:

Traceback (most recent call last):
File “filename”, line 108, in update_output
children1 = [
File “filename”, line 109, in
parse_all_contents(c, n) for c, n in
File “filename”, line 59, in parse_all_contents
content_type, content_string = contents.split(‘,’)
ValueError: not enough values to unpack (expected 2, got 1)

Any suggestions would be greatly appreciated! Here’s my code:

import base64
import datetime
import io

import dash
from dash.dependencies import Input, Output, State
from dash import dcc, html, dash_table

import numpy as np
import pandas as pd

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

app = dash.Dash(name, external_stylesheets=external_stylesheets)

app.layout = html.Div([
dcc.Upload(
id=‘upload-list1-data’,
children=html.Div([
'Drag and Drop or ',
html.A(‘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=False
),
dcc.Upload(
id=‘upload-list2-data’,
children=html.Div([
'Drag and Drop or ',
html.A(‘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=False
),
html.Div(id=‘output-merged-data’),
])

def parse_list1_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
        df1 = pd.read_csv(
            io.StringIO(decoded.decode('utf-8')))
    elif 'xls' in filename:
        # Assume that the user uploaded an excel file
        df1 = pd.read_excel(io.BytesIO(decoded), sheet_name=0)
except Exception as e:
    print(e)
    return html.Div([
        'There was an error processing this file.'
    ])
return df1

def parse_list2_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
        df2 = 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), sheet_name=2)
except Exception as e:
    print(e)
    return html.Div([
        'There was an error processing this file.'
    ])
return df2

@app.callback(
Output(‘output-merged-data’, ‘children’),
Input(‘upload-list1-data’, ‘contents’),
State(‘upload-list1-data’, ‘filename’),
Input(‘upload-list2-data’, ‘contents’),
State(‘upload-list2-data’, ‘filename’)
)
def update_output(list_of_contents1, list_of_names1, list_of_contents2, list_of_names2):
if list_of_contents1 is not None and list_of_contents2 is not None:
children1 = [
parse_list1_contents(c, n) for c, n in
zip(list_of_contents1, list_of_names1)]
children1 = np.array(children1)
children1 = np.reshape(children1,(children1.shape[1],children1.shape[2]))
df1 = pd.DataFrame(children1, columns=[‘FIRM_ID#’,‘FIRM_NAME’])
children2 = [
parse_list2_contents(c, n) for c, n in
zip(list_of_contents2, list_of_names2)]
children2 = np.array(children2)
children2 = np.reshape(children2,(children2.shape[1],children2.shape[2]))
df2 = pd.DataFrame(children2, columns=[‘NO.’,‘ENTITIES’])
df1[‘FIRM_NAME’] = df1[‘FIRM_NAME’].str.upper()
df2[‘ENTITIES’] = df2[‘ENTITIES’].str.upper()
df = pd.merge(df1, df2, left_on=[‘FIRM_NAME’], right_on=[‘ENTITIES’])
return dash_table.DataTable(df.to_dict(‘records’), [{“name”: str(i), “id”: str(i)} for i in df.columns])

if name == ‘main’:
app.run_server(debug=True)

Hi @Solstice welcome to the forums.

Without having read all of your code, this error is pretty explicit and is not related to dash.

The string you are trying to split does not contain a “,”

The code in the plotly dash documentation is used to upload one file: Upload | Dash for Python Documentation | Plotly

When uploading one file, I was able to do this successfully (and modify the data) with my dataset before printing the resulting dataframe. The error printed above only comes up when I try to upload and modify two files in the same @callback.

I also found a similar (but more complicated?) problem on this forum without a complete solution: Upload 2 datasets, choose columns to merge on, get final merged output

Any idea why the same code works when uploading one file but fails when uploading two?