python - Django Pandas to http response (download file) -


python: 2.7.11

django: 1.9

pandas: 0.17.1

how should go creating potentially large xlsx file download? i'm creating xlsx file pandas list of dictionaries , need give user possibility download it. list in variable , not allowed saved locally (on server).

example:

df = pandas.dataframe(self.csvdict) writer = pandas.excelwriter('pandas_simple.xlsx', engine='xlsxwriter') df.to_excel(writer, sheet_name='sheet1') writer.save() 

this example create file , save executing script located. need create http response user download prompt.

i have found few posts doing xlsxwriter non pandas. think should using 'streaminghttpresponse' , not 'httpresponse'.

jmcnamara pointing in rigth direction. translated question looking 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 fact saving data stringio variable , not file location. way prevent file being saved before generate response.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -