Forums

500 Internal Server Error when attempting to create a saved file using static fig.savefig(os.path.join('static', 'filename')

500 Internal Server Error when attempting to create a saved file using static fig.savefig(os.path.join('static', 'filename')

fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

I had followed https://help.pythonanywhere.com/pages/StaticFiles/ to setup my static files with the following:

URL: /static/

Directory: /home/dennislee04/portfo/static

There is this full actual directory on my app server: /home/dennislee04/portfo/static/assets/plots

I have also reloaded the App & refreshed the web-browser on my page, after adding this static directory. Same error. Internal Error. Is there something wrong with how I had setup the Static Directory? This python code works locally on my workstation, just not when I have it up in my App Server.

Maybe add some logging to check if the path is actually what you expect and check if heatmap.png actually exists. Also -- what is fig?

I will try to do a check to see the path is actually what I expect. "heatmap.png" doesn't exist, because this code

fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

Is suppose to create the heatmap.png file here.

Here is the code in Python.

fig, ax = plt.subplots(figsize=(6,6))
ax = sns.set_style(style="darkgrid")
x = [i for i in range(100)]
y = [i for i in range(100)]
sns.heatmap(resultsOne, annot = True, fmt = '.2g', center = 0, cmap = 'coolwarm', linewidth = 1, linecolor = 'black')
canvas = FigureCanvas(fig)
fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

'fig' is the heatmap that I am creating, via Python.

Also, I would think that the path is what I expect, as the paths are the same on my App Server in Python Anywhere & on the local copy on my pc (I'm also using flask on my local pc).

Folder paths are the same on my App Server & my local pc. The code is the same on my App Server & on my local PC. There is no issues on my local PC, but there is that error on my App Server, in Python Anywhere.

See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Interesting. Isn't there were the "static" file setup from

https://help.pythonanywhere.com/pages/StaticFiles/

comes in to play? Or am I getting this wrong?

The error has nothing to do with static files. You are trying to save into a location that does not exist because you are using a relative path without paying attention to the working directory. Save the file using an absolute path to a location that exists and it should save correctly. Then you can use static files to serve it once it's saved.

That doesn't make sense, since I'm able to write to a directory using relative path, using a different function in the same Server.py file

I'm able to write to a file, using relative path in the same Server.py file.

def write_to_file(data):
with open('./portfo/data_files/database.txt', mode='a') as database:
    email = data["email"]
    subject = data["subject"]
    message = data["message"]
    file = database.write(f'\n{email}, {subject}, {message}')

Current issue is on this block:

fig, ax = plt.subplots(figsize=(6,6))
ax = sns.set_style(style="darkgrid")
x = [i for i in range(100)]
y = [i for i in range(100)]
fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

OK. In that case, you would need the directory portfo and the one called static to be in the same directory. Are they? And you would also need there to be assets/plots in the directory called static

I shouldn't need my "static" directory to be in the same directory as "portfo", right? I have already setup my static URL

I have this static URL setup:

URL: /static/

Directory: /home/dennislee04/portfo/static

I have the directory on my App server:

/home/dennislee04/portfo/static/assets/plots

Here is my HTML code that I am trying to pass the file to:

<!--<img src="{{ url_for('static', filename='assets/plots/heatmap.png') }}" alt="">

Here is the Python code that is suppose to create the file, using my defined static location (referenced from my static URL setup):

fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

Or will os.path.join('static', 'assets/plots/heatmap.png') not work using the static location, setup via the App Server or am I using that os.path.join wrong?

Anyhow. I couldn't get that static URL location to work with

fig.savefig(os.path.join('static', 'assets/plots/heatmap.png'), dpi=75)

So I just removed this static URL location setting from my App Server.

https://help.pythonanywhere.com/pages/StaticFiles/

And I just changed my HTML & Server.py files to not use the following 2 lines above.

Sorry did you get it working?