#! /usr/bin/env python """gnupyplot.py -- Develop a Gnuplot python module that takes data files written by MathAction/Axiom statements in the "images" directory and forms displayable graphics files for inclusion in MathAction pages. This working version generates a png plot of data found in the file testfile2.dat located in directory the var/LatexWiki subdirectory of the zope instance which is the LocalFS called images in a MathAction installation (on the original development machine: /var/lib/zope2.9/instance/dczope/var/LatexWiki) and returns a file with name "filename".png in the same directory. It is linked to an ExternalMethod with the id gnupyplot. """ from Numeric import * # If the package has been installed correctly, this should work: import Gnuplot, Gnuplot.funcutils def gnupyplot(self,plotfile="testfile2.dat",plottitle="Empty Title"): """Demonstrate the Gnuplot-py basis for MathAction Graphics. This will have to be called via a URL. The Test function in the Zope management window will fail and spill a long error message since if we do not pass a file name argument, unless we provide a default value. Our test value is testfile2.dat. To call this function use the GET form input format, for example: http://guardian:9673/Math/gnupyplotfile?plotfile=testfile2.dat Add a title with the plottitle parameter and remember to escape spaces as %20 http://guardian:9673/Math/gnupyplotfile?plotfile=testfile2.dat&plottitle=sin(x)%20plot Since the cwd given to an External Method is the root of the calling instance, for example: /var/lib/zope2.9/instance/dczope, in my case, we can refer to files relative to this root, for example in var/Latexwiki which is the "images" LocalFS for MathAction. """ # Generate a plot from a list of points. g = Gnuplot.Gnuplot() g('set terminal png medium size 640,480') #If we pass a full path/filename, it must be in "" inside the '' quotes! #g('set output "/var/lib/zope2.9/instance/dczope/var/LatexWiki/testfile2.dat.png"') #So the outputfile name passed to g must similarly constructed with "" bracketing outputfile = '"var/LatexWiki/'+plotfile+'.png"' #Removing the following line would cause XWindow display instead g('set output '+outputfile) g.title(plottitle) # (optional) #g.title('Plotting from a file') # (optional) #g('set data style linespoints') # give gnuplot an arbitrary command # Plot a list of (x, y) pairs stored in a file): g.plot(Gnuplot.File('var/LatexWiki/testfile2.dat', using='1:2', with='lines')) #restore the non-file output status of gnuplot for non-zope testing g('set output') #Line below used for troubleshooting, passes text back to calling web browser #return "plotfile= %s." % outputfile # when executed, just run demo(): if __name__ == '__main__': demo()