[mod_python] matplotlib and sys.stdout[.write()] <> req[.write()]

John Hunter jdhunter at nitace.bsd.uchicago.edu
Thu Jun 2 18:42:49 EDT 2005

>>>>> "Adrian" == Adrian Immler <mod_python-maillist at uebergeek.de> writes:

    Adrian>   File "/www/uebergeek.de/htdocs/music/test.py", line 45,
    Adrian> in test canvas.print_figure(req)

    Adrian>   File
    Adrian> "/usr/lib/python2.4/site-packages/matplotlib/backends/backend_agg.py",
    Adrian> line 383, in print_figure basename, ext =
    Adrian> os.path.splitext(filename)

    Adrian>   File "/usr/lib/python2.4/posixpath.py", line 92, in
    Adrian> splitext i = p.rfind('.')

    Adrian> AttributeError: 'mp_request' object has no attribute
    Adrian> 'rfind'

    Adrian> so it is nearly the same error. now my question is:

    Adrian> how can i create a png file with matplotlib and send it to
    Adrian> the client without saving the file to harddisk and send
    Adrian> that one.

    Adrian> thanks in advance !

    Adrian> p.s. yes i know that the content_type('image/png') is
    Adrian> missing in the above example ...

It looks like mod_python is doing something funny with sys.stdout,
because I can run your script in as a standalone python script

    import sys
    import matplotlib
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.set_title('hi mom')

Run with 

 > python test.py > test.png

Everything works as it should.

The relevant code in lib/matplotlib/backends/backend_agg.py around
line 377 is

        if  isinstance(filename, file):
            # assume png and write to fileobject
            # take a look at the extension and choose the print handler
            basename, ext = os.path.splitext(filename)
            if not len(ext):
                ext = '.png'

You want to be in the first part of this conditional, ie, you want the
call to 


Apparently the isinstance(filename, file) test is failing under
modpython and working under normal python.  Anyone know why, or what
would be a more appropriate test for a file object under modpython


More information about the Mod_python mailing list