0

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:

enter image description here

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
Adrian
  • 725
  • 4
  • 18
  • Check value of `output_filename`. – Olvin Roght May 25 '21 at 09:32
  • @OlvinRoght I am not sure if I understand what you mean. I am comparing Excel files using 2 main functions compare dfs that is comparing dataframes and to_formatted_excel that is creating Excel file with rich string formating and the file_name looks like this: file_name = upload_folder + f'/Compared_DataFrames_{str_now}.xlsx' – Adrian May 25 '21 at 09:40
  • I've meant to check function output. If it's `None` - that could cause an exception. – Olvin Roght May 25 '21 at 09:42
  • for now new.xlsx and old.xlsx is being correctly uploaded to the uploads folder but there is no Compared_DataFrames_.xlsx file so yes you are right – Adrian May 25 '21 at 09:48

1 Answers1

1

Can you try it by changing below code -
I haven't tested it but it should work for you.

from flask import request


@app.route('/uploads')
def download_comparison():
    return send_from_directory(UPLOAD_FOLDER, request.args.get('filename'), as_attachment=True)
Suyog Shimpi
  • 706
  • 1
  • 8
  • 16
  • thanks for the answer, now I am getting this error: TypeError: expected str, bytes or os.PathLike object, not NoneType – Adrian May 25 '21 at 09:36
  • 1
    You may received error at `request.args.get('filename')` this place. Please, check [this answer](https://stackoverflow.com/questions/26954122/how-can-i-pass-arguments-into-redirecturl-for-of-flask) – Suyog Shimpi May 25 '21 at 09:43