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 关注
- 200 浏览
添加回答
举报