[Matplotlib-users] Python script Graph to webpage

Glenn Nelson kitecamguy at gmail.com
Wed Jan 25 17:49:11 EST 2017


Here's my brief outline of a really basic approach.
You will create a simple program, let's call it *myplot.py*.

import sys
import matplotlib
matplotlib.use('Agg')       # backend that is used for web graphics
import matplotlib.pyplot as plt
# create your plot here, using 'plt'
print 'Content-Type: image/png\n'
plt.savefig( sys.stdout, format='png' )

And then in your web page, you have HTML IMG tag with *SRC="<**website*
*>/myplot.py"*

That's all there is to it. When I first got into this, I don't think the
suggested *webagg *backend was available to me.
Web browsers will normally cache the image, so you probably want to set
expiration headers, either Cache-Control or Expires.
Another way to force browser to make a new request is to add a parameter
with random value:
*SRC="<**website**>/myplot.py?x=<**random number**>"*
This would be especially helpful in testing, because the browser would
never think it had already cached the image (unless the random number
repeats).


----
Glenn Nelson in Santa Cruz
social: http://google.com/+GlennNelson
see my Kite Aerial Photos at http://www.glenn-nelson.us/kap

On Tue, Jan 24, 2017 at 11:00 PM, Trevor H <tripytrev at hotmail.com> wrote:

> Hi all,
>
> I am going to attempt to move my current python script (most seen here:
> http://stackoverflow.com/questions/41697966/python-pie-
> chart-not-presented-in-order
> To a web application so every time the page is refreshed the python script
> will run and display the latest graph & pie chart.
>
> Would anyone have any examples I could follow to get some idea of how I
> could create something like this?
>
> Thanks
> TJ
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170125/022c329b/attachment-0001.html>


More information about the Matplotlib-users mailing list