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