I am creating a flask app that takes a CSV as input. Parses the CSV and the content. Then returns an updated CSV to the browser to download/or instantly downloads. The program needs an HTML template for users to interact. I am attempting to use the render_template() method to return the HTML file and the CSV file via flask. What is the best way to achieve this?
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
print("POST request received!")
if "file" not in request.files:
return redirect(request.url)
list_keywords = request.files["file"]
if not list_keywords:
return "No file"
the_list = io.StringIO(list_keywords.stream.read().decode("UTF8"), newline=None)
csv_input = csv.reader(the_list)
...# rest of program.....
csv_file1 = pd.DataFrame(internal_linking_opportunities,
columns=["Keyword", "Text", "Source URL", "Target URL", "Link Presence", "Keyword Position"])
si = io.StringIO()
cw = csv.writer(csv_file1)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=export.csv"
output.headers["Content-type"] = "text/csv"
return Response(render_template('index.html', output=output))
I have attempted to use other methods to return the CSV with no luck. Starting to think it could be due to the render_template function that I am using. Your help is appreciated.