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