##// END OF EJS Templates
Basic display logic...
Jonathan Frederic -
Show More
@@ -1,148 +1,155 b''
1 from IPython.kernel.comm import Comm
2 from IPython.config import LoggingConfigurable
3 from IPython.utils.traitlets import Unicode, Float, Bool, Dict
4 from IPython.display import clear_output
5 1
6 2 from copy import copy
7 3 import uuid
8 4 import sys
9 5
6 from IPython.kernel.comm import Comm
7 from IPython.config import LoggingConfigurable
8 from IPython.utils.traitlets import Unicode, Dict
9 from IPython.display import Javascript, display
10 10
11 11 class Widget(Comm):
12 12
13 13 ### Public declarations
14 14 target_name = Unicode('widget')
15 default_view_name = Unicode()
15 view_name = Unicode()
16 16
17 17
18 18 ### Private/protected declarations
19 19 _keys = []
20 20 _property_lock = False
21 21 _parent = None
22 22 _children = []
23 23 _css = Dict()
24 24
25 25
26 26 ### Public constructor
27 27 def __init__(self, parent=None, **kwargs):
28 28 super(Widget, self).__init__(**kwargs)
29 29
30 30 self._children = []
31 31 if parent is not None:
32 32 parent._children.append(self)
33 33 self._parent = parent
34
34
35 # Send frontend type code.
36 display(Javascript(data=self._get_backbone_js()))
37
38 # Create a comm.
35 39 self.comm = Comm(target_name=self.target_name)
36 40 self.comm.on_msg(self._handle_msg)
37 41
38 42 # Register after init to allow default values to be specified
39 43 self.on_trait_change(self._handle_property_changed, self.keys)
40 44
41 45 # Set initial properties on client model.
42 46 self.send_state()
43 47
44 48
45 49 def __del__(self):
46 50 self.close()
47 51
48 52 def close(self):
49 53 self.comm.close()
50 54 del self.comm
51 55
52 56
53 57 ### Properties
54 58 def _get_parent(self):
55 59 return self._parent
56 60 parent = property(_get_parent)
57 61
58 62
59 63 def _get_children(self):
60 64 return copy(self._children)
61 65 children = property(_get_children)
62 66
63 67
64 68 def _get_keys(self):
65 69 keys = ['_css']
66 70 keys.extend(self._keys)
67 71 return keys
68 72 keys = property(_get_keys)
69 73
70 74 def _get_css(self, key, selector=""):
71 75 if selector in self._css and key in self._css[selector]:
72 76 return self._css[selector][key]
73 77 else:
74 78 return None
75 79 def _set_css(self, value, key, selector=""):
76 80 if selector not in self._css:
77 81 self._css[selector] = {}
78 82
79 83 # Only update the property if it has changed.
80 84 if not (key in self._css[selector] and value in self._css[selector][key]):
81 85 self._css[selector][key] = value
82 86 self.send_state() # Send new state to client.
83 87
84 88 css = property(_get_css, _set_css)
85 89
86 90
87 91 ### Event handlers
88 92 def _handle_msg(self, msg):
89 93
90 94 # Handle backbone sync methods
91 95 sync_method = msg['content']['data']['sync_method']
92 96 sync_data = msg['content']['data']['sync_data']
93 97 if sync_method.lower() in ['create', 'update']:
94 98 self._handle_recieve_state(sync_data)
95 99
96 100
97 101 def _handle_recieve_state(self, sync_data):
98 102 self._property_lock = True
99 103 try:
100 104
101 105 # Use _keys instead of keys - Don't get retrieve the css from the client side.
102 106 for name in self._keys:
103 107 if name in sync_data:
104 108 setattr(self, name, sync_data[name])
105 109 finally:
106 110 self._property_lock = False
107 111
108 112
109 113 def _handle_property_changed(self, name, old, new):
110 114 if not self._property_lock:
111 115 # TODO: Validate properties.
112 116 # Send new state to frontend
113 117 self.send_state()
114 118
115 119
116 120 ### Public methods
117 def show(self, view_name=None):
118 if not view_name:
119 view_name = self.default_view_name
121 def _repr_widget_(self):
122 view_name = self.view_name
120 123 if not view_name:
121 124 view_name = self.target_name
122 125
123 126 # Make sure model is syncronized
124 127 self.send_state()
125 128
126 129 # Show view.
127 130 if self.parent is None:
128 131 self.comm.send({"method": "show", "view_name": view_name})
129 132 else:
130 133 self.comm.send({"method": "show",
131 134 "view_name": view_name,
132 135 "parent": self.parent.comm.comm_id})
133 136
134 137 # Now show children if any.
135 138 for child in self.children:
136 child.show()
139 child._repr_widget_()
140 return self._get_backbone_js
137 141
138 142
139 143 def send_state(self):
140 144 state = {}
141 145 for key in self.keys:
142 146 try:
143 147 state[key] = getattr(self, key)
144 148 except Exception as e:
145 149 pass # Eat errors, nom nom nom
146 150 self.comm.send({"method": "update",
147 151 "state": state})
148 No newline at end of file
152
153 ### Private methods
154 def _get_backbone_js(self):
155 return 'alert("woohoo!");'
General Comments 0
You need to be logged in to leave comments. Login now