1. Prerequisites: You need to have the following installed to successfully run this example: Zope, Matplotlib (on top of Zope's Python), Python Image Library (PIL). And one more thing - probably every body does this right, but just in case - zope instance home directory has to be writable, for following to work.
  2. Create a file (e.g. mpl.py) in INSTANCEHOME\Extensions:

   1 import matplotlib
   2 matplotlib.use('Agg')
   3 from pylab import *
   4 from os import *
   5 from StringIO import StringIO
   6 from PIL import Image as PILImage
   7 from matplotlib.backends.backend_agg import FigureCanvasAgg
   8 def chart(self):
   9     clf()
  10     img_dpi=72
  11     width=400
  12     height=300
  13     fig=figure(dpi=img_dpi, figsize=(width/img_dpi, height/img_dpi))
  14     x=arange(0, 2*pi+0.1, 0.1)
  15     sine=plot(x, sin(x))
  16     legend(sine, "y=sin x", "upper right")
  17     xlabel('x')
  18     ylabel('y=sin x')
  19     grid(True)
  20     canvas = FigureCanvasAgg(fig)
  21     canvas.draw()
  22     size = (int(canvas.figure.get_figwidth())*img_dpi, int(canvas.figure.get_figheight())*img_dpi)
  23     buf=canvas.tostring_rgb()
  24     im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)
  25     imgdata=StringIO()
  26     im.save(imgdata, 'PNG')
  27     self.REQUEST.RESPONSE.setHeader('Pragma', 'no-cache')
  28     self.REQUEST.RESPONSE.setHeader('Content-Type', 'image/png')
  29     return imgdata.getvalue()

2. Then create an external method in ZMI (e.g. Id -> mplchart, module name -> mpl, function name -> chart).

3. Click the Test tab and you should see the sine plot.


CategoryCookbookMatplotlib

Cookbook/Matplotlib/Matplotlib and Zope (last edited 2006-12-04 17:31:22 by MarekSzczypinski)