TurboGears Widgets
Kevin introduced today ‘TurboGears’s Widgets’ to the TG.mailing_list. I think this is going to be one of the coolest features for the upcoming release!
I couldn’t resist the itch to play with it right away, so I implemented a simple grid widget and a subclass of it, a dataGrid widget.
The dataGrid is nice, it takes a reference to a model and an object name as parameter.
To display a dataGrid in an application, import the reference to the widget in your controller file,
instantiate the widget and pass it to your template.
This is my sample controller file:
import turbogears
import model
from turbogears import controllers
from turbogears.widgets.grid import DataGrid
class Root(controllers.Root):
@turbogears.expose(html="freshtest.templates.welcome")
def index(self):
return dict(grid=DataGrid('test',model=model,view='Author'))
In your kid template just call the widget’s insert method:
<body> <h2>Data Grid test</h2> <div py:replace="grid.insert()"></div> </body> </html>
The resulting page looks like this:

The DataGrid use CatWalk to pull the object data, that means that you can configure your view in CatWalk (visible columns, label name for foreignKeys, order of columns, etc) and get that for free in your application.
November 11th, 2005 at 3:29 pm
Wonderful!
So it seems as CatWalk internals are easily reusable by widgets and other components, am I wrong?
CRUDController
Ciao
November 11th, 2005 at 3:46 pm
It’s not difficult to use CatWalk to retrieve your objects data, the grid sample uses catwalk.getInstances(). The method returns a dictionary containing a list of rows, a list of headers and a list of hidden column.
November 11th, 2005 at 3:55 pm
That’s great, you have done a great job not only from a UI POV.
By the way, with CRUDController I was meaning that in the future (in my previous comment WordPress stripped my
November 11th, 2005 at 3:56 pm
:-( Ok, WordPress hates me.
…in the future it will be pretty easy to implement it thanks to CatWalk and TG widgets.
Ciao
Michele
November 11th, 2005 at 8:31 pm
DataGrid(’test’,model=model,view=’Author’)
Can you share your model code? I’m assuming Author is a SQLObject, but am not sure.
Thanks in advance!
November 11th, 2005 at 8:37 pm
Sure, there is nothing special about it though:
from sqlobject import *
from turbogears.database import PackageHub
hub = PackageHub(”freshtest”)
__connection__ = hub
class Author(SQLObject):
name = StringCol(length = 255)
description = StringCol()
books = MultipleJoin(’Book’)
class Book(SQLObject):
name = StringCol(length = 255)
description = StringCol()
author = ForeignKey(’Author’)
Author.createTable(ifNotExists=True)
Book.createTable(ifNotExists=True)