2 回答

TA貢獻2037條經驗 獲得超6個贊
您還可以將文件存儲在服務器上并通過下載 csv 路徑將其發送給用戶。這是發送文件教程
from flask import Flask, render_template, send_file
app = Flask(__name__)
@app.route('/uploader', methods = ['GET','POST'])
def upload():
new=nrecs[['UserID','ProductID','Rating']]
new['Recommendations'] = list(zip(new.ProductID, new.Rating))
res=new[['UserID','Recommendations']]
res_new=res['Recommendations'].groupby([res.UserID]).apply(list).reset_index()
# store the dataframe on the server.
res_new.to_csv('Recommendations.csv')
pd.options.display.max_colwidth = 500
return render_template('simple.html', tables=[res_new.to_html(classes='data')], titles='')
@app.route('/download-csv', methods = ['GET'])
def download():
# return the CSV file to the user here.
return send_file('Recommendations.csv')

TA貢獻1757條經驗 獲得超7個贊
您可以嘗試使用會話對象。請參閱此問題/答案。但是,根據數據框的大小以及您最終嘗試執行的操作,這可能不是執行此操作的最佳方法。如果您嘗試設置上傳/下載路由,將文件存儲在服務器/其他地方,然后在用戶請求時將其發送給用戶可能是更好的解決方案。
from flask import Flask, render_template, session
app = Flask(__name__)
# secret key is needed for session
app.secret_key = 'your secret key'
@app.route('/uploader', methods = ['GET','POST'])
def upload():
new=nrecs[['UserID','ProductID','Rating']]
new['Recommendations'] = list(zip(new.ProductID, new.Rating))
res=new[['UserID','Recommendations']]
res_new=res['Recommendations'].groupby([res.UserID]).apply(list).reset_index()
session['reco_df'] = res_new
pd.options.display.max_colwidth = 500
return render_template('simple.html', tables=[res_new.to_html(classes='data')], titles='')
@app.route('/download-csv', methods = ['GET'])
def download():
return session['reco_df'].to_csv('Recommendations.csv')
- 2 回答
- 0 關注
- 241 瀏覽
添加回答
舉報