Skip to content

Commit 9361dcd

Browse files
Several edits to address review comments.
1 parent 5d7ef90 commit 9361dcd

3 files changed

Lines changed: 68 additions & 99 deletions

File tree

slides/01_intro.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If you haven't already:
2929
- Install packages:
3030
- Using Enthought Deployment Manager (recommended)
3131
(https://www.enthought.com/edm):
32-
32+
3333
```bash
3434
edm envs create bootstrap
3535
edm install --environment bootstrap click
@@ -125,12 +125,12 @@ XXX Screenshot of final application
125125
<!-- #region slideshow={"slide_type": "slide"} -->
126126
## Schedule
127127

128-
- Step 0: Python script
129-
- Step 1: Using Traits.
130-
- Step 2: Basic GUI using TraitsUI
131-
- Step 3: PyFace application: tree navigator
132-
- Step 4: More features
133-
- Step 5: Installer
128+
- Step 1: Python script
129+
- Step 2: Using Traits.
130+
- Step 3: Basic GUI using TraitsUI
131+
- Step 4: PyFace application: tree navigator
132+
- Step 5: More features
133+
- Step 6: Installer
134134

135135

136136
<!-- #endregion -->

slides/02_traits.md

Lines changed: 53 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jupyter:
4141
<!-- #region slideshow={"slide_type": "slide"} -->
4242
## Why all this?
4343

44-
- No pain, no gain!
44+
- No pain, no gain! (Only a little pain, we promise!)
4545

4646
- Small change to thinking yields big benefits
4747

@@ -181,7 +181,7 @@ class Child(HasStrictTraits):
181181

182182
@observe('father.last_name')
183183
def _dad_name_updated(self, event):
184-
print('DAD name', self.father.last_name)
184+
print(Father name changed to', self.father.last_name)
185185

186186
```
187187

@@ -194,25 +194,11 @@ c = Child(father=Parent)
194194
dad.last_name = 'Valderrama'
195195
```
196196

197-
```python
198-
def handler(event):
199-
print("handler", event.object, event.name, event.old, event.new)
200-
```
201-
202-
```python
203-
c = Child(father=Parent(last_name='Ram'))
204-
c.observe(handler, 'father, age')
205-
```
206-
207197
<!-- #region slideshow={"slide_type": "slide"} -->
208198
## Trait change notification
209199

210200
- Static: `def _<trait_name>_changed()`
211201
- Decorator: `@observe('extended.trait.name')`
212-
- Dynamic:
213-
214-
```obj.observe(handler, 'extended.trait.name')
215-
```
216202

217203
- See documentation: https://docs.enthought.com/traits/traits_user_manual/notification.html
218204

@@ -245,8 +231,8 @@ class Child(HasStrictTraits):
245231
age = Int
246232
father = Instance(Parent)
247233
first_name = Str('')
248-
alive = Bool(True)
249-
gender = Enum('female', 'male', 'neither')
234+
likes_queso = Bool(True)
235+
handedness = Enum('right', 'left')
250236

251237
def _age_changed(self, old, new):
252238
print('Age changed from %s to %s ' % (old, new))
@@ -259,113 +245,96 @@ class Child(HasStrictTraits):
259245

260246
```python
261247
p = Parent(last_name='Ray')
262-
c = Child(age=21, father=p, first_name='Romano', gender='male')
248+
c = Child(age=21, father=p, first_name='Romano', handedness='right')
263249
```
264250

265-
<!-- #region slideshow={"slide_type": "slide"} -->
266-
## Setting default values
267251

268-
- For simple cases, use the default of the trait
269-
- For more complex cases use a special method
270-
- A simple example
252+
<!-- #region slideshow={"slide_type": "slide"} -->
253+
## Trait change event
271254

255+
- Recall this
272256
<!-- #endregion -->
273257

274258
```python
275-
import datetime
259+
@observe('father.last_name')
260+
def _dad_name_updated(self, event):
261+
print('Dad name', self.father.last_name)
262+
```
276263

277-
from traits.api import HasStrictTraits, Date, Range
264+
- `event` is a `TraitChangeEvent` instance
278265

279-
class Thing(HasStrictTraits):
280-
date = Date()
281-
age = Int(12)
282266

283-
def _date_default(self):
284-
print('default')
285-
return datetime.datetime.today()
286-
```
267+
```python slideshow={"slide_type": "slide"}
268+
class Child(HasStrictTraits):
269+
age = Int
270+
father = Instance(Parent)
271+
first_name = Str('')
272+
likes_queso = Bool(True)
273+
handedness = Enum('right', 'left')
287274

275+
def _age_changed(self, old, new):
276+
print('Age changed from %s to %s ' % (old, new))
277+
278+
@observe('father.last_name')
279+
def _dad_name_updated(self, event):
280+
print(event.object, event.name, event.old, event.new)
281+
print('Dad name', self.father.last_name)
282+
283+
```
288284

289285
```python
290-
t = Thing()
286+
p = Parent(last_name='Ray')
287+
c = Child(age=21, father=p, first_name='Romano', handedness='right')
291288
```
292289

293290
```python
294-
type(c.age)
291+
p.last_name = 'Ahmed'
295292
```
296293

297294
<!-- #region slideshow={"slide_type": "slide"} -->
298-
## Trait Lists
295+
## Other Trait Events
299296

297+
- Advanced, for other kind of traits
298+
- `List` trait changes: `ListChangeEvent`
299+
- `Dict` trait changes: `DictChangeEvent`
300+
- `Set` trait changes: `SetChangeEvent`
300301

301302
<!-- #endregion -->
302303

303-
```python
304-
from traits.api import List
305-
306-
class Bowl(HasStrictTraits):
307-
fruits = List(Str)
308-
309-
def _fruits_changed(self, o, n):
310-
print("Fruits changed", o, n)
311-
312-
```
313-
314-
```python
315-
b = Bowl()
316-
b.fruits = ['apple']
317-
b.fruits.append('mango')
318-
```
319304

320305
<!-- #region slideshow={"slide_type": "slide"} -->
321-
## Trait List events
306+
## Setting default values
307+
308+
- For simple cases, use the default of the trait
309+
- For more complex cases use a special method
310+
- A simple example
322311

323312
<!-- #endregion -->
324313

325314
```python
326-
class Bowl(HasStrictTraits):
327-
fruits = List(Str)
328-
def _fruits_changed(self, o, n):
329-
print("Fruits changed", o, n)
315+
import datetime
330316

331-
def _fruits_items_changed(self, list_event):
332-
print(list_event.index)
333-
print(list_event.removed)
334-
print(list_event.added)
317+
from traits.api import HasStrictTraits, Date, Range
335318

336-
```
319+
class Thing(HasStrictTraits):
320+
date = Date()
321+
age = Int(12)
337322

338-
```python
339-
b = Bowl()
340-
b.fruits = ['apple']
341-
b.fruits.append('mango')
323+
def _date_default(self):slideshow={"slide_type": "slide"}
324+
print('default')
325+
return datetime.datetime.today()
342326
```
343327

344-
```python
345-
def handler(event):
346-
print("h:", event)
347328

348-
b.observe(handler, 'fruits.items')
349-
b.fruits.append('peach')
329+
```python
330+
t = Thing()
350331
```
351332

352333
```python
353-
# Remove the handler
354-
b.observe(handler, 'fruits.items', remove=True)
334+
type(c.age)
355335
```
356336

357337

358-
<!-- #region slideshow={"slide_type": "slide"} -->
359-
## Other events
360-
361-
- `TraitChangeEvent`
362-
- `ListChangeEvent`
363-
- `DictChangeEvent`
364-
- `SetChangeEvent`
365-
366-
<!-- #endregion -->
367-
368-
369338

370339
<!-- #region slideshow={"slide_type": "slide"} -->
371340
## Exercise time!

slides/03_traitsui.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jupyter:
6161
<!-- #region slideshow={"slide_type": "slide"} -->
6262
## MVC with traitsui
6363

64-
- Model: `HasTraits` object
64+
- Model: `HasStrictTraits` object
6565
- View: `traitsui`, `View` class
6666
- Controller: `traitsui` `Handler` class
6767

@@ -82,12 +82,12 @@ jupyter:
8282
<!-- #endregion -->
8383

8484
```python
85-
from traits.api import HasTraits, Int, Str, Enum
85+
from traits.api import HasStrictTraits, Int, Str, Enum
8686

87-
class Person(HasTraits):
87+
class Person(HasStrictTraits):
8888
name = Str()
8989
age = Int()
90-
gender = Enum('female', 'male', 'other')
90+
handedness = Enum('left', 'right')
9191

9292
```
9393

@@ -111,7 +111,7 @@ from traitsui.api import Item, View
111111
view1 = View(
112112
Item(name='name', style='readonly'),
113113
Item(name='age'),
114-
Item(name='gender', visible_when='age > 10'),
114+
Item(name=handedness', visible_when='age > 10'),
115115
)
116116
```
117117

@@ -155,7 +155,7 @@ p.edit_traits(view=view1)
155155
```python
156156
from traitsui.api import Group
157157
158-
class Person(HasTraits):
158+
class Person(HasStrictTraits):
159159
name = Str()
160160
age = Int()
161161
gender = Enum('female', 'male', 'other')
@@ -188,7 +188,7 @@ p.edit_traits()
188188
- `icon`/`image`
189189
- `resizable`
190190
- `scrollable`
191-
- `title`
191+
- `title`: name of the window
192192
- `buttons`
193193
- `key_bindings`
194194
- See docs for more: https://docs.enthought.com/traitsui/traitsui_user_manual/
@@ -202,7 +202,7 @@ p.edit_traits()
202202
```python
203203
from traitsui.api import CancelButton, OKButton
204204
205-
class Person(HasTraits):
205+
class Person(HasStrictTraits):
206206
name = Str()
207207
age = Int()
208208
gender = Enum('female', 'male', 'other')

0 commit comments

Comments
 (0)