Matplotlib in Php
You can call Python code from within a Php script and get the return value and output like this:
exec('python mycode.py', $output, $return); foreach ($output as $line) print "$line<br/>";
To test if everything is ok for your web server to call the python script, try: sudo -u www-data python mycode.py
From this I found out that the Python call import matplotlib
was failing with the error:
RuntimeError: ‘/home/azimout’ is not a writable dir; you must set /home/azimout/.matplotlib to be a writable dir.
This can be resolved by adding the following line to your Python code before you import matplotlib: os.environ[ 'HOME' ] = '/tmp/'
Reference: http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script
THANK YOU! I’ve been debuggins some cgi scripts, and didn’t realize I could specify the server user to run it. I was setting my writeable directory correctly, but the hidden .matplotlib folder was not writable by the server.
Tim
30/08/2012 at 21:57