Paul Johnston wrote:
> I would like to call a Python function from within a TG widget template. How
> can I do this? I tried:
>
> def myfunc(x):
> return 'test'
>
> class MyWidget(Widget):
> params = {'myfunc': 'pass a function in'}
> myfunc = myfunc
> template = '''... ${myfunc(1)}...'''
>
> But that caused "TypeError: 'str' object is not callable". I guess TG is
> spotting the param is a callable and (in an attempt to be helpful) calling
> it. So I tried:
>
> def outer():
> def myfunc(x):
> return 'text'
> return myfunc
>
> class MyWidget(Widget):
> params = {'myfunc': 'pass a function in'}
> myfunc = outer
> template = '''... ${myfunc(1)}...'''
>
> Now this just gives me "TypeError: 'NoneType' object is not callable" for a
> reason I cannot figure out.
The problem here seems to be that if you define it this way, myfunc
becomes a Widget method, but TurboGears expects a function.
You can try the following:
def myfunc(x):
return 'text'
class MyWidget(Widget):
params = {'myfunc': 'pass a function in'}
myfunc = staticmethod(lambda: myfunc)
template = '''... ${myfunc(1)}...'''
Anyway, I think you're right, this is not very intuitive. It should be
simpler to make functions or Widget methods available to the template,
and these error messages are not very helpful.
-- Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---