如何在flask python中将“新csv文件”保存为“文件名”

如何在flask python中将“新csv文件”保存为“文件名”,python,flask,web,redirect,path,Python,Flask,Web,Redirect,Path,我想将result.csv保存到filename中,这样就可以将它传递到app.config['UPLOAD\u FOLDER']并从HTML页面中的下载按钮检索回来,我该如何处理我的代码?你们能和我分享一些关于我的代码的提示吗?提前谢谢 app.py UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/transf

我想将
result.csv
保存到
filename
中,这样就可以将它传递到
app.config['UPLOAD\u FOLDER']
并从HTML页面中的
下载按钮
检索回来,我该如何处理我的代码?你们能和我分享一些关于我的代码的提示吗?提前谢谢

app.py

UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__))
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/transform', methods=["POST"])
def transform_view():
.........
........
for i in range(X_FUTURE):
            curr_date = curr_date +  relativedelta(months=+1)
            dicts.append({'Predictions': transform[i], "Month": curr_date})
            

        new_data = pd.DataFrame(dicts).set_index("Month")
        ##df_predict = pd.DataFrame(transform, columns=["predicted value"])

        new_data.to_csv(os.path.join(app.config['UPLOAD_FOLDER'], "result.csv"), index = True, encoding='utf8')

        labels = [d['Month'] for d in dicts]
            
        values = [d['Predictions'] for d in dicts]

        colors = [ "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA",
                       "#ABCDEF", "#DDDDDD", "#ABCABC", "#4169E1",
                       "#C71585", "#FF4500", "#FEDCBA", "#46BFBD"]

        line_labels=labels
        line_values=values
        return render_template('graph.html', title='Time Series Sales forecasting', max=17000, labels=line_labels, values=line_values)

@app.route('/download/<filename>')
def download(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment = True)
UPLOAD\u FOLDER=os.path.dirname(os.path.abspath(\u文件\u))
app.config['UPLOAD\u FOLDER']=UPLOAD\u FOLDER
@app.route('/transform',methods=[“POST”])
def transform_view():
.........
........
对于范围内的i(X_未来):
当前日期=当前日期+相对延迟(月=+1)
追加({'Predictions':transform[i],“Month”:curr_date})
新数据=pd.数据帧(dicts).设置索引(“月”)
##df_predict=pd.DataFrame(转换,列=[“预测值”])
新建到csv(os.path.join(app.config['UPLOAD\u FOLDER'],“result.csv”),index=True,encoding='utf8')
标签=[d['Month']表示dicts中的d]
数值=[d['predicts']表示dicts中的d]
颜色=[“F7464A”、“46BFBD”、“FDB45C”、“FEDCBA”,
“ABCDEF”、“DDDD”、“ABCABC”、“4169E1”,
“C71585”、“FF4500”、“FEDCBA”、“46BFBD”]
line_标签=标签
行_值=值
返回渲染模板('graph.html',title='Time Series Sales forecasting',max=17000,labels=line\u labels,values=line\u values)
@app.route(“/download/”)
def下载(文件名):
从_目录返回发送_(app.config['UPLOAD_FOLDER'],文件名,如_attachment=True)
HTML pgae>

试试这个