Johannes Erdfelt
johannes at erdfelt.com
Fri May 18 20:31:27 EDT 2007
On Fri, May 18, 2007, Robert Norman <tfcrobert at gmail.com> wrote: > I'm trying to create graphs on the fly with mod-python. Using PIL I can get > a "jpeg" image as a string. Is there some way to wrap this information in an > IMG tag? I gather that src parameter in IMG is required so anythink like: > > req.write("<IMG %s>" % myString) > > is not going to work. No, that certainly won't work. The problem is that the data of your generated JPEG image isn't text and so isn't appropriate to insert into HTML. > One suggestion was to: > > <img src="get-graph.py?graphId=xyz" /> > > where: > > "get-graph.py is a python script. it sets the header to "image/jpeg" or > whatever format you want to use than you can use some python libraries to > create the image. Instead of saving the image to a file you just write it to > stdout." > > I don't understand the suggestion especially in the context of mod-python > since we want to use req to write the page. It looks like you don't fully understand how web pages are fetched. The web browser fetchs the URL specified by the user (either by typing in, or clicking a link). It then parses the HTML and then starts loading all of the URLs pointed to by IMG elements. These are seperate HTTP requests, the only difference from the original request for HTML data is a different content-type. The suggestion was you shouldn't generate the images in the handler for the HTML page. You should generate the image in the handler for the graphics you're linking to in the original HTML page. Think of it as a layer of indirection. The HTML points to the images, which are seperate URLs and in the context of your mod_python application, it is also a different handler. The complexity comes in passing data from your main handler to the image handlers. This is usually done via the query string in the URL. Alternatively, you can also create the images in your main handler and save them to somewhere in the webroot which you can then use in the src attribute of the IMG tag. This usually isn't a good solution since most generated images are temporary and you'll have to remove the images you've saved after some period of time. > What is a reasonable method to generate dynamic image graphics? The aforementioned suggestion is the usual way of handling it. You can also embed the JPEG data in HTML as well, using the new data: URI scheme (which essentially takes image data and converts it into base64 and makes that the "URL"), but it's a bit complicated and I don't know how well supported it is in browsers right now. I'd consider this an advanced solution. JE
|