Show More
@@ -1,18 +1,31 b'' | |||
|
1 | 1 | |
|
2 | 2 | from copy import copy |
|
3 | from glob import glob | |
|
3 | 4 | import uuid |
|
4 | 5 | import sys |
|
6 | import os | |
|
5 | 7 | |
|
8 | import IPython | |
|
6 | 9 | from IPython.kernel.comm import Comm |
|
7 | 10 | from IPython.config import LoggingConfigurable |
|
8 | 11 | from IPython.utils.traitlets import Unicode, Dict |
|
9 | 12 | from IPython.display import Javascript, display |
|
13 | from IPython.utils.py3compat import string_types | |
|
14 | from IPython.utils.javascript import display_all_js | |
|
15 | ||
|
16 | def init_widget_js(cls): | |
|
17 | path = os.path.split(os.path.abspath( __file__ ))[0] | |
|
18 | display_all_js(path) | |
|
19 | for root, dirs, files in os.walk(path): | |
|
20 | for sub_directory in dirs: | |
|
21 | display_all_js(os.path.join(path, sub_directory)) | |
|
22 | ||
|
23 | ||
|
24 | class Widget(LoggingConfigurable): | |
|
10 | 25 | |
|
11 | class Widget(Comm): | |
|
12 | ||
|
13 | 26 | ### Public declarations |
|
14 | 27 | target_name = Unicode('widget') |
|
15 | view_name = Unicode() | |
|
28 | default_view_name = Unicode() | |
|
16 | 29 | |
|
17 | 30 | |
|
18 | 31 | ### Private/protected declarations |
@@ -31,20 +44,11 b' class Widget(Comm):' | |||
|
31 | 44 | if parent is not None: |
|
32 | 45 | parent._children.append(self) |
|
33 | 46 | self._parent = parent |
|
34 | ||
|
35 | # Send frontend type code. | |
|
36 | display(Javascript(data=self._get_backbone_js())) | |
|
37 | ||
|
38 | # Create a comm. | |
|
39 | self.comm = Comm(target_name=self.target_name) | |
|
40 | self.comm.on_msg(self._handle_msg) | |
|
47 | self.comm = None | |
|
41 | 48 | |
|
42 | 49 | # Register after init to allow default values to be specified |
|
43 | 50 | self.on_trait_change(self._handle_property_changed, self.keys) |
|
44 | 51 | |
|
45 | # Set initial properties on client model. | |
|
46 | self.send_state() | |
|
47 | ||
|
48 | 52 | |
|
49 | 53 | def __del__(self): |
|
50 | 54 | self.close() |
@@ -115,14 +119,23 b' class Widget(Comm):' | |||
|
115 | 119 | # TODO: Validate properties. |
|
116 | 120 | # Send new state to frontend |
|
117 | 121 | self.send_state() |
|
122 | ||
|
123 | ||
|
124 | def _handle_close(self): | |
|
125 | self.comm = None | |
|
118 | 126 | |
|
119 | 127 | |
|
120 | 128 | ### Public methods |
|
121 | def _repr_widget_(self): | |
|
122 | view_name = self.view_name | |
|
129 | def _repr_widget_(self, view_name=None): | |
|
123 | 130 | if not view_name: |
|
124 |
view_name = self. |
|
|
125 |
|
|
|
131 | view_name = self.default_view_name | |
|
132 | ||
|
133 | # Create a comm. | |
|
134 | if self.comm is None: | |
|
135 | self.comm = Comm(target_name=self.target_name) | |
|
136 | self.comm.on_msg(self._handle_msg) | |
|
137 | self.comm.on_close(self._handle_close) | |
|
138 | ||
|
126 | 139 | # Make sure model is syncronized |
|
127 | 140 | self.send_state() |
|
128 | 141 | |
@@ -137,9 +150,9 b' class Widget(Comm):' | |||
|
137 | 150 | # Now show children if any. |
|
138 | 151 | for child in self.children: |
|
139 | 152 | child._repr_widget_() |
|
140 |
return |
|
|
141 | ||
|
142 | ||
|
153 | return None | |
|
154 | ||
|
155 | ||
|
143 | 156 | def send_state(self): |
|
144 | 157 | state = {} |
|
145 | 158 | for key in self.keys: |
@@ -148,8 +161,5 b' class Widget(Comm):' | |||
|
148 | 161 | except Exception as e: |
|
149 | 162 | pass # Eat errors, nom nom nom |
|
150 | 163 | self.comm.send({"method": "update", |
|
151 | "state": state}) | |
|
152 |
|
|
|
153 | ### Private methods | |
|
154 | def _get_backbone_js(self): | |
|
155 | return 'alert("woohoo!");' | |
|
164 | "state": state}) | |
|
165 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now