I have a problem with downloading the output file from the website. I am using Flask to upload files, process them, generate the output and download the file.
Below you can see an error I am getting:
It seems that I am not passing filename but I am not sure how to fix it. Any help or advice will be much appreciated.
functions in flask.py that are related to this issue
UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/uploads/'
@app.route('/compare_content', methods=['GET', 'POST'])
def compare():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('file')
files = [read_xlsx_binary(file) for file in files]
df_old, df_new = files
output_filename = run_compare_dfs(UPLOAD_FOLDER, df_old, df_new)
return redirect(url_for('download_comparison', filename=request.args.get(output_filename)))
return render_template('index.html', username=session['username'])
def read_xlsx_binary(file):
filename = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filename)
df = pd.read_excel(filename, engine="openpyxl")
# os.unlink(filename)
return df
# Allow user to download the output that was generated by run_compare_dfs() function
@app.route('/uploads')
def download_comparison(filename):
return send_from_directory(UPLOAD_FOLDER, request.args.get('filename'), as_attachment=True)
compare.py
def run_compare_dfs(upload_folder, df_old=None, df_new=None):
if df_old is None or df_new is None:
path_OLD = 'old.xlsx'
path_NEW = 'new.xlsx'
df_old = pd.read_excel(path_OLD, engine="openpyxl")
df_new = pd.read_excel(path_NEW, engine="openpyxl")
final_df = compare_dfs(df_old, df_new)
output_filename = to_formatted_excel(upload_folder, final_df)
return output_filename