wiki:IntroductionToWidgets
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 6 (modified by anonymous, 13 years ago) (diff)

--

Introduction

Widgets are one of the most useful features added to TurboGears 0.9. Unfortunately, they're also one of the least-documented. This tutorial will explain why widgets are so useful, and show you how to create your own lightweight widgets, with a bonus introduction to the standard DataGrid widget.

The Fooball Application

We're going to create a web application that tracks statistics for the entirely fictitious sport of "Fooball". Fooball is a very simple game: players on teams do unspecified things to score as many points as possible before the end of the game. The team with the most points at the end wins. The player with the most points at the end gets lucrative sponsorship deals, but that's outside the scope of this tutorial.

The front page will contain a list of teams and a list of all league players and their stats. The list of teams will contain links to individual team pages, each listing stats for the players on that team. Simple enough.

I'll assume that you know enough TurboGears to "quickstart" the application; if not, see the TurboGears documentation. I'm going to quickstart the application using 'fooball' for both the application and module name, and I'll assume that you've properly configured the database connection in your "dev.cfg" file (I'm using an SQLite backend on Windows XP).

Now, let's define our data model in

Conclusion

Widgets are a powerful addition to the TurboGears tool set. They can be complex to write and use (especially when you get into the "form" and "fastdata") libraries, but they don't have to be. Wrapping custom display elements in simple widgets is quick and easy, and can help you develop faster and with fewer errors and display inconsistencies.