from flask import Flask, send_file app = Flask(__name__) @app.route('/') def home(): return 'Welcome to the Data Analysis System!' if __name__ == '__main__': app.run(debug=True)
这段代码启动了一个基础的Flask服务器。
@app.route('/download') def download_file(): path = "data_analysis_result.csv" return send_file(path, as_attachment=True)
这里假设你已经生成了一个名为`data_analysis_result.csv`的数据文件。
import pandas as pd from flask import Flask, send_file app = Flask(__name__) @app.route('/download') def download_file(): # 示例数据 data = {'Name': ['Tom', 'Nick', 'John'], 'Age': [20, 21, 19]} df = pd.DataFrame(data) # 保存到CSV文件 file_path = 'temp_data.csv' df.to_csv(file_path, index=False) return send_file(file_path, as_attachment=True) if __name__ == '__main__': app.run(debug=True)
这样每次访问`/download`时都会自动生成一个新的CSV文件并供用户下载。