Show More
@@ -1,91 +1,91 b'' | |||||
1 | """ButtonWidget class. |
|
1 | """ButtonWidget class. | |
2 |
|
2 | |||
3 | Represents a button in the frontend using a widget. Allows user to listen for |
|
3 | Represents a button in the frontend using a widget. Allows user to listen for | |
4 | click events on the button and trigger backend code when the clicks are fired. |
|
4 | click events on the button and trigger backend code when the clicks are fired. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | import inspect |
|
17 | import inspect | |
18 | import types |
|
18 | import types | |
19 |
|
19 | |||
20 | from .widget import DOMWidget |
|
20 | from .widget import DOMWidget | |
21 |
from IPython.utils.traitlets import Unicode, Bool |
|
21 | from IPython.utils.traitlets import Unicode, Bool | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Classes |
|
24 | # Classes | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | class ButtonWidget(DOMWidget): |
|
26 | class ButtonWidget(DOMWidget): | |
27 | view_name = Unicode('ButtonView', sync=True) |
|
27 | view_name = Unicode('ButtonView', sync=True) | |
28 |
|
28 | |||
29 | # Keys |
|
29 | # Keys | |
30 | description = Unicode('', help="Description of the button (label).", sync=True) |
|
30 | description = Unicode('', help="Description of the button (label).", sync=True) | |
31 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) |
|
31 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def __init__(self, **kwargs): |
|
34 | def __init__(self, **kwargs): | |
35 | super(ButtonWidget, self).__init__(**kwargs) |
|
35 | super(ButtonWidget, self).__init__(**kwargs) | |
36 |
|
36 | |||
37 | self._click_handlers = [] |
|
37 | self._click_handlers = [] | |
38 | self.on_msg(self._handle_button_msg) |
|
38 | self.on_msg(self._handle_button_msg) | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | def on_click(self, callback, remove=False): |
|
41 | def on_click(self, callback, remove=False): | |
42 | """Register a callback to execute when the button is clicked. The |
|
42 | """Register a callback to execute when the button is clicked. The | |
43 | callback can either accept no parameters or one sender parameter: |
|
43 | callback can either accept no parameters or one sender parameter: | |
44 | - callback() |
|
44 | - callback() | |
45 | - callback(sender) |
|
45 | - callback(sender) | |
46 | If the callback has a sender parameter, the ButtonWidget instance that |
|
46 | If the callback has a sender parameter, the ButtonWidget instance that | |
47 | called the callback will be passed into the method as the sender. |
|
47 | called the callback will be passed into the method as the sender. | |
48 |
|
48 | |||
49 | Parameters |
|
49 | Parameters | |
50 | ---------- |
|
50 | ---------- | |
51 | remove : bool (optional) |
|
51 | remove : bool (optional) | |
52 | Set to true to remove the callback from the list of callbacks.""" |
|
52 | Set to true to remove the callback from the list of callbacks.""" | |
53 | if remove: |
|
53 | if remove: | |
54 | self._click_handlers.remove(callback) |
|
54 | self._click_handlers.remove(callback) | |
55 | elif not callback in self._click_handlers: |
|
55 | elif not callback in self._click_handlers: | |
56 | self._click_handlers.append(callback) |
|
56 | self._click_handlers.append(callback) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | def _handle_button_msg(self, content): |
|
59 | def _handle_button_msg(self, content): | |
60 | """Handle a msg from the front-end |
|
60 | """Handle a msg from the front-end | |
61 |
|
61 | |||
62 | Parameters |
|
62 | Parameters | |
63 | ---------- |
|
63 | ---------- | |
64 | content: dict |
|
64 | content: dict | |
65 | Content of the msg.""" |
|
65 | Content of the msg.""" | |
66 | if 'event' in content and content['event'] == 'click': |
|
66 | if 'event' in content and content['event'] == 'click': | |
67 | self._handle_click() |
|
67 | self._handle_click() | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | def _handle_click(self): |
|
70 | def _handle_click(self): | |
71 | """Handles when the button has been clicked. Fires on_click |
|
71 | """Handles when the button has been clicked. Fires on_click | |
72 | callbacks when appropriate.""" |
|
72 | callbacks when appropriate.""" | |
73 |
|
73 | |||
74 | for handler in self._click_handlers: |
|
74 | for handler in self._click_handlers: | |
75 | if callable(handler): |
|
75 | if callable(handler): | |
76 | argspec = inspect.getargspec(handler) |
|
76 | argspec = inspect.getargspec(handler) | |
77 | nargs = len(argspec[0]) |
|
77 | nargs = len(argspec[0]) | |
78 |
|
78 | |||
79 | # Bound methods have an additional 'self' argument |
|
79 | # Bound methods have an additional 'self' argument | |
80 | if isinstance(handler, types.MethodType): |
|
80 | if isinstance(handler, types.MethodType): | |
81 | nargs -= 1 |
|
81 | nargs -= 1 | |
82 |
|
82 | |||
83 | # Call the callback |
|
83 | # Call the callback | |
84 | if nargs == 0: |
|
84 | if nargs == 0: | |
85 | handler() |
|
85 | handler() | |
86 | elif nargs == 1: |
|
86 | elif nargs == 1: | |
87 | handler(self) |
|
87 | handler(self) | |
88 | else: |
|
88 | else: | |
89 | raise TypeError('ButtonWidget click callback must ' \ |
|
89 | raise TypeError('ButtonWidget click callback must ' \ | |
90 | 'accept 0 or 1 arguments.') |
|
90 | 'accept 0 or 1 arguments.') | |
91 |
|
91 |
@@ -1,28 +1,28 b'' | |||||
1 | """FloatWidget class. |
|
1 | """FloatWidget class. | |
2 |
|
2 | |||
3 | Represents an unbounded float using a widget. |
|
3 | Represents an unbounded float using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, Float, Bool, List |
|
17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, List | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class FloatTextWidget(DOMWidget): |
|
22 | class FloatTextWidget(DOMWidget): | |
23 | view_name = Unicode('FloatTextView', sync=True) |
|
23 | view_name = Unicode('FloatTextView', sync=True) | |
24 |
|
24 | |||
25 | # Keys |
|
25 | # Keys | |
26 | value = Float(0.0, help="Float value", sync=True) |
|
26 | value = CFloat(0.0, help="Float value", sync=True) | |
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
28 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
28 | description = Unicode(help="Description of the value this widget represents", sync=True) |
@@ -1,38 +1,38 b'' | |||||
1 | """FloatRangeWidget class. |
|
1 | """FloatRangeWidget class. | |
2 |
|
2 | |||
3 | Represents a bounded float using a widget. |
|
3 | Represents a bounded float using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, Float, Bool, List |
|
17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, List | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class BoundedFloatTextWidget(DOMWidget): |
|
22 | class BoundedFloatTextWidget(DOMWidget): | |
23 | view_name = Unicode('FloatTextView', sync=True) |
|
23 | view_name = Unicode('FloatTextView', sync=True) | |
24 | value = Float(0.0, help="Float value", sync=True) |
|
24 | value = CFloat(0.0, help="Float value", sync=True) | |
25 | max = Float(100.0, help="Max value", sync=True) |
|
25 | max = CFloat(100.0, help="Max value", sync=True) | |
26 | min = Float(0.0, help="Min value", sync=True) |
|
26 | min = CFloat(0.0, help="Min value", sync=True) | |
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
28 | step = Float(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
28 | step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
29 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
29 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | class FloatSliderWidget(BoundedFloatTextWidget): |
|
32 | class FloatSliderWidget(BoundedFloatTextWidget): | |
33 | view_name = Unicode('FloatSliderView', sync=True) |
|
33 | view_name = Unicode('FloatSliderView', sync=True) | |
34 | orientation = Unicode(u'horizontal', help="Vertical or horizontal.", sync=True) |
|
34 | orientation = Unicode(u'horizontal', help="Vertical or horizontal.", sync=True) | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class FloatProgressWidget(BoundedFloatTextWidget): |
|
37 | class FloatProgressWidget(BoundedFloatTextWidget): | |
38 | view_name = Unicode('ProgressView', sync=True) |
|
38 | view_name = Unicode('ProgressView', sync=True) |
@@ -1,28 +1,28 b'' | |||||
1 | """IntWidget class. |
|
1 | """IntWidget class. | |
2 |
|
2 | |||
3 | Represents an unbounded int using a widget. |
|
3 | Represents an unbounded int using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, Int, Bool, List |
|
17 | from IPython.utils.traitlets import Unicode, CInt, Bool, List | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class IntTextWidget(DOMWidget): |
|
22 | class IntTextWidget(DOMWidget): | |
23 | view_name = Unicode('IntTextView', sync=True) |
|
23 | view_name = Unicode('IntTextView', sync=True) | |
24 |
|
24 | |||
25 | # Keys |
|
25 | # Keys | |
26 | value = Int(0, help="Int value", sync=True) |
|
26 | value = CInt(0, help="Int value", sync=True) | |
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
28 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
28 | description = Unicode(help="Description of the value this widget represents", sync=True) |
@@ -1,40 +1,40 b'' | |||||
1 | """IntRangeWidget class. |
|
1 | """IntRangeWidget class. | |
2 |
|
2 | |||
3 | Represents a bounded int using a widget. |
|
3 | Represents a bounded int using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, Int, Bool, List |
|
17 | from IPython.utils.traitlets import Unicode, CInt, Bool, List | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class BoundedIntTextWidget(DOMWidget): |
|
22 | class BoundedIntTextWidget(DOMWidget): | |
23 | view_name = Unicode('IntTextView', sync=True) |
|
23 | view_name = Unicode('IntTextView', sync=True) | |
24 |
|
24 | |||
25 | # Keys |
|
25 | # Keys | |
26 | value = Int(0, help="Int value", sync=True) |
|
26 | value = CInt(0, help="Int value", sync=True) | |
27 | max = Int(100, help="Max value", sync=True) |
|
27 | max = CInt(100, help="Max value", sync=True) | |
28 | min = Int(0, help="Min value", sync=True) |
|
28 | min = CInt(0, help="Min value", sync=True) | |
29 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
29 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
30 | step = Int(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
30 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
31 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
31 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class IntSliderWidget(BoundedIntTextWidget): |
|
34 | class IntSliderWidget(BoundedIntTextWidget): | |
35 | view_name = Unicode('IntSliderView', sync=True) |
|
35 | view_name = Unicode('IntSliderView', sync=True) | |
36 | orientation = Unicode(u'horizontal', help="Vertical or horizontal.", sync=True) |
|
36 | orientation = Unicode(u'horizontal', help="Vertical or horizontal.", sync=True) | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class IntProgressWidget(BoundedIntTextWidget): |
|
39 | class IntProgressWidget(BoundedIntTextWidget): | |
40 | view_name = Unicode('ProgressView', sync=True) |
|
40 | view_name = Unicode('ProgressView', sync=True) |
@@ -1,60 +1,60 b'' | |||||
1 | """SelectionContainerWidget class. |
|
1 | """SelectionContainerWidget class. | |
2 |
|
2 | |||
3 | Represents a multipage container that can be used to group other widgets into |
|
3 | Represents a multipage container that can be used to group other widgets into | |
4 | pages. |
|
4 | pages. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | from .widget import DOMWidget |
|
17 | from .widget import DOMWidget | |
18 | from IPython.utils.traitlets import Unicode, Dict, Int, List, Instance |
|
18 | from IPython.utils.traitlets import Unicode, Dict, CInt, List, Instance | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Classes |
|
21 | # Classes | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | class AccordionWidget(DOMWidget): |
|
23 | class AccordionWidget(DOMWidget): | |
24 | view_name = Unicode('AccordionView', sync=True) |
|
24 | view_name = Unicode('AccordionView', sync=True) | |
25 |
|
25 | |||
26 | # Keys |
|
26 | # Keys | |
27 | _titles = Dict(help="Titles of the pages", sync=True) |
|
27 | _titles = Dict(help="Titles of the pages", sync=True) | |
28 | selected_index = Int(0, sync=True) |
|
28 | selected_index = CInt(0, sync=True) | |
29 |
|
29 | |||
30 | children = List(Instance(DOMWidget), sync=True) |
|
30 | children = List(Instance(DOMWidget), sync=True) | |
31 |
|
31 | |||
32 | # Public methods |
|
32 | # Public methods | |
33 | def set_title(self, index, title): |
|
33 | def set_title(self, index, title): | |
34 | """Sets the title of a container page |
|
34 | """Sets the title of a container page | |
35 |
|
35 | |||
36 | Parameters |
|
36 | Parameters | |
37 | ---------- |
|
37 | ---------- | |
38 | index : int |
|
38 | index : int | |
39 | Index of the container page |
|
39 | Index of the container page | |
40 | title : unicode |
|
40 | title : unicode | |
41 | New title""" |
|
41 | New title""" | |
42 | self._titles[index] = title |
|
42 | self._titles[index] = title | |
43 | self.send_state('_titles') |
|
43 | self.send_state('_titles') | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | def get_title(self, index): |
|
46 | def get_title(self, index): | |
47 | """Gets the title of a container pages |
|
47 | """Gets the title of a container pages | |
48 |
|
48 | |||
49 | Parameters |
|
49 | Parameters | |
50 | ---------- |
|
50 | ---------- | |
51 | index : int |
|
51 | index : int | |
52 | Index of the container page""" |
|
52 | Index of the container page""" | |
53 | if index in self._titles: |
|
53 | if index in self._titles: | |
54 | return self._titles[index] |
|
54 | return self._titles[index] | |
55 | else: |
|
55 | else: | |
56 | return None |
|
56 | return None | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class TabWidget(AccordionWidget): |
|
59 | class TabWidget(AccordionWidget): | |
60 | view_name = Unicode('TabView', sync=True) |
|
60 | view_name = Unicode('TabView', sync=True) |
@@ -1,94 +1,94 b'' | |||||
1 | """StringWidget class. |
|
1 | """StringWidget class. | |
2 |
|
2 | |||
3 | Represents a unicode string using a widget. |
|
3 | Represents a unicode string using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | import inspect |
|
16 | import inspect | |
17 | import types |
|
17 | import types | |
18 |
|
18 | |||
19 | from .widget import DOMWidget |
|
19 | from .widget import DOMWidget | |
20 |
from IPython.utils.traitlets import Unicode, Bool, List |
|
20 | from IPython.utils.traitlets import Unicode, Bool, List | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Classes |
|
23 | # Classes | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | class HTMLWidget(DOMWidget): |
|
25 | class HTMLWidget(DOMWidget): | |
26 | view_name = Unicode('HTMLView', sync=True) |
|
26 | view_name = Unicode('HTMLView', sync=True) | |
27 |
|
27 | |||
28 | # Keys |
|
28 | # Keys | |
29 | value = Unicode(help="String value", sync=True) |
|
29 | value = Unicode(help="String value", sync=True) | |
30 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
30 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
31 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
31 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class LatexWidget(HTMLWidget): |
|
34 | class LatexWidget(HTMLWidget): | |
35 | view_name = Unicode('LatexView', sync=True) |
|
35 | view_name = Unicode('LatexView', sync=True) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class TextAreaWidget(HTMLWidget): |
|
38 | class TextAreaWidget(HTMLWidget): | |
39 | view_name = Unicode('TextAreaView', sync=True) |
|
39 | view_name = Unicode('TextAreaView', sync=True) | |
40 |
|
40 | |||
41 | def scroll_to_bottom(self): |
|
41 | def scroll_to_bottom(self): | |
42 | self.send({"method": "scroll_to_bottom"}) |
|
42 | self.send({"method": "scroll_to_bottom"}) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | class TextBoxWidget(HTMLWidget): |
|
45 | class TextBoxWidget(HTMLWidget): | |
46 | view_name = Unicode('TextBoxView', sync=True) |
|
46 | view_name = Unicode('TextBoxView', sync=True) | |
47 |
|
47 | |||
48 | def __init__(self, **kwargs): |
|
48 | def __init__(self, **kwargs): | |
49 | super(TextBoxWidget, self).__init__(**kwargs) |
|
49 | super(TextBoxWidget, self).__init__(**kwargs) | |
50 | self._submission_callbacks = [] |
|
50 | self._submission_callbacks = [] | |
51 | self.on_msg(self._handle_string_msg) |
|
51 | self.on_msg(self._handle_string_msg) | |
52 |
|
52 | |||
53 | def _handle_string_msg(self, content): |
|
53 | def _handle_string_msg(self, content): | |
54 | """Handle a msg from the front-end |
|
54 | """Handle a msg from the front-end | |
55 |
|
55 | |||
56 | Parameters |
|
56 | Parameters | |
57 | ---------- |
|
57 | ---------- | |
58 | content: dict |
|
58 | content: dict | |
59 | Content of the msg.""" |
|
59 | Content of the msg.""" | |
60 | if 'event' in content and content['event'] == 'submit': |
|
60 | if 'event' in content and content['event'] == 'submit': | |
61 | for handler in self._submission_callbacks: |
|
61 | for handler in self._submission_callbacks: | |
62 | handler(self) |
|
62 | handler(self) | |
63 |
|
63 | |||
64 | def on_submit(self, callback, remove=False): |
|
64 | def on_submit(self, callback, remove=False): | |
65 | """Register a callback to handle text submission (triggered when the |
|
65 | """Register a callback to handle text submission (triggered when the | |
66 | user clicks enter). |
|
66 | user clicks enter). | |
67 |
|
67 | |||
68 | Parameters |
|
68 | Parameters | |
69 | callback: Method handle |
|
69 | callback: Method handle | |
70 | Function to be called when the text has been submitted. Function |
|
70 | Function to be called when the text has been submitted. Function | |
71 | can have two possible signatures: |
|
71 | can have two possible signatures: | |
72 | callback() |
|
72 | callback() | |
73 | callback(sender) |
|
73 | callback(sender) | |
74 | remove: bool (optional) |
|
74 | remove: bool (optional) | |
75 | Whether or not to unregister the callback""" |
|
75 | Whether or not to unregister the callback""" | |
76 | if remove and callback in self._submission_callbacks: |
|
76 | if remove and callback in self._submission_callbacks: | |
77 | self._submission_callbacks.remove(callback) |
|
77 | self._submission_callbacks.remove(callback) | |
78 | elif not remove and not callback in self._submission_callbacks: |
|
78 | elif not remove and not callback in self._submission_callbacks: | |
79 | if callable(callback): |
|
79 | if callable(callback): | |
80 | argspec = inspect.getargspec(callback) |
|
80 | argspec = inspect.getargspec(callback) | |
81 | nargs = len(argspec[0]) |
|
81 | nargs = len(argspec[0]) | |
82 |
|
82 | |||
83 | # Bound methods have an additional 'self' argument |
|
83 | # Bound methods have an additional 'self' argument | |
84 | if isinstance(callback, types.MethodType): |
|
84 | if isinstance(callback, types.MethodType): | |
85 | nargs -= 1 |
|
85 | nargs -= 1 | |
86 |
|
86 | |||
87 | # Call the callback |
|
87 | # Call the callback | |
88 | if nargs == 0: |
|
88 | if nargs == 0: | |
89 | self._submission_callbacks.append(lambda sender: callback()) |
|
89 | self._submission_callbacks.append(lambda sender: callback()) | |
90 | elif nargs == 1: |
|
90 | elif nargs == 1: | |
91 | self._submission_callbacks.append(callback) |
|
91 | self._submission_callbacks.append(callback) | |
92 | else: |
|
92 | else: | |
93 | raise TypeError('TextBoxWidget submit callback must ' \ |
|
93 | raise TypeError('TextBoxWidget submit callback must ' \ | |
94 | 'accept 0 or 1 arguments.') |
|
94 | 'accept 0 or 1 arguments.') |
General Comments 0
You need to be logged in to leave comments.
Login now