from flask import Flask, Response
import pandas as pd
app = Flask(__name__)
@app.route('/get_csv')
def get_csv():
# Step 1: Create a sample DataFrame (Replace this with your actual DataFrame)
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 22],
'City': ['New York', 'San Francisco', 'Seattle']}
df = pd.DataFrame(data)
# Step 2: Convert the DataFrame to a CSV string
csv_data = df.to_csv(index=False)
# Step 3: Set up the HTTP response with the appropriate headers
response = Response(csv_data, mimetype='text/csv')
response.headers['Content-Disposition'] = 'attachment; filename=data.csv'
# Step 4: Return the response
return response
if __name__ == '__main__':
app.run(debug=True)