Flask App Gives Error When Deployed On Heroku And Trying To Open Excel File
Solution 1:
What I want to do is to create excel spreadsheet with data from the database, and then I want the user to print it or download it on his computer
os.startfile()
will never do this.
It might look like it does in development, but that's only because your client and server are running on the same machine. If you host your code on Windows (where os.startfile()
exists) and connect to it from another machine, your server will try to open the file. It won't show up on the client at all.
Get rid of
os.startfile("C:\\Users\\Nenad\\Desktop\\magacin vs\\ISPRATNICI\\{}, {}.xlsx".format(s.id, s.ime))
entirely.
Instead, end your function with send_file
:
from flask import send_file
@app.route("/proverka", methods=['GET', 'POST'])defproverka():
# ...
send_file('ISPRATNICI\\{}, {}.xlsx'.format(s.id, s.ime))
As Tin noted, Heroku's filesystem is ephemeral so the PDFs on your server will disappear. If that's okay, you probably won't run into too many issues with it. If somebody requests a PDF just before the dyno restarts you might have some issues, but they should be able to request it again.
Solution 2:
You hardcoded the path:
os.startfile("C:\\Users\\Nenad\\Desktop\\magacin vs\\ISPRATNICI\\{}, {}.xlsx".format(s.id, s.ime))
With os
you can build a relative path so it works both on Windows and Linux. We need to see the source code to advice you how to build the path correctly.
With that said it seems you are writing data to a file which you probably are using later. Heroku is not suited for this.
https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem
Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.
Dynos are restarted once a day. Meaning your data will be lost. You need a proper database or use another hosting solution.
Post a Comment for "Flask App Gives Error When Deployed On Heroku And Trying To Open Excel File"