Issue
On the click of a download button, I am trying to download multiple dash datatables to one excel file on different sheets. I have a callback that on a different click, performs some calculations and returns dataframes to dash datatables. I want to take those tables (after calc performed) and export to one excel file. Note: I cannot download new version of dash with dcc.download not have access to expansions. That is why I am trying to do this via pandas.
The dataframes below ('Investment_To_Gross_Impact', etc.) represent the new datatables after calcs have been performed.
#download to xsxl
@CAD.callback(
Output('download', 'data'),
Input('download', 'n_clicks'),
Input("titleinput", "value"),
Input('Investment_To_Gross_Impact','data'),
Input('Investment_From_Gross_Impact','data'),
Input('Investment_To_Assumptions','data'),
Input('Investment_From_Assumptions','data'),
)
def export(n_clicks, value, invto, invfrom, ato, afrom ):
df1= pd.DataFrame.from_dict(invto)
df2= pd.DataFrame.from_dict(invfrom)
df3= pd.DataFrame.from_dict(ato)
df4= pd.DataFrame.from_dict(afrom)
if n_clicks>0:
with pd.ExcelWriter(str(value)+'.xlsx', engine='xlsxwriter') as writer:
df1.to_excel(writer, sheet_name='Inv To Gross Impact')
df2.to_excel(writer, sheet_name='Inv From Gross Impact')
df3.to_excel(writer, sheet_name='Inv To Assumptions')
df4.to_excel(writer, sheet_name='Inv From Assumptions')
writer.save()
Solution
Didn't realize the file was downloading into the directory where my py file is being saved.
def export(n_clicks, netimpact, invto, invfrom, ato, afrom, value):
netdf= pd.DataFrame.from_dict(data=netimpact)
df1= pd.DataFrame.from_dict(data=invto)
df2= pd.DataFrame.from_dict(data=invfrom)
df3= pd.DataFrame.from_dict(data=ato)
df4= pd.DataFrame.from_dict(data=afrom)
filename= (str(value)+'.xlsx')
writer= pd.ExcelWriter(filename, engine='xlsxwriter')
netdf.to_excel(writer, sheet_name='Net RAF Impacts', index=False)
df1.to_excel(writer, sheet_name='Inv To Gross Impact', index=False)
df2.to_excel(writer, sheet_name='Inv From Gross Impact', index=False)
df3.to_excel(writer, sheet_name='Inv To Assumptions', index=False)
df4.to_excel(writer, sheet_name='Inv From Assumptions', index=False)
writer.save()
Answered By - kw134
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.