How to use dataframe from a function and return response to download csv file – Flask

Solution
import io 
from flask import send_file

@app.route('/csv_download')
def csv_download():
    sql = """select * from test"""
    df = pd.read_sql_query(sql, conn, index_col='itemID')
    return send_file(
        io.BytesIO(df.to_csv(index=False, encoding='utf-8').encode()),
        as_attachment=True,
        attachment_filename='Export_Merchant_Validator_Tool.csv',
        mimetype='text/csv')
Download