'Flask application open in multiple tabs downloads corrupted excel file

I am a new developer and I developed a small flask 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



Solution 1:[1]

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")

What I think happened is when more than 1 session runs they try to create file with same name and write to it which cause the files corrupt.

My Solution:

import random 
key = random.randint(1, 10000) 
filename = 'Result' + str(n) + '.xlsx' 
writer = pd.ExcelWriter(file_name, engine="xlsxwriter")

This works well. You may concatenate the timestamp or add a logic to rename the file before it is downloaded.

If there is a better way to do this please comment

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ellhe-blaster