Displaying an image for a boolean column on a DataGrid

3 views
Skip to first unread message

Jorge Godoy

unread,
Feb 5, 2006, 11:06:55 AM2/5/06
to TurboGears

Hi!


Without subclassing DataGrid, is it possible to show an image for each value
of a boolean column in a DataGrid?


TIA,
--
Jorge Godoy <jgo...@gmail.com>

Lee McFadden

unread,
Feb 6, 2006, 5:59:13 AM2/6/06
to turbo...@googlegroups.com
On 2/5/06, Jorge Godoy <jgo...@gmail.com> wrote:
>
> Without subclassing DataGrid, is it possible to show an image for each value
> of a boolean column in a DataGrid?
>

You could just create an instance and overwrite the .template
attribute. Just copy/paste the template into your controller from the
widget's source, instanciate the widget and then assign your modified
template to widget_instance.template.

Unless the new api forbids this (which afaik it doesn't) that should
be all you need to do using a py:if in the template to display the
image.

Lee

Alberto Valverde

unread,
Feb 6, 2006, 6:37:00 AM2/6/06
to turbo...@googlegroups.com

On Feb 6, 2006, at 11:59 AM, Lee McFadden wrote:
>
> You could just create an instance and overwrite the .template
> attribute. Just copy/paste the template into your controller from the
> widget's source, instanciate the widget and then assign your modified
> template to widget_instance.template.
>
> Unless the new api forbids this (which afaik it doesn't) that should
> be all you need to do using a py:if in the template to display the
> image.
>
> Lee

No, you cannot do that! The new API explicitly forbids changing
widget attributres once the widget is initialized. You can, however,
override the template when you initialize your widget:

Bad:
widget = widgets.TextField()
widget.template = mytemplate
ValueError: It is not threadsafe to modify widgets in a request

Good:
widget = widgets.TextField(template=mytemplate)
asset widget.template == mytemplate

Hoope it helps,
Alberto

Lee McFadden

unread,
Feb 6, 2006, 6:40:13 AM2/6/06
to turbo...@googlegroups.com
On 2/6/06, Alberto Valverde <alb...@toscat.net> wrote:
>
> No, you cannot do that! The new API explicitly forbids changing
> widget attributres once the widget is initialized. You can, however,
> override the template when you initialize your widget:
>
> Bad:
> widget = widgets.TextField()
> widget.template = mytemplate
> ValueError: It is not threadsafe to modify widgets in a request
>
> Good:
> widget = widgets.TextField(template=mytemplate)
> asset widget.template == mytemplate
>

You are correct. That's what I get for answering questions before my
morning coffee. :)

Jorge Godoy

unread,
Feb 6, 2006, 6:44:18 AM2/6/06
to turbo...@googlegroups.com
Alberto Valverde <alb...@toscat.net> writes:

> Good:
> widget = widgets.TextField(template=mytemplate)

This helps. I'll see what I can come up with for DataGrid here. Thanks.

--
Jorge Godoy <jgo...@gmail.com>

Jorge Godoy

unread,
Feb 6, 2006, 6:46:04 AM2/6/06
to turbo...@googlegroups.com
Lee McFadden <sple...@gmail.com> writes:

> You are correct. That's what I get for answering questions before my
> morning coffee. :)

Hey! If you told me that I'd invite you for coffee! There's a nice and
strong brazilian coffee here -- I just made some -- with some bread, butter,
cheese (mozzarela)... ;-) You pay the plane tickets and I'll pay you the
coffee ;-)

--
Jorge Godoy <jgo...@gmail.com>

Alberto Valverde

unread,
Feb 6, 2006, 6:55:13 AM2/6/06
to turbo...@googlegroups.com
Wait! Before you shoot yourself on the foot (or someplace worst ;).

I've just found out that mytemplate should be an already compiled
template. This is because the widget __metaclass__ compiles the
template when classes are initiialized, not when instances are. You
can override the template as I told you, but make sure:

repr(mytemplate) prints something like:

<turbogears.widgets.newforms.ListForm.Template.Template object at
0x17769d0>

RFC:

How about fixing the widget constructor so it does this automatically
for us?? I think it would be a good idea as users would normally
override templates with a string, not an already compiled template...

I can write a patch as soon as i get a green light as I've booked
myself exclusively for TG development/testing today ;)

Alberto

Jorge Godoy

unread,
Feb 6, 2006, 7:04:11 AM2/6/06
to turbo...@googlegroups.com
Alberto Valverde <alb...@toscat.net> writes:

> Wait! Before you shoot yourself on the foot (or someplace worst ;).

The foot! Please, let it be in the foot! :-)

> I've just found out that mytemplate should be an already compiled
> template. This is because the widget __metaclass__ compiles the template when
> classes are initiialized, not when instances are. You can override the
> template as I told you, but make sure:
>
> repr(mytemplate) prints something like:
>
> <turbogears.widgets.newforms.ListForm.Template.Template object at 0x17769d0>

And how can I achieve that? Using something like serialize()?

> RFC:
>
> How about fixing the widget constructor so it does this automatically for
> us?? I think it would be a good idea as users would normally override
> templates with a string, not an already compiled template...

+1 for that. And let it be a Kid file as well.

> I can write a patch as soon as i get a green light as I've booked myself
> exclusively for TG development/testing today ;)

I wish I could do that as well, but I'm a lot late with two apps...

--
Jorge Godoy <jgo...@gmail.com>

Lee McFadden

unread,
Feb 6, 2006, 7:11:25 AM2/6/06
to turbo...@googlegroups.com
On 2/5/06, Jorge Godoy <jgo...@gmail.com> wrote:
>You pay the plane tickets and I'll pay you the coffee ;-)

As tempting as that sounds, I'm not sure I'd want to stump up that
much for a cup of coffee... if I want to get ripped off I can just
head down to my local <insert coffee franchise here>. ;)

The alternative to having to compile a template would be to subclass
of course. This is what I do, and when simply changing a template
it's never too onerous a task. By the time you've compiled your
template and made your widget instance I'm betting the amount of code
will be about the same, only the "modify the instance" method would be
less clear imho.

Lee

Alberto Valverde

unread,
Feb 6, 2006, 7:41:45 AM2/6/06
to turbo...@googlegroups.com

On Feb 6, 2006, at 1:04 PM, Jorge Godoy wrote:
>
> And how can I achieve that? Using something like serialize()?

kid.load_template(template_string, name=module_name)

module_name is the name of the module you want the tempate module to
be called (I think)

>
>> RFC:
>>
>> How about fixing the widget constructor so it does this
>> automatically for
>> us?? I think it would be a good idea as users would normally
>> override
>> templates with a string, not an already compiled template...
>
> +1 for that. And let it be a Kid file as well.
>

Check out:
http://trac.turbogears.org/turbogears/attachment/ticket/520/
newbase_template_overridal.patch

I've implemented it and written a test for it, from running the tests
at newfastdata and widgets it seems that nothing is broken.

*I think* it should work if you specify a template filename as well,
but I would appreciate if you can test it (if possible) just to be
sure... (a unittest would be soooo great ;)

>> I can write a patch as soon as i get a green light as I've booked
>> myself
>> exclusively for TG development/testing today ;)
>
> I wish I could do that as well, but I'm a lot late with two apps...
>

Yeah, me too... :) I thought TG would make me so fast that I've
miscalculated two delivery dates... probably wouldn't have
miscalculated if TG was at 0.9 though (as i *really* think it will be
a great framework once it's there...). But now i've got some pressure
removed from by back i think the best thing I can do is help to get
it there ASAP by getting my hands dirty with it.

Regards,
Alberto


Alberto

unread,
Feb 6, 2006, 8:14:11 AM2/6/06
to TurboGears
Nevermind about the unittest, I've written it and works...

Jorge Godoy

unread,
Feb 6, 2006, 9:01:09 AM2/6/06
to turbo...@googlegroups.com
Lee McFadden <sple...@gmail.com> writes:

> The alternative to having to compile a template would be to subclass
> of course. This is what I do, and when simply changing a template
> it's never too onerous a task. By the time you've compiled your
> template and made your widget instance I'm betting the amount of code
> will be about the same, only the "modify the instance" method would be
> less clear imho.

Hmmm... I have to think about it. Subclassing doesn't seem all that bad, but
if it was possible to change the template passing either a file or a Kid
template it would be really great (depending on the interface, it would also
solve some problems and allow using other templating engines more easily...).

Thanks for your help.

--
Jorge Godoy <jgo...@gmail.com>

Michele Cella

unread,
Feb 6, 2006, 10:08:28 AM2/6/06
to TurboGears
Jorge Godoy wrote:
>
> Hmmm... I have to think about it. Subclassing doesn't seem all that bad, but
> if it was possible to change the template passing either a file or a Kid
> template it would be really great (depending on the interface, it would also
> solve some problems and allow using other templating engines more easily...).
>
> Thanks for your help.

You can already do that by passing the template argument at the widget
constructor.

Now thanks to Alberto you can also pass a template string (that should
be a kid template).

I guess passing template using the template plug-in schema should also
work.

Ciao
Michele

Jorge Godoy

unread,
Feb 6, 2006, 1:48:06 PM2/6/06
to turbo...@googlegroups.com
"Michele Cella" <michel...@gmail.com> writes:

> You can already do that by passing the template argument at the widget
> constructor.

It didn't work for turbogears.fastdata.datawidgets.DataGrid. Subclassing
solved the problem and my new class has just two lines:

class ClienteGrid(datawidgets.DataGrid):
template = "site_amostras.templates.grid_clientes_template"


--
Jorge Godoy <jgo...@gmail.com>

Alberto

unread,
Feb 6, 2006, 4:49:40 PM2/6/06
to TurboGears
Hi,

turbogears.fastdata.datawidgets.DataGrid is an old-style widget... try
turbogears.newfastdata.datawidgets.DataGrid if you want to override at
construction time.

Regards,
Alberto

Reply all
Reply to author
Forward
0 new messages