##// END OF EJS Templates
Renamed widget python classes to avoid name stomping
Jonathan Frederic -
Show More
@@ -1,10 +1,10 b''
1 from base import Widget, init_widget_js
1 from widget import Widget, init_widget_js
2
2
3 from bool import BoolWidget
3 from widget_bool import BoolWidget
4 from container import ContainerWidget
4 from widget_container import ContainerWidget
5 from float import FloatWidget
5 from widget_float import FloatWidget
6 from float_range import FloatRangeWidget
6 from widget_float_range import FloatRangeWidget
7 from int import IntWidget
7 from widget_int import IntWidget
8 from int_range import IntRangeWidget
8 from widget_int_range import IntRangeWidget
9 from selection import SelectionWidget
9 from widget_selection import SelectionWidget
10 from string import StringWidget
10 from widget_string import StringWidget
@@ -1,164 +1,165 b''
1 from copy import copy
1 from copy import copy
2 from glob import glob
2 from glob import glob
3 import uuid
3 import uuid
4 import sys
4 import sys
5 import os
5 import os
6
6
7 import IPython
7 import IPython
8 from IPython.kernel.comm import Comm
8 from IPython.kernel.comm import Comm
9 from IPython.config import LoggingConfigurable
9 from IPython.config import LoggingConfigurable
10 from IPython.utils.traitlets import Unicode, Dict, List
10 from IPython.utils.traitlets import Unicode, Dict, List
11 from IPython.display import Javascript, display
11 from IPython.display import Javascript, display
12 from IPython.utils.py3compat import string_types
12 from IPython.utils.py3compat import string_types
13
13
14 def init_widget_js():
14 def init_widget_js():
15 path = os.path.split(os.path.abspath( __file__ ))[0]
15 path = os.path.split(os.path.abspath( __file__ ))[0]
16 for filepath in glob(os.path.join(path, "*.py")):
16 for filepath in glob(os.path.join(path, "*.py")):
17 filename = os.path.split(filepath)[1]
17 filename = os.path.split(filepath)[1]
18 name = filename.rsplit('.', 1)[0]
18 name = filename.rsplit('.', 1)[0]
19 if not (name == 'base' or name == '__init__'):
19 if not (name == 'widget' or name == '__init__') and name.startswith('widget_'):
20 js_path = 'static/notebook/js/widgets/%s.js' % name
20 # Remove 'widget_' from the start of the name before compiling the path.
21 js_path = 'static/notebook/js/widgets/%s.js' % name[7:]
21 display(Javascript(data='$.getScript("%s");' % js_path))
22 display(Javascript(data='$.getScript("%s");' % js_path))
22
23
23
24
24 class Widget(LoggingConfigurable):
25 class Widget(LoggingConfigurable):
25
26
26 ### Public declarations
27 ### Public declarations
27 target_name = Unicode('widget')
28 target_name = Unicode('widget')
28 default_view_name = Unicode()
29 default_view_name = Unicode()
29
30
30
31
31 ### Private/protected declarations
32 ### Private/protected declarations
32 _keys = []
33 _keys = []
33 _property_lock = False
34 _property_lock = False
34 _parent = None
35 _parent = None
35 _children = []
36 _children = []
36 _css = Dict()
37 _css = Dict()
37
38
38
39
39 ### Public constructor
40 ### Public constructor
40 def __init__(self, parent=None, **kwargs):
41 def __init__(self, parent=None, **kwargs):
41 super(Widget, self).__init__(**kwargs)
42 super(Widget, self).__init__(**kwargs)
42
43
43 self._children = []
44 self._children = []
44 if parent is not None:
45 if parent is not None:
45 parent._children.append(self)
46 parent._children.append(self)
46 self._parent = parent
47 self._parent = parent
47 self.comm = None
48 self.comm = None
48
49
49 # Register after init to allow default values to be specified
50 # Register after init to allow default values to be specified
50 self.on_trait_change(self._handle_property_changed, self.keys)
51 self.on_trait_change(self._handle_property_changed, self.keys)
51
52
52
53
53 def __del__(self):
54 def __del__(self):
54 self.close()
55 self.close()
55
56
56 def close(self):
57 def close(self):
57 self.comm.close()
58 self.comm.close()
58 del self.comm
59 del self.comm
59
60
60
61
61 ### Properties
62 ### Properties
62 def _get_parent(self):
63 def _get_parent(self):
63 return self._parent
64 return self._parent
64 parent = property(_get_parent)
65 parent = property(_get_parent)
65
66
66
67
67 def _get_children(self):
68 def _get_children(self):
68 return copy(self._children)
69 return copy(self._children)
69 children = property(_get_children)
70 children = property(_get_children)
70
71
71
72
72 def _get_keys(self):
73 def _get_keys(self):
73 keys = ['_css']
74 keys = ['_css']
74 keys.extend(self._keys)
75 keys.extend(self._keys)
75 return keys
76 return keys
76 keys = property(_get_keys)
77 keys = property(_get_keys)
77
78
78 def _get_css(self, key, selector=""):
79 def _get_css(self, key, selector=""):
79 if selector in self._css and key in self._css[selector]:
80 if selector in self._css and key in self._css[selector]:
80 return self._css[selector][key]
81 return self._css[selector][key]
81 else:
82 else:
82 return None
83 return None
83 def _set_css(self, value, key, selector=""):
84 def _set_css(self, value, key, selector=""):
84 if selector not in self._css:
85 if selector not in self._css:
85 self._css[selector] = {}
86 self._css[selector] = {}
86
87
87 # Only update the property if it has changed.
88 # Only update the property if it has changed.
88 if not (key in self._css[selector] and value in self._css[selector][key]):
89 if not (key in self._css[selector] and value in self._css[selector][key]):
89 self._css[selector][key] = value
90 self._css[selector][key] = value
90 self.send_state() # Send new state to client.
91 self.send_state() # Send new state to client.
91
92
92 css = property(_get_css, _set_css)
93 css = property(_get_css, _set_css)
93
94
94
95
95 ### Event handlers
96 ### Event handlers
96 def _handle_msg(self, msg):
97 def _handle_msg(self, msg):
97
98
98 # Handle backbone sync methods
99 # Handle backbone sync methods
99 sync_method = msg['content']['data']['sync_method']
100 sync_method = msg['content']['data']['sync_method']
100 sync_data = msg['content']['data']['sync_data']
101 sync_data = msg['content']['data']['sync_data']
101 if sync_method.lower() in ['create', 'update']:
102 if sync_method.lower() in ['create', 'update']:
102 self._handle_recieve_state(sync_data)
103 self._handle_recieve_state(sync_data)
103
104
104
105
105 def _handle_recieve_state(self, sync_data):
106 def _handle_recieve_state(self, sync_data):
106 self._property_lock = True
107 self._property_lock = True
107 try:
108 try:
108
109
109 # Use _keys instead of keys - Don't get retrieve the css from the client side.
110 # Use _keys instead of keys - Don't get retrieve the css from the client side.
110 for name in self._keys:
111 for name in self._keys:
111 if name in sync_data:
112 if name in sync_data:
112 setattr(self, name, sync_data[name])
113 setattr(self, name, sync_data[name])
113 finally:
114 finally:
114 self._property_lock = False
115 self._property_lock = False
115
116
116
117
117 def _handle_property_changed(self, name, old, new):
118 def _handle_property_changed(self, name, old, new):
118 if not self._property_lock and self.comm is not None:
119 if not self._property_lock and self.comm is not None:
119 # TODO: Validate properties.
120 # TODO: Validate properties.
120 # Send new state to frontend
121 # Send new state to frontend
121 self.send_state()
122 self.send_state()
122
123
123
124
124 def _handle_close(self):
125 def _handle_close(self):
125 self.comm = None
126 self.comm = None
126
127
127
128
128 ### Public methods
129 ### Public methods
129 def _repr_widget_(self, view_name=None):
130 def _repr_widget_(self, view_name=None):
130 if not view_name:
131 if not view_name:
131 view_name = self.default_view_name
132 view_name = self.default_view_name
132
133
133 # Create a comm.
134 # Create a comm.
134 if self.comm is None:
135 if self.comm is None:
135 self.comm = Comm(target_name=self.target_name)
136 self.comm = Comm(target_name=self.target_name)
136 self.comm.on_msg(self._handle_msg)
137 self.comm.on_msg(self._handle_msg)
137 self.comm.on_close(self._handle_close)
138 self.comm.on_close(self._handle_close)
138
139
139 # Make sure model is syncronized
140 # Make sure model is syncronized
140 self.send_state()
141 self.send_state()
141
142
142 # Show view.
143 # Show view.
143 if self.parent is None:
144 if self.parent is None:
144 self.comm.send({"method": "show", "view_name": view_name})
145 self.comm.send({"method": "show", "view_name": view_name})
145 else:
146 else:
146 self.comm.send({"method": "show",
147 self.comm.send({"method": "show",
147 "view_name": view_name,
148 "view_name": view_name,
148 "parent": self.parent.comm.comm_id})
149 "parent": self.parent.comm.comm_id})
149
150
150 # Now show children if any.
151 # Now show children if any.
151 for child in self.children:
152 for child in self.children:
152 child._repr_widget_()
153 child._repr_widget_()
153 return None
154 return None
154
155
155
156
156 def send_state(self):
157 def send_state(self):
157 state = {}
158 state = {}
158 for key in self.keys:
159 for key in self.keys:
159 try:
160 try:
160 state[key] = getattr(self, key)
161 state[key] = getattr(self, key)
161 except Exception as e:
162 except Exception as e:
162 pass # Eat errors, nom nom nom
163 pass # Eat errors, nom nom nom
163 self.comm.send({"method": "update",
164 self.comm.send({"method": "update",
164 "state": state})
165 "state": state})
1 NO CONTENT: file renamed from IPython/html/widgets/bool.py to IPython/html/widgets/widget_bool.py
NO CONTENT: file renamed from IPython/html/widgets/bool.py to IPython/html/widgets/widget_bool.py
1 NO CONTENT: file renamed from IPython/html/widgets/container.py to IPython/html/widgets/widget_container.py
NO CONTENT: file renamed from IPython/html/widgets/container.py to IPython/html/widgets/widget_container.py
1 NO CONTENT: file renamed from IPython/html/widgets/float.py to IPython/html/widgets/widget_float.py
NO CONTENT: file renamed from IPython/html/widgets/float.py to IPython/html/widgets/widget_float.py
1 NO CONTENT: file renamed from IPython/html/widgets/float_range.py to IPython/html/widgets/widget_float_range.py
NO CONTENT: file renamed from IPython/html/widgets/float_range.py to IPython/html/widgets/widget_float_range.py
1 NO CONTENT: file renamed from IPython/html/widgets/int.py to IPython/html/widgets/widget_int.py
NO CONTENT: file renamed from IPython/html/widgets/int.py to IPython/html/widgets/widget_int.py
1 NO CONTENT: file renamed from IPython/html/widgets/int_range.py to IPython/html/widgets/widget_int_range.py
NO CONTENT: file renamed from IPython/html/widgets/int_range.py to IPython/html/widgets/widget_int_range.py
1 NO CONTENT: file renamed from IPython/html/widgets/selection.py to IPython/html/widgets/widget_selection.py
NO CONTENT: file renamed from IPython/html/widgets/selection.py to IPython/html/widgets/widget_selection.py
1 NO CONTENT: file renamed from IPython/html/widgets/string.py to IPython/html/widgets/widget_string.py
NO CONTENT: file renamed from IPython/html/widgets/string.py to IPython/html/widgets/widget_string.py
General Comments 0
You need to be logged in to leave comments. Login now