| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
This program demonstrates defining and visualizing dynamic ranges. |
| 13 |
|
| 14 |
A dynamic range is a range whose low or high limit can be modified dynamically |
| 15 |
at run time. |
| 16 |
|
| 17 |
You can define a dynamic range using the standard Range trait and specifying |
| 18 |
the name of other traits as either the low or high limit value (or both). In |
| 19 |
fact, it is even possible to specify the default value of the range trait as |
| 20 |
another trait if desired. The traits used as low, high or default values need |
| 21 |
not be defined on the same object, but can be defined on any object reachable |
| 22 |
from the object (i.e. it allows use of extended trait names). |
| 23 |
|
| 24 |
In this completely artificial example, we present an example of the first |
| 25 |
hotel at the North Pole. The hotel guarantees that each room will be heated |
| 26 |
to a certain minimum temperature. However, that minimum value is determined |
| 27 |
both by the time of year and the current cost of heating fuel, so it can vary; |
| 28 |
but each room is guaranteed the same minimum temperature. |
| 29 |
|
| 30 |
Each guest of the hotel can choose among several different plans that allow |
| 31 |
them to control the maximum room temperature. Higher maximum room temperatures |
| 32 |
correspond to higher plan costs. Thus each guest must decide which plan (and |
| 33 |
highest maximum room temperature) to pay for. |
| 34 |
|
| 35 |
And finally, each guest is free to set the current room temperature anywhere |
| 36 |
between the hotel minimum value and the guest's maximum plan value. |
| 37 |
|
| 38 |
The demo is organized as a series of tabs corresponding to each guest of the |
| 39 |
hotel, with access to the plan they have chosen and the current room |
| 40 |
temperature setting. In addition, there is a master set of hotel information |
| 41 |
displayed at the top of the view which allows you to change the season of the |
| 42 |
year and the current fuel cost. There is also a button to allow more guests to |
| 43 |
be added to the hotel. |
| 44 |
|
| 45 |
Notes: |
| 46 |
|
| 47 |
- The dynamic range trait is the 'temperature' trait in the Guest class. It |
| 48 |
depends upon traits defined both in the Guest instance as well as in the |
| 49 |
containing Hotel object. |
| 50 |
|
| 51 |
- As with most traits code and examples, observe how much of the code is |
| 52 |
'declarative' versus 'imperative'. Note also the use of properties and |
| 53 |
'observe' metadata, as well as 'cached_property' and 'on_trait_change' |
| 54 |
method decorators. |
| 55 |
|
| 56 |
- Try dragging the guest tabs around so that you can see multiple guests |
| 57 |
simultaneously, and then watch the behavior of the guest's 'temperature' |
| 58 |
slider as you adjust the hotel 'season', 'fuel cost' and each guest's |
| 59 |
'plan'. |
| 60 |
""" |
| 61 |
|
| 62 |
# -- Imports -------------------------------------------------------------- |
| 63 |
|
| 64 |
import logging |
| 65 |
import sys |
| 66 |
|
| 67 |
from random import choice |
| 68 |
|
| 69 |
from traits.api import ( |
| 70 |
HasPrivateTraits, |
| 71 |
Str, |
| 72 |
Enum, |
| 73 |
Range, |
| 74 |
List, |
| 75 |
Button, |
| 76 |
Instance, |
| 77 |
Property, |
| 78 |
cached_property, |
| 79 |
observe, |
| 80 |
) |
| 81 |
|
| 82 |
from traitsui.api import View, VGroup, HGroup, Item, ListEditor, spring |
| 83 |
|
| 84 |
logging.basicConfig(stream=sys.stderr) |
| 85 |
|
| 86 |
# -- The Hotel class ------------------------------------------------------ |
| 87 |
|
| 88 |
|
| 89 |
class Hotel(HasPrivateTraits): |
| 90 |
|
| 91 |
# The season of the year: |
| 92 |
season = Enum('Winter', 'Spring', 'Summer', 'Fall') |
| 93 |
|
| 94 |
# The current cost of heating fuel (in dollars/gallon): |
| 95 |
fuel_cost = Range(2.00, 10.00, 4.00) |
| 96 |
|
| 97 |
# The current minimum temparature allowed by the hotel: |
| 98 |
min_temperature = Property(observe='season, fuel_cost') |
| 99 |
|
| 100 |
# The guests currently staying at the hotel: |
| 101 |
guests = List # ( Instance( 'Guest' ) ) |
| 102 |
|
| 103 |
# Add a new guest to the hotel: |
| 104 |
add_guest = Button('Add Guest') |
| 105 |
|
| 106 |
# The view of the hotel: |
| 107 |
view = View( |
| 108 |
VGroup( |
| 109 |
HGroup( |
| 110 |
Item('season'), |
| 111 |
'20', |
| 112 |
Item('fuel_cost', width=300), |
| 113 |
spring, |
| 114 |
Item('add_guest', show_label=False), |
| 115 |
show_border=True, |
| 116 |
label='Hotel Information', |
| 117 |
), |
| 118 |
VGroup( |
| 119 |
Item( |
| 120 |
'guests', |
| 121 |
style='custom', |
| 122 |
editor=ListEditor( |
| 123 |
use_notebook=True, |
| 124 |
deletable=True, |
| 125 |
dock_style='tab', |
| 126 |
page_name='.name', |
| 127 |
), |
| 128 |
), |
| 129 |
show_labels=False, |
| 130 |
show_border=True, |
| 131 |
label='Guests', |
| 132 |
), |
| 133 |
), |
| 134 |
title='The Belmont Hotel Dashboard', |
| 135 |
width=0.6, |
| 136 |
height=0.2, |
| 137 |
resizable=True, |
| 138 |
) |
| 139 |
|
| 140 |
# Property implementations: |
| 141 |
@cached_property |
| 142 |
def _get_min_temperature(self): |
| 143 |
return {'Winter': 32, 'Spring': 40, 'Summer': 45, 'Fall': 40}[ |
| 144 |
self.season |
| 145 |
] + min(int(60.00 / self.fuel_cost), 15) |
| 146 |
|
| 147 |
# Event handlers: |
| 148 |
@observe('guests.items') |
| 149 |
def _guests_modified(self, event): |
| 150 |
# The entire guests list changed |
| 151 |
if isinstance(event.object, Hotel): |
| 152 |
for guest in event.new: |
| 153 |
guest.hotel = self |
| 154 |
# the contents of the guests list changed |
| 155 |
else: |
| 156 |
for guest in event.added: |
| 157 |
guest.hotel = self |
| 158 |
|
| 159 |
def _add_guest_changed(self): |
| 160 |
self.guests.append(Guest(hotel=self)) |
| 161 |
|
| 162 |
|
| 163 |
# -- The Guest class ------------------------------------------------------ |
| 164 |
|
| 165 |
|
| 166 |
class Guest(HasPrivateTraits): |
| 167 |
|
| 168 |
# The name of the guest: |
| 169 |
name = Str() |
| 170 |
|
| 171 |
# The hotel the guest is staying at: |
| 172 |
hotel = Instance(Hotel) |
| 173 |
|
| 174 |
# The room plan the guest has chosen: |
| 175 |
plan = Enum('Flop house', 'Cheap', 'Cozy', 'Deluxe') |
| 176 |
|
| 177 |
# The maximum temperature allowed by the guest's plan: |
| 178 |
max_temperature = Property(observe='plan') |
| 179 |
|
| 180 |
# The current room temperature as set by the guest: |
| 181 |
temperature = Range('hotel.min_temperature', 'max_temperature') |
| 182 |
|
| 183 |
# The view of the guest: |
| 184 |
view = View(Item('plan'), Item('temperature')) |
| 185 |
|
| 186 |
# Property implementations: |
| 187 |
@cached_property |
| 188 |
def _get_max_temperature(self): |
| 189 |
return {'Flop house': 62, 'Cheap': 66, 'Cozy': 75, 'Deluxe': 85}[ |
| 190 |
self.plan |
| 191 |
] |
| 192 |
|
| 193 |
# Default values: |
| 194 |
def _name_default(self): |
| 195 |
return choice( |
| 196 |
[ |
| 197 |
'Leah', |
| 198 |
'Vibha', |
| 199 |
'Janet', |
| 200 |
'Jody', |
| 201 |
'Dave', |
| 202 |
'Evan', |
| 203 |
'Ilan', |
| 204 |
'Gael', |
| 205 |
'Peter', |
| 206 |
'Robert', |
| 207 |
'Judah', |
| 208 |
'Eric', |
| 209 |
'Travis', |
| 210 |
'Mike', |
| 211 |
'Bryce', |
| 212 |
'Chris', |
| 213 |
] |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
# -- Create the demo ------------------------------------------------------ |
| 218 |
|
| 219 |
# Create the demo object: |
| 220 |
demo = Hotel(guests=[Guest() for i in range(5)]) |
| 221 |
|
| 222 |
# Run the demo (if invoked from the command line): |
| 223 |
if __name__ == '__main__': |
| 224 |
logging.info('Start!') |
| 225 |
demo.configure_traits() |