has anyone got matplotlib to work in a cgi script on Apache?

Hi,

The code below when run from a file in the cgi-bin directory should generate a plot but it does not work:

#!d:/apps/Python23/python

import sys
import matplotlib
matplotlib.use('Agg')
from pylab import *

plot([1,2,3,4])

#print "Content-type: text/html\n\n"
#print "<html>Hello world!</html>"

print "Content-type: image/png\n\n"
print savefig(sys.stdout)

However, it does not work and I am really struggling to get matplotlib to generate a plot dynamically in a cgi-script. If anyone has done this successfully I would really appreciate some help or a simple example. I am using Windows XP and Apache 2.0.53.

Many thanks,

Simon

I do it all the time, but instead of writing to a stream directly, I write to file, and embed the image in the html. This works for me, because (1) I need that html around to let users setup options, and (2) the images are available at a later date - something that has been plotted once can be viewed multiple times.

You basically save your plot to a file and then include something like:
<img src="/tmp/image.2005-03-19_10-50-20.38431390.png">
in your html.

Unless you serve thousands of requests, the few ms waisted writing/reading to/from a file should be negligible.

Finally, you might want to look into the error log file for apache, and see what goes on behind the scenes. I still run 0.65 (modified) and remember had to setup proper permissions on a cache file before thigs worked.
Another thing, maybe not the case here, but worth remembering that it is crucial to have the 'Content-type' line be the first data written. I've had issues in the past where importing some libraries would write something else to the stream, and often would be get some criptic errror.
Cheers,

ยทยทยท

--
Peter Groszkowski Gemini Observatory
Tel: +1 808 9742509 670 N. A'ohoku Place
Fax: +1 808 9359235 Hilo, Hawai'i 96720, USA

Simon Hook wrote:

Hi,

The code below when run from a file in the cgi-bin directory should generate a plot but it does not work:

#!d:/apps/Python23/python

import sys
import matplotlib
matplotlib.use('Agg')
from pylab import *

plot([1,2,3,4])

#print "Content-type: text/html\n\n"
#print "<html>Hello world!</html>"

print "Content-type: image/png\n\n"
print savefig(sys.stdout)

However, it does not work and I am really struggling to get matplotlib to generate a plot dynamically in a cgi-script. If anyone has done this successfully I would really appreciate some help or a simple example. I am using Windows XP and Apache 2.0.53.

Many thanks,

Simon

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users