Hello. I’m new here and having trouble with the dcc.Upload component.
I would like to make it so that I can use the data in the uploaded file in a callback that performs calculation for another datatable where the user inputs data. The calculations work perfect for the input data but do not work on the uploaded data to datatable. I suspect the values are being read as strings? Though, when I try to convert, I get errors about Nans not being able to convert to integers. How would I go about converting specific columns to integers?
Note: I needed to make the column names standard no matter what the user uploads in order for calculations to work. I am assuming col order in uploads will be same. This is why I am taking row values from upload and putting it into df.
Here is my code:
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
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))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
newdf= pd.DataFrame(df1)
num_cols = len(newdf.columns.values)
num_rows = len(newdf.index)
for i in range( num_cols ):
df.iloc[:num_rows,i] = newdf.iloc[:,i]
idx = np.where( df.iloc[:,0] == '\"\"' )[0]
df.drop(idx, inplace=True)
df['Entity']=df['Entity'].str.upper()
df.loc[df['Asset Type'].str.contains('alts'), 'Asset Type'] = 'Equity/Alts'
df.loc[df['Asset Type'].str.contains('equity'), 'Asset Type'] = 'Equity/Alts'
df.loc[df['Asset Type'].str.contains('credit'), 'Asset Type'] = 'Credit ex Structured'
df.loc[df['Asset Type'].str.contains('structured'), 'Asset Type'] = 'Structured'
df['Asset Class']=df['Asset Class'].str.title()
df.loc[df['Asset Type'] == 'Equity/Alts', 'Tenor']='None'
df.loc[df['Asset Type'] == 'Equity/Alts', 'Rating']='None'
# df['Net Investment Income (% pts)']= df['Net Investment Income (% pts)'].astype('int64')
# df['EL Override (% pts)']= df['EL Override (% pts)'].astype(int)
# df['1in10 UL Override (% pts)']= df['1in10 UL Override (% pts)'].astype(int)
# df['Base RBC Override (% pts)']= df['Base RBC Override (% pts)'].astype(int)
# df['1in10 RBC Override (% pts)']= df['1in10 RBC Override (% pts)'].astype(int)
return dash_table.DataTable( id='uploadtable',
data= df.to_dict('records'),
columns=[{'name': i, 'id': i} for i in df.columns],
page_action = 'none',
style_table = {'height':'250px','overflowY':'auto'},
editable=True,
style_header_conditional= necessary + optional,
style_cell = {'textAlign':'center', 'minWidth': '150px', 'maxWidth':'250px', 'Width':'100px', 'font-family': 'PrudentialModern'},
fixed_rows={'headers': True, 'data': 0},
css=[{"selector": ".Select-menu-outer", "rule": "display: block !important"}]
)
#import
@CAD.callback(Output('output-to-data-upload', 'children'),
Input('upload-to-data', 'contents'),
State('upload-to-data', 'filename'),
)
def update_to_output(to_list_of_contents, to_list_of_names):
if to_list_of_contents is not None:
children = [
parse_contents(c, n) for c, n in
zip(to_list_of_contents, to_list_of_names)]
return children
@CAD.callback(Output('output-from-data-upload', 'children'),
Input('upload-from-data', 'contents'),
State('upload-from-data', 'filename'),
)
def update_from_output(from_list_of_contents, from_list_of_names):
if from_list_of_contents is not None:
children = [
parse_contents(c, n) for c, n in
zip(from_list_of_contents, from_list_of_names)]
return children