Hi All,
I am using Python and CGI to create a web server that displays charts from some data provided. Below is the code from two files. The first piece of code 'tries' to define a simple web-page, that is intended to contain an image that should be created by a script in the second file (call it a library file that knows how to plot different types of charts).
The problem is on how to call the script located in the library file, passing also the necessary data to create the chart. I know that line 23 of the first piece of code is not correct. But I do not know how to write it either. Could anyone give me a hint?
Dealing with python, I would assume there is a simpler way to do this, any ideas on how to do it or where to look for it will be welcome.
Thanks!
index.py
LibFile.py
I am using Python and CGI to create a web server that displays charts from some data provided. Below is the code from two files. The first piece of code 'tries' to define a simple web-page, that is intended to contain an image that should be created by a script in the second file (call it a library file that knows how to plot different types of charts).
The problem is on how to call the script located in the library file, passing also the necessary data to create the chart. I know that line 23 of the first piece of code is not correct. But I do not know how to write it either. Could anyone give me a hint?
Dealing with python, I would assume there is a simpler way to do this, any ideas on how to do it or where to look for it will be welcome.
Thanks!
index.py
Code:
#!c:/Python24/python.exe -u import sys, os, cgi, urllib import cgitb; cgitb.enable() ## Import my libraries # Add path to search for libraries sys.path.append("./myLibs") import LibFile query = cgi.FieldStorage() TheData={'x1': range(10),'y1': range(10),'l1':'First Legend','t':'Some title','h':'Some X label','v':'Some Y label'} print "Content-type: text/html\n" print """ <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>Data analysis</title> </head> <body> <p><IMG SRC="myLibs/LibFile.py/myPlot(%(TheData)s)"></p> </body> </html> """ %{"TheData":urllib.quote(str(TheData))}
Code:
def myPlot1(myData): [...] ## Create plot using matplotlib... [...] print "Content-Type: image/png\n" pylab.savefig(sys.stdout) def myPlot2(myData): [...] ## Create plot using matplotlib... [...] print "Content-Type: image/png\n" pylab.savefig(sys.stdout)
Comment