Forums

Plot only displaying when logged into pythonanywhere

I am using matplotlib & bottle.py to plot a graph, based on an input value on my webpage:

http://carboncycle2000.pythonanywhere.com/

when I am logged into pythonanywhere, the plot displays. But when I am logged out, I just see "image placeholder". The plot is a .png image, and seems to be readable by everyone.

In the error log, I do see:

OSError: write error 2017-11-28 14:15:13,247: Error running WSGI application 2017-11-28 14:15:13,267: GeneratorExit

Does anyone know why this might be occurring?

it sounds like maybe you're trying to serve the image directly from your files tab, instead of via our static files service? check out these two help pages:

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

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

Hi Harry, thank you for your reply. Yes I uploaded the image to my files tab on pythonanywhere and was trying to display it on the webpage. Now I understand I should use your static files mapping service, but where do I put/ save my image? Is it on pythonanywhere?

Yes, that's right -- if you want something to be served by our static files system, it needs to be on PythonAnywhere. A popular way to do this is, if your website is at /home/CarbonCycle2000/mysite on PythonAnywhere, you create a subdirectory called /home/CarbonCycle2000/mysite/static/, and then you set up a mapping on the static files table that has the URL set to /static/ and the directory set to /home/CarbonCycle2000/mysite/static/. Once you've done that, and reloaded the website using the button at the top of the "Web" page, an image that you've stored as /home/CarbonCycle2000/mysite/static/myimage.png will be available publicly as http://carboncycle2000.pythonanywhere.com/static/myimage.png.

I did succeed in using your static files system on the "Web" tab. But it says "Files that aren't dynamically generated by your code, like CSS, JavaScript or uploaded files" . I'm trying to display a graph using an input value from a user on my webpage, which makes it a dynamic file generated by my code. Currently my .tlp file looks like

% plt.savefig('plottest.png')

% end

<img src="/plottest.png" alt="Image Placeholder">

I plot a graph with an input value and save it at /home/CarbonCycle2000/mysite . And then trying to show it on the same page but it doesn't show up. The png image is in the right directory (/mysite) and I can see it from my PythonAnywhere "Files" tab. It just doesn't show on the webpage.

One more problem is that the graph does not overwrite by itself. So I input a value on my webpage, make a graph, the code saves it but doesn't show it, and I go back to the input page again and do the same thing. I can see the plot from the "Files" tab then it shows the two graphs on the same axises. I see two lines in one plot area. How can I fix it?

Hi there,

It's ok to ignore that sentence about "files that aren't dynamically generated", maybe that needs a bit of a wording tweak.

If you always save the plot with the same filename, you'll run into issues with browser caching. My suggestion would be to save each new plot with some sort of unique-id or timestamp in it?