In python using Flask, how can I write out an object for download? In python using Flask, how can I write out an object for download? flask flask

In python using Flask, how can I write out an object for download?


Streaming files to the client without saving them to disk is covered in the "pattern" section of Flask's docs - specifically, in the section on streaming. Basically, what you do is return a fully-fledged Response object wrapping your iterator:

from flask import Response# construct your app@app.route("/get-file")def get_file():    results = generate_file_data()    generator = (cell for row in results                    for cell in row)    return Response(generator,                       mimetype="text/plain",                       headers={"Content-Disposition":                                    "attachment;filename=test.txt"})