Parameters for external methods and page templates
Hello, I'm quiet happy with the example of using External Methods with PIL which can be found at http://www.zopelabs.com/cookbook/1010702887 I can perfectly call this method using <img src="tumb?width=42&height=42"> and get an thumbnail in the size I want it to be. Remark: I had to use a default image name (see below) because I did not found out the correct syntax for specifying an image name. On the other hand I would like to use this external method with pages templates and I failed while having no clue to specify the parameters.s The problem is the first parameter def MakeThumbHi2(self, image="bland.gif", width, height): ^^^^ self. I really have no idea how to put this into the correct syntax from page templates. When reading the Zope Book I noticed that the examples are exclusively realized using Python Scripts and these are glued in a DTML document. Is this the only way to go here to avoid the problem of specifying the "self" argument and arguments of type string? Kind regards Andreas.
On Thu, 12 Sep 2002, Dieter Maurer wrote:
Andreas Tille writes:
def MakeThumbHi2(self, image="bland.gif", width, height): ^^^^
This is invalid Python. You must not have arguments without default after one with a default value. Thanks. This was the solution. Beeing not very familiar with python I've thought that self is an exception from this rule because this would mean that I can not have any default arguments if there is a self argument. Or can I use self even at the end of the parameter list?
Once one problem is solved - I'm facing the next (as usual :) ). The Python Method I'm using is rendering a PNG image and returns it as string (right es it is done in http://www.zopelabs.com/cookbook/1010702887). I can use it in dtml as <img src="inzidenz?argument=1" alt="Comment" /> and the page is rendered as expected. But I found no working way to use any kind of Python-like syntax. I tried several things with page templates and dtml. The only thing happened when I tried Python like syntax was that the rendered string was put inplace and I've got something like <img src="<binary PNG image inserted here>" alt="Comment" /> which is bviousely not what I want. Moreover I have real trouble in debugging the correctness of the parameter values. I tried something like fp = open('/tmp/my_debug.log', 'a') print >>fp, "argument = " + argument fp.close() in the External Python method. I have no clue why this file is not created or appended at all. Kind regards Andreas.
On Mon, Sep 16, 2002 at 02:02:47PM +0200, Andreas Tille wrote:
On Thu, 12 Sep 2002, Dieter Maurer wrote:
Andreas Tille writes:
def MakeThumbHi2(self, image="bland.gif", width, height): ^^^^
This is invalid Python. You must not have arguments without default after one with a default value. Thanks. This was the solution. Beeing not very familiar with python I've thought that self is an exception from this rule because this would mean that I can not have any default arguments if there is a self argument. Or can I use self even at the end of the parameter list?
No no no - you can't put self anywhere but at the beginning. (In fact, self is not a keyword; it's a convention. You can call the self-referencing variable anything you want, it's just the first argument to a method.) self is not the problem in your code. You don't have to give a default value to the first argument, ever. The problem in your case is that width and height do not have default values, but they follow image, which does. -- Paul Winkler "Welcome to Muppet Labs, where the future is made - today!"
On Mon, Sep 16, 2002 at 02:02:47PM +0200, Andreas Tille wrote:
The Python Method I'm using is rendering a PNG image and returns it as string (right es it is done in http://www.zopelabs.com/cookbook/1010702887).
I can use it in dtml as
<img src="inzidenz?argument=1" alt="Comment" />
and the page is rendered as expected. But I found no working way to use any kind of Python-like syntax. I tried several things with page templates and dtml. The only thing happened when I tried Python like syntax was that the rendered string was put inplace and I've got something like
<img src="<binary PNG image inserted here>" alt="Comment" />
which is bviousely not what I want.
This is very odd; looking at that zopelabs recipe, I don't even understand why it works in DTML. The external method returns the actual image data, not a URL to an image object. What is "argument" in your version? What does "View source" show you when you look at the rendered img tag from DTML? -- Paul Winkler "Welcome to Muppet Labs, where the future is made - today!"
On Mon, 16 Sep 2002, Paul Winkler wrote:
This is very odd; looking at that zopelabs recipe, I don't even understand why it works in DTML. The external method returns the actual image data, not a URL to an image object. Yes, that's correct and intended. I'm creating a dynamical image. I'm using a template gif (containing a geographic map - each region with a different palette entry) and obtain a parameter for each geographic region from a database and set the color of the region accordingly. The resulting image is returned and can be viewed inside an <img>-tag. If you call the URL of the External Method explicitely the browser just presents the image data as bytes in ASCII-text - but this is no problem in my opinion.
What is "argument" in your version? Well any argument where I want to have influenze on the parameters I want to obtain from the database. Just any integer later I will add one or two strings - perhaps a more complex structure.
What does "View source" show you when you look at the rendered img tag from DTML? As I said if the External Method "method" is called like
<img src="method" ...> the image is displayed as expected. Kind regards Andreas.
On Tue, Sep 17, 2002 at 11:33:49AM +0200, Andreas Tille wrote:
As I said if the External Method "method" is called like
<img src="method" ...>
the image is displayed as expected.
OK, I see. You're not calling the method in your DTML, you're just using it as a src target. so why can't you do that in a page template? Can you give an example of what you did in a template? If you do something like <img tal:replace="here/method">... then of course you'll get binary gunk inline. That calls the method and inserts the result as a string. So don't do that. :) -- Paul Winkler "Welcome to Muppet Labs, where the future is made - today!"
On Tue, 17 Sep 2002, Paul Winkler wrote:
On Tue, Sep 17, 2002 at 11:33:49AM +0200, Andreas Tille wrote:
As I said if the External Method "method" is called like
<img src="method" ...>
the image is displayed as expected.
OK, I see. You're not calling the method in your DTML, you're just using it as a src target. so why can't you do that in a page template? Can you give an example of what you did in a template? If you do something like <img tal:replace="here/method">... then of course you'll get binary gunk inline. That calls the method and inserts the result as a string. So don't do that. :) It took some time to make it clear in my mind. Sorry for bothering you with my slow understanding of simple facts ...
Thanks for your help Andreas.
Andreas Tille writes:
I can use it in dtml as
<img src="inzidenz?argument=1" alt="Comment" /> In TAL, it looks the same ;-)
It's because it is pure HTMLm not DTML at all.
.... Moreover I have real trouble in debugging the correctness of the parameter values. I tried something like
fp = open('/tmp/my_debug.log', 'a') print >>fp, "argument = " + argument fp.close() There are only two possible reasons: the code is not executed or raises an exception.
Dieter
participants (3)
-
Andreas Tille -
Dieter Maurer -
Paul Winkler