Skip to content

Commit 4e6fb72

Browse files
author
Jonathan Rocher
committed
Added table demo for traits
1 parent 01396e6 commit 4e6fb72

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

Traits/Tabular_editor_demo.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
"""
2+
Tabular editor
3+
4+
The TabularEditor can be used for many of the same purposes as the TableEditor,
5+
that is, for displaying a table of attributes of lists or arrays of objects.
6+
7+
While similar in function, the tabular editor has advantages and disadvantages
8+
relative to the table editor. See the Traits UI User Manual for details.
9+
10+
This example defines three classes:
11+
12+
- *Person*: A single person.
13+
- *MarriedPerson*: A married person (subclass of Person).
14+
- *Report*: Defines a report based on a list of single and married people.
15+
16+
It creates a tabular display of 10,000 single and married people showing the
17+
following information:
18+
19+
- Name of the person.
20+
- Age of the person.
21+
- The person's address.
22+
- The name of the person's spouse (if any).
23+
24+
In addition:
25+
26+
- It uses a Courier 10 point font for each line in the table.
27+
- It displays age column right, instead of left, justified.
28+
- If the person is a minor (age < 18) and married, it displays a red flag
29+
image in the age column.
30+
- If the person is married, it makes the background color for that row a light
31+
blue.
32+
33+
This example demonstrates:
34+
35+
- How to set up a *TabularEditor*.
36+
- The display speed of the *TabularEditor*.
37+
- How to create a *TabularAdapter* that meets each of the specified display
38+
requirements.
39+
40+
Additional notes:
41+
42+
- You can change the current selection using the up and down arrow keys.
43+
- You can move a selected row up and down in the table using the left and
44+
right arrow keys.
45+
"""
46+
47+
from random import randint, choice, shuffle
48+
from traits.api import HasTraits, Str, Int, List, Instance, Property, Constant,\
49+
Color
50+
from traitsui.api import View, Group, Item, TabularEditor, CancelButton
51+
from traitsui.tabular_adapter import TabularAdapter
52+
53+
#-- Person Class Definition ----------------------------------------------------
54+
55+
class Person(HasTraits):
56+
57+
name = Str
58+
address = Str
59+
age = Int
60+
61+
#-- MarriedPerson Class Definition ---------------------------------------------
62+
63+
class MarriedPerson(Person):
64+
65+
partner = Instance(Person)
66+
67+
#-- Tabular Adapter Definition -------------------------------------------------
68+
69+
class ReportAdapter(TabularAdapter):
70+
71+
columns = [ ('Name', 'name'),
72+
('Age', 'age'),
73+
('Address', 'address'),
74+
('Spouse', 'spouse') ]
75+
76+
# Font fails with wx in OSX; see traitsui issue #13:
77+
# font = 'Courier 10'
78+
age_alignment = Constant('right')
79+
MarriedPerson_age_image = Property
80+
MarriedPerson_bg_color = Color(0xE0E0FF)
81+
MarriedPerson_spouse_text = Property
82+
Person_spouse_text = Constant('')
83+
84+
def _get_MarriedPerson_age_image(self):
85+
if self.item.age < 18:
86+
return '@icons:red_ball'
87+
88+
return None
89+
90+
def _get_MarriedPerson_spouse_text(self):
91+
return self.item.partner.name
92+
93+
#-- Tabular Editor Definition --------------------------------------------------
94+
95+
tabular_editor = TabularEditor(
96+
adapter = ReportAdapter(),
97+
operations = [ 'move' ],
98+
)
99+
100+
#-- Report Class Definition ----------------------------------------------------
101+
102+
class Report(HasTraits):
103+
104+
people = List(Person)
105+
106+
view = View(
107+
Group(
108+
Item('people', id = 'table', editor = tabular_editor),
109+
show_labels = False
110+
),
111+
title = 'Tabular Editor Demo',
112+
id = 'traitsui.demo.Applications.tabular_editor_demo',
113+
width = 0.60,
114+
height = 0.75,
115+
resizable = True,
116+
buttons = [CancelButton]
117+
)
118+
119+
def _people_items_changed(self):
120+
print "CHanged"
121+
122+
if __name__ == '__main__':
123+
124+
#-- Generate 10,000 random single and married people ---------------------------
125+
126+
male_names = [ 'Michael', 'Edward', 'Timothy', 'James', 'George', 'Ralph',
127+
'David', 'Martin', 'Bryce', 'Richard', 'Eric', 'Travis', 'Robert', 'Bryan',
128+
'Alan', 'Harold', 'John', 'Stephen', 'Gael', 'Frederic', 'Eli', 'Scott',
129+
'Samuel', 'Alexander', 'Tobias', 'Sven', 'Peter', 'Albert', 'Thomas',
130+
'Horatio', 'Julius', 'Henry', 'Walter', 'Woodrow', 'Dylan', 'Elmer' ]
131+
132+
female_names = [ 'Leah', 'Jaya', 'Katrina', 'Vibha', 'Diane', 'Lisa', 'Jean',
133+
'Alice', 'Rebecca', 'Delia', 'Christine', 'Marie', 'Dorothy', 'Ellen',
134+
'Victoria', 'Elizabeth', 'Margaret', 'Joyce', 'Sally', 'Ethel', 'Esther',
135+
'Suzanne', 'Monica', 'Hortense', 'Samantha', 'Tabitha', 'Judith', 'Ariel',
136+
'Helen', 'Mary', 'Jane', 'Janet', 'Jennifer', 'Rita', 'Rena', 'Rianna' ]
137+
138+
all_names = male_names + female_names
139+
140+
male_name = lambda: choice(male_names)
141+
female_name = lambda: choice(female_names)
142+
any_name = lambda: choice(all_names)
143+
age = lambda: randint(15, 72)
144+
145+
family_name = lambda: choice([ 'Jones', 'Smith', 'Thompson', 'Hayes', 'Thomas', 'Boyle',
146+
"O'Reilly", 'Lebowski', 'Lennon', 'Starr', 'McCartney', 'Harrison',
147+
'Harrelson', 'Steinbeck', 'Rand', 'Hemingway', 'Zhivago', 'Clemens',
148+
'Heinlien', 'Farmer', 'Niven', 'Van Vogt', 'Sturbridge', 'Washington',
149+
'Adams', 'Bush', 'Kennedy', 'Ford', 'Lincoln', 'Jackson', 'Johnson',
150+
'Eisenhower', 'Truman', 'Roosevelt', 'Wilson', 'Coolidge', 'Mack', 'Moon',
151+
'Monroe', 'Springsteen', 'Rigby', "O'Neil", 'Philips', 'Clinton',
152+
'Clapton', 'Santana', 'Midler', 'Flack', 'Conner', 'Bond', 'Seinfeld',
153+
'Costanza', 'Kramer', 'Falk', 'Moore', 'Cramdon', 'Baird', 'Baer',
154+
'Spears', 'Simmons', 'Roberts', 'Michaels', 'Stuart', 'Montague',
155+
'Miller' ])
156+
157+
address = lambda: '%d %s %s' % (randint(11, 999), choice([ 'Spring',
158+
'Summer', 'Moonlight', 'Winding', 'Windy', 'Whispering', 'Falling',
159+
'Roaring', 'Hummingbird', 'Mockingbird', 'Bluebird', 'Robin', 'Babbling',
160+
'Cedar', 'Pine', 'Ash', 'Maple', 'Oak', 'Birch', 'Cherry', 'Blossom',
161+
'Rosewood', 'Apple', 'Peach', 'Blackberry', 'Strawberry', 'Starlight',
162+
'Wilderness', 'Dappled', 'Beaver', 'Acorn', 'Pecan', 'Pheasant', 'Owl' ]),
163+
choice([ 'Way', 'Lane', 'Boulevard', 'Street', 'Drive', 'Circle',
164+
'Avenue', 'Trail' ]))
165+
166+
people = [ Person(name = '%s %s' % (any_name(), family_name()),
167+
age = age(),
168+
address = address()) for i in range(5000) ]
169+
170+
marrieds = [ (MarriedPerson(name = '%s %s' % (female_name(), last_name),
171+
age = age(),
172+
address = address),
173+
MarriedPerson(name = '%s %s' % (male_name(), last_name),
174+
age = age(),
175+
address = address))
176+
for last_name, address in
177+
[ (family_name(), address()) for i in range(2500) ] ]
178+
179+
for female, male in marrieds:
180+
female.partner = male
181+
male.partner = female
182+
people.extend([ female, male ])
183+
184+
shuffle(people)
185+
186+
demo = Report(people = people)
187+
demo.configure_traits()
188+

0 commit comments

Comments
 (0)