I've been trying to run matplotlib on CGI scripts with little success. I'm using Python3.5.
Most of the references I found online show functionality for Python2.x.
#!/usr/bin/python
import os,sys
import cgi
import cgitb
cgitb.enable()
import matplotlib
import matplotlib.pyplot as plt
import pylab
matplotlib.use('Agg')
plt.figure()
plt.plot([1,2,3])
import io
imgData = io.BytesIO()
pylab.savefig(imgData, format='png')
imgData.seek(0)
print("Content-type: image/png")
print()
print(imgData.read())
I am running Apache 2.4.18 on Arch Linux, and I get the following error:
The server encountered an internal error and was unable to complete your request.
Error message:
End of script output before headers: index.py
If you think this is a server error, please contact the webmaster.
My script has all required permissions to execute.
Any help will be appreciated.
UPDATE: I moved matplotlib.use('Agg') right beneath import matplotlib and now it gets past the server header error. The backend was getting declared earlier, hence the statement above has no effect. However, now I get the error:
The image 'http://localhost' cannot be displayed since it contains errors.
How can I correctly render images?
pylabwithout importing it.printat the end, write the data to a file and then open it in the browser. See if that is okay.