This script is intended to be run as a CGI in a web server.
[The following is available as "pythondemo_cgi/simplebar.py".]
The code is almost identical to the code in The First Project, so the details will not be further explained. The major difference is that instead of using BaseChart.makeChart to output the chart as a PNG file, it outputs the chart as a binary string using BaseChart.makeChart2 and streams the data directly to the browser.
The chart image is streamed to tbe browser using the folloiwng code:
|
The above code first prints the MIME Content-type header, and then use pychartdir.binaryPrint to print out the binary image for delivery to the browser.
pychartdir.binaryPrint is a ChartDirector utility for binary printing, which solves two problems with the Python "print" statement when printing binary images:
- On Windows, by default, the "print" statement will print in text mode.
It will replace [LF] with [CR][LF], and will corrupt a binary image. The
"binaryPrint" statement always print in binary mode.
- The "print" statement will automatically append a new line character at the end of the output, which is incorrect for a binary image. The "binaryPrint" statement will not append any character at the end.