Forums

Uploaded file through python code not refreshed

Hi,

I have python code which will write an excel file based on two input files to my project directory location. When I execute the code through a link in web app, the file created does not reflect/overwrite until I manually reload the web app. Is there any solution for this ?

cal_operations.py:
 # write dataframe to Excel file
        df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in cal_df.items()]))
        df.to_excel("/home/asinha01/autocal/output/CALENDAR.xlsx", index=False)

main.py:
@app.route("/downloadxls", methods=['GET'])
def get_cal_xl():
    exec('cal_operations')
    return send_file(
        '/home/asinha01/autocal/output/CALENDAR.xlsx',
        mimetype="xlsx",
        as_attachment=True)

cal_operations are evaluated only once on import, and import happens when you reload your web app.

Do not do that. You do not need to use exec, you can import functions and call them and you can operate on files in memory, that you do not have to write to disk.

See Using Flask's send_file function with BytesIO

Thank you for your help. I created a function and returned data-frame in the main.py. I can now create and overwrite the excel file without reloading the web app.

Glad to hear that you made it work!