Show More
@@ -1,75 +1,80 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 Widget |
|
20 | from .widget import Widget | |
21 | from IPython.utils.traitlets import Unicode, Bool, Int |
|
21 | from IPython.utils.traitlets import Unicode, Bool, Int | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Classes |
|
24 | # Classes | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | class ButtonWidget(Widget): |
|
26 | class ButtonWidget(Widget): | |
27 | target_name = Unicode('ButtonWidgetModel') |
|
27 | target_name = Unicode('ButtonWidgetModel') | |
28 | default_view_name = Unicode('ButtonView') |
|
28 | default_view_name = Unicode('ButtonView') | |
29 |
|
29 | |||
30 | # Keys |
|
30 | # Keys | |
31 | _keys = ['clicks', 'description', 'disabled'] |
|
31 | _keys = ['clicks', 'description', 'disabled'] | |
32 | clicks = Int(0, help="Number of times the button has been clicked.") |
|
32 | clicks = Int(0, help="Number of times the button has been clicked.") | |
33 | description = Unicode('', help="Description of the button (label).") |
|
33 | description = Unicode('', help="Description of the button (label).") | |
34 | disabled = Bool(False, help="Enable or disable user changes.") |
|
34 | disabled = Bool(False, help="Enable or disable user changes.") | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | def __init__(self, **kwargs): |
|
37 | def __init__(self, **kwargs): | |
38 | self._click_handlers = [] |
|
38 | self._click_handlers = [] | |
39 | super(ButtonWidget, self).__init__(**kwargs) |
|
39 | super(ButtonWidget, self).__init__(**kwargs) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def on_click(self, callback, remove=False): |
|
42 | def on_click(self, callback, remove=False): | |
43 | """Register a callback to execute when the button is clicked. |
|
43 | """Register a callback to execute when the button is clicked. The | |
|
44 | callback can either accept no parameters or one sender parameter: | |||
|
45 | - callback() | |||
|
46 | - callback(sender) | |||
|
47 | If the callback has a sender parameter, the ButtonWidget instance that | |||
|
48 | called the callback will be passed into the method as the sender. | |||
44 |
|
49 | |||
45 | Parameters |
|
50 | Parameters | |
46 | ---------- |
|
51 | ---------- | |
47 | remove : bool (optional) |
|
52 | remove : bool (optional) | |
48 | Set to tru to remove the callback from the list of callbacks.""" |
|
53 | Set to true to remove the callback from the list of callbacks.""" | |
49 | if remove: |
|
54 | if remove: | |
50 | self._click_handlers.remove(callback) |
|
55 | self._click_handlers.remove(callback) | |
51 | else: |
|
56 | else: | |
52 | self._click_handlers.append(callback) |
|
57 | self._click_handlers.append(callback) | |
53 |
|
58 | |||
54 |
|
59 | |||
55 | def _clicks_changed(self, name, old, new): |
|
60 | def _clicks_changed(self, name, old, new): | |
56 | """Handles when the clicks property has been changed. Fires on_click |
|
61 | """Handles when the clicks property has been changed. Fires on_click | |
57 | callbacks when appropriate.""" |
|
62 | callbacks when appropriate.""" | |
58 | if new > old: |
|
63 | if new > old: | |
59 | for handler in self._click_handlers: |
|
64 | for handler in self._click_handlers: | |
60 | if callable(handler): |
|
65 | if callable(handler): | |
61 | argspec = inspect.getargspec(handler) |
|
66 | argspec = inspect.getargspec(handler) | |
62 | nargs = len(argspec[0]) |
|
67 | nargs = len(argspec[0]) | |
63 |
|
68 | |||
64 | # Bound methods have an additional 'self' argument |
|
69 | # Bound methods have an additional 'self' argument | |
65 | if isinstance(handler, types.MethodType): |
|
70 | if isinstance(handler, types.MethodType): | |
66 | nargs -= 1 |
|
71 | nargs -= 1 | |
67 |
|
72 | |||
68 | # Call the callback |
|
73 | # Call the callback | |
69 | if nargs == 0: |
|
74 | if nargs == 0: | |
70 | handler() |
|
75 | handler() | |
71 | elif nargs == 1: |
|
76 | elif nargs == 1: | |
72 | handler(self) |
|
77 | handler(self) | |
73 | else: |
|
78 | else: | |
74 | raise TypeError('ButtonWidget click callback must ' \ |
|
79 | raise TypeError('ButtonWidget click callback must ' \ | |
75 | 'accept 0 or 1 arguments.') |
|
80 | 'accept 0 or 1 arguments.') |
General Comments 0
You need to be logged in to leave comments.
Login now