0

I need to send db data in csv format:

def send_csv():
    mkdir_p('csv')    

    outfile = open('csv/output.csv', 'w')
    outcsv = csv.writer(outfile)

    cursor = g.db.execute('Select * from my_table')

    outcsv.writerows(cursor)
    outfile.close()        

    return send_file('csv/output.csv', attachment_filename='output.csv')

But this gives following error during send_file:

RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
cosmos
  • 2,263
  • 1
  • 17
  • 30

2 Answers2

0

Problem is that you should set direct_passthrough to false; https://www.programcreek.com/python/example/58917/flask.send_file

You can also send file without writing to fs; flask make_response with large files

umut
  • 1,016
  • 1
  • 12
  • 25
0

First import below line in your script.

from flask import send_from_directory

Anyhow you are saving your csv in one folder and make that file serve using flask's inbuilt function called send_from_directory.

Change your return statement to this line, it will work.

return send_from_directory('folder-path', file_name, as_attachment=True)

For more details on send_from_directory, can go through the below link.

https://www.programcreek.com/python/example/65747/flask.send_from_directory

Venkatesh_CTA
  • 120
  • 10