How to generate graphics dynamically on the web using Python CGI script?

Steve Holden steve at
Fri Jan 20 05:35:02 EST 2006

Debashis Dey wrote:
> Hello,
> I have a python CGI program. I would like to show a graph within a HTML 
> plage. I would like to dynamically generate the graph using the python 
> CGI script on the web server side and send it to the browser.
> My question is how can I do this in python? Is there a free tool to do 
> this? Can someone please send me some simple python code to draw simple 
> graphics within HTML (e.g. draw a line or a circle).
Unfortunately HTML does not include graphics facilities, simply the 
ability to refer to graphical resources.

The typical way to include a created graphic would be:

  1. Create a (.png, .jpg, .gif) file showing the
     image you want

  2. Store it in a temporary file whose name will be
     unique to the current session

  3. Generate an HTML response including an <IMG ...>
     tag referring to the newly created graphic

Step 1, which is what you seem to be asking about, can be handled by a 
number of packages, perhaps the best-known of which is PIL, the Python 
Imaging Library - see

This is open source, and available at no cost. There's a wealth of 
information on how to use it on the web, and many regular readers of 
this group are skilled with it!

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC           
PyCon TX 2006        

More information about the Python-list mailing list