How should I go about creating a potentially large xlsx file download? I'm creating a xlsx file with pandas from a list of dictionaries and now need to give the user possibility to download it. The list is in a variable and is not allowed to be saved locally (on server).
df = pandas.DataFrame(self.csvdict)
writer = pandas.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
Jmcnamara is pointing you in the rigth direction. Translated to your question you are looking for the following code:
sio = StringIO() PandasDataFrame = pandas.DataFrame(self.csvdict) PandasWriter = pandas.ExcelWriter(sio, engine='xlsxwriter') PandasDataFrame.to_excel(PandasWriter, sheet_name=sheetname) PandasWriter.save() sio.seek(0) workbook = sio.getvalue() response = StreamingHttpResponse(workbook, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename=%s' % filename
Notice the fact that you are saving the data to the StringIO variable and not to a file location. This way you prevent the file being saved before you generate the response.