I have an external method that is capable of generating a PNG image in response to some input data. I would like to use this in the context of Zope to display a computed image on the fly. I am looking for suggestions on a couple of fronts: how can I wire this infrastructure into DTML and how does one write directly back to the browser from an external method? Here are my first guesses on both fronts. For DTML, my thinking is along the following lines: <IMG SRC="GenerateGraphics"> Here, I imagine that "GenerateGraphics" is an external method. I read somewhere (although I can't recall where), that an external method can take RESPONSE as an argument. So, on the second front, I am imagining that the external method uses RESPONSE to set the content type and then writes the results using the RESPONSE.write() method. Am I on the right track here? David S. Harrison (dsh@magma-da.com)
On Wed, 10 Nov 1999, David S. Harrison wrote:
for suggestions on a couple of fronts: how can I wire this infrastructure into DTML and how does one write directly back to the browser from an external method?
Hi David - You can actually just return the image file from the external method. For instance if your method is called gen_im def gen_im(self): imag=doimage... return imag then going to its URL will actually return the image. You might want to set the header through the Response object. Similarly you can just call the external method from DTML. (I think I am not clear but I haven't had my shakky coffee yet) Pavlos
Maybe I am not being clear. There is no image file. It will be generated completely from within the external method. I would like the external method to write to some sort of sink that will deliver the bits directly to the browser. If I can help it, I don't want the image to be written to disk at all. Pavlos Christoforou wrote:
On Wed, 10 Nov 1999, David S. Harrison wrote:
for suggestions on a couple of fronts: how can I wire this infrastructure into DTML and how does one write directly back to the browser from an external method?
Hi David -
You can actually just return the image file from the external method. For instance if your method is called gen_im
def gen_im(self): imag=doimage... return imag
then going to its URL will actually return the image. You might want to set the header through the Response object. Similarly you can just call the external method from DTML.
(I think I am not clear but I haven't had my shakky coffee yet)
Pavlos
On Thu, 11 Nov 1999, David S. Harrison wrote:
Maybe I am not being clear. There is no image file. It will be generated
No you were clear ...
completely from within the external method. I would like the external method to write to some sort of sink that will deliver the bits directly to the browser. If I can help it, I don't want the image to be written to disk at all.
Still I will repeat the original recipe ;-)
def gen_im(self): imag=<code to generate image on the fly> return imag
access it like: http://Zobjects.in.the.mirror.appear.harder/myfolder/gen_im ok maybe you need to set: RESPONSE.setHeader('content-type','<your image type'>) before you return the object Pavlos
Maybe I am not understanding your point. What is the type of 'imag' in your example? Is it a string? If so, that means that the external image must write the image to disk and that location must be visible to the ZServer. I would like to avoid it if possible. If 'imag' is some other type, how does ZServer know what to do with it? Pavlos Christoforou wrote:
On Thu, 11 Nov 1999, David S. Harrison wrote:
Maybe I am not being clear. There is no image file. It will be generated
No you were clear ...
completely from within the external method. I would like the external method to write to some sort of sink that will deliver the bits directly to the browser. If I can help it, I don't want the image to be written to disk at all.
Still I will repeat the original recipe ;-)
def gen_im(self): imag=<code to generate image on the fly> return imag
access it like:
http://Zobjects.in.the.mirror.appear.harder/myfolder/gen_im
ok maybe you need to set: RESPONSE.setHeader('content-type','<your image type'>) before you return the object
Pavlos
"David S. Harrison" wrote:
Maybe I am not understanding your point. What is the type of 'imag' in your example?
type is string contents are what your external method generated (or what would be written to the file if the generated image were saved to disk)
Is it a string? If so, that means that the external image must write the image to disk and that location must be visible to the ZServer.
There is no /law/ that strings should be written to disk :) You can just as well return them from functions.
I would like to avoid it if possible. If 'imag' is some other type, how does ZServer know what to do with it?
Zope has no need to know what to do with it, it just returns it to the browser that requested the url. You should set the Content-type: header though, so that the _browser_ would know that it is a png image ------------ Hannu
Pavlos is correct. What I didn't realize is that Zope treats the return value from an external method as the actual content that will be returned by the server (not the name of the content). As long as my routine can write the image into a Python string, it is easy to return that data correctly for use in DTML. The only trick is to make sure the content type is set appropriately: def GenerateImage(self, RESPONSE): # Image bits is a string containing the bytes of the image. imageBits = computeImage() RESPONSE.setHeader('content-type', 'image/png') return imageBits In the DTML, I can use a simple image tag to get access to this image: <!--#var standard_html_header--> <h2><!--#var title_or_id--></h2> <p> This is the <!--#var id--> Document. Below is a computed image: <IMG SRC="GenerateImage"> </p> <!--#var standard_html_footer--> All in all, it looks like this is easier than I expected. I am certainly glad I don't have to worry about writing the images to disk. Figuring out when those files expire is always a nightmare. David S. Harrison (dsh@magma-da.com) Pavlos Christoforou wrote:
def gen_im(self): imag=<code to generate image on the fly> return imag
David S. Harrison wrote:
I have an external method that is capable of generating a PNG image in response to some input data. I would like to use this in the context of Zope to display a computed image on the fly. I am looking for suggestions on a couple of fronts: how can I wire this infrastructure into DTML and how does one write directly back to the browser from an external method?
Here are my first guesses on both fronts. For DTML, my thinking is along the following lines:
<IMG SRC="GenerateGraphics">
This is HTML, not DTML. -Michel
participants (4)
-
David S. Harrison -
Hannu Krosing -
Michel Pelletier -
Pavlos Christoforou