1.9.3 Webserver Example

It is useful to be able to produce graphs in a script and then return them. The example below generates a graph. It can be used in an HTML tage like this <img src="webserver-web-image-graph.py" alt="Graph" />.

#!/usr/bin/env python

"""Graph Generation Example. 
<img src="webserver-web-image-graph-web.py" alt="Graph" />"""

# show python where the web modules are
import sys, os

import web.error; web.error.handle()
import web.image, web.image.graph

graph = web.image.graph.BarGraph(
    xAxis={'max':10, 'unit':1, 'label':'Days Since Send'}, 
    yAxis={'max':10, 'unit':1, 'label':'Number of Page Views'},
    size=(500, 300),
    title='Page View Rate For Newsletter',
print web.header('image/png'), graph.toString('png')

You can test this example by starting the test webserver in scripts/webserver.py and visiting http://localhost:8080/doc/src/lib/webserver-web-image-graph.py on your local machine. You will need the Arial.ttf font somewhere on your system where Python can find it.