| A quick tutorial on Traits, Traits UI and Chaco |
|
|
|
| Written by Administrator |
| Sunday, 16 August 2009 17:09 |
|
Traits is a Python package from Enthought. Traits allows us to easily give our variable an explicit type with optional bounds checking(this helps us to catch bugs as we develop our software). Traits supports reactive programming, which Wikipedia defines as "Reactive programming is a programming paradigm oriented around data flows and the propagation of change.". This sounds like exactly what we wish to do when writing a scientific application. Chaco is a visualisation package from Enthought, which produces excellent plots and integrates well with Traits. This tutorial begins with a quick introduction to Traits and then presents and discusses a simple application. This application will present the user with a graph and three sliders. The sliders will set the coefficients of a polynomial. The resulting polynomial will be displayed and updated as the sliders are moved.
Lets start with a simple demo of the traits package.
Here we have created a class 'Person' with the attributes name,age,height,weight. These attributes have a type associated with them, and will only accept data of that type (casting and coercion rules apply), if we give an unacceptable value Traits returns a nice descriptive error. We then create a Person instance and call its 'configure_traits' method. Running this program we see.
Traits has very kindly provided us with a GUI to configure this instance with, the layout can be customised to our needs. If we supply bad data the UI highlights this and blocks us from continuing.
Make some changes and click OK, and the new values are displayed in the terminal. All this has been achieved with minimal coding effort on the part of the user, there has been no need to pack widgets and write callbacks, Traits knows how to display a float,string etc and provides the appropriate display elements for us. Now lets try something a little more useful. Lets add a BMI attribute to our class. We have specified that bmi is a property of type Float, which depends on two things height and weight. Whenever we change one of these Traits will look for a method "_get_bmi" to calculate the new bmi (in fact Traits delays this call until the point when the attribute is actually needed). Running this code we see the bmi field has been filled in, if we change either the height or weight, the new bmi is calculated.
The view is quite simple, we set three traits and give them a label, we then add some buttons. The revert button allows us to revert any changes we have made to the instance. Items can be grouped together easily. To finish up lets present the graph plotting app, I believe its quite readable. We define five traits. The first three are coefficients for the polynomial limited to the range (-5,5). Then comes the array 'xdata' which has a default value set by '_xdata_default' . The trait ydata is more interesting, it is defined as a being a property which depends on the coefficients, when the coefficients change the polynomial is recalculated. The plot, an instance of 'ChacoPlotItem' knows that it depends on 'xdata' and 'ydata' and will replot itself whenever they change. Then comes the view which displays the a plot window and the sliders.
For a little over 30 lines of code, this quite a powerful app. |
| Last Updated on Sunday, 16 August 2009 20:51 |