Excel file corrupted if running dash app simultaneously in two browser tabs

Hello Team,

I developed a small dash app that takes excel file as input, process it and downloads the result as an excel file.

When I run in 1 browser tab the app works as expected. But if I run in multiple browser tabs simultaneously, the downloaded excel files are corrupted

I deployed the app in Beanstalk and tried running the app in two different browsers simultaneously still the excel files that are downloaded are corrupted. But if I run only 1 session it works fine.

I tried setting threaded = True but that did not help

Am I missing anything? Please help. Thanks in advance

Hi @svhdec3 ,

Welcome to the community!

Could you share the part of your code where you generate the Excel file?

Hi jlfsjunior,

Thank you very much.

My code
writer = pd.ExcelWriter(“Result”, engine=“xlsxwriter”)
tab1.to_excel(writer, sheet_name=‘Tab1’, index=False)
tab2.to_excel(writer, sheet_name=‘Tab2’, index=False)
tab3.to_excel(writer, sheet_name=‘Tab3’, index=False)

return dcc.send_file(Result)

I was able to find a work around from one of colleague. While writing to the file I hardcoded the file name like:

writer = pd.ExcelWriter(“Result”, engine=“xlsxwriter”)

I believe this caused all the sessions to use the same file name which caused the files to corrupt (just guessing)

Work around I added a random number to the file name so every session that I open in a browser tab uses a distinct file name:

import random
key = random.randint(1, 10000)
filename = ‘Result’ + str(n) + ‘.xlsx’

writer = pd.ExcelWriter(file_name, engine=“xlsxwriter”)

This fixed my issue. Now I am trying to find a way to rename my file to “Result” and truncate the appended number before downloading. Is that possible?

If there is a better way to do this please let me know

Thank you.

That was my expectation, that you would have the same file opened multiple times.

I think your fix is good, but if you don’t want to create the files in the backend, you could try an approach like this:

1 Like

I would recommend generating the file in-memory to avoid this kind of issue. That should also solve you naming problem. Here is a small example,

1 Like

Thank you Emil. This is a great solution. Works perfect. Thank you

Thank you jlfsjunior. This is exactly what I wanted. I changed my code now I don’t have to deal with deleting files from my project folder or renaming.