Show More
@@ -0,0 +1,31 b'' | |||||
|
1 | ||||
|
2 | require(["notebook/js/widget"], function(){ | |||
|
3 | ||||
|
4 | var ButtonWidgetModel = IPython.WidgetModel.extend({}); | |||
|
5 | IPython.notebook.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel); | |||
|
6 | ||||
|
7 | var ButtonView = IPython.WidgetView.extend({ | |||
|
8 | ||||
|
9 | // Called when view is rendered. | |||
|
10 | render : function(){ | |||
|
11 | var that = this; | |||
|
12 | this.$el = $("<button />") | |||
|
13 | .addClass('btn') | |||
|
14 | .click(function() { | |||
|
15 | that.model.set('clicks', that.model.get('clicks') + 1) | |||
|
16 | }); | |||
|
17 | ||||
|
18 | this.update(); // Set defaults. | |||
|
19 | }, | |||
|
20 | ||||
|
21 | // Handles: Backend -> Frontend Sync | |||
|
22 | // Frontent -> Frontend Sync | |||
|
23 | update : function(){ | |||
|
24 | this.$el.html(this.model.get('description')); | |||
|
25 | }, | |||
|
26 | ||||
|
27 | }); | |||
|
28 | ||||
|
29 | IPython.notebook.widget_manager.register_widget_view('ButtonView', ButtonView); | |||
|
30 | ||||
|
31 | }); |
@@ -0,0 +1,40 b'' | |||||
|
1 | from base import Widget | |||
|
2 | from IPython.utils.traitlets import Unicode, Bool, Int | |||
|
3 | ||||
|
4 | class ButtonWidget(Widget): | |||
|
5 | target_name = Unicode('ButtonWidgetModel') | |||
|
6 | default_view_name = Unicode('ButtonView') | |||
|
7 | _keys = ['clicks', 'description', 'disabled'] | |||
|
8 | ||||
|
9 | clicks = Int(0) | |||
|
10 | description = Unicode('') # Description of the button (label). | |||
|
11 | disabled = Bool(False) # Enable or disable user changes | |||
|
12 | ||||
|
13 | _click_handlers = [] | |||
|
14 | ||||
|
15 | def handle_click(self, callback, remove=False): | |||
|
16 | if remove: | |||
|
17 | self._click_handlers.remove(callback) | |||
|
18 | else: | |||
|
19 | self._click_handlers.append(callback) | |||
|
20 | ||||
|
21 | def _clicks_changed(self, name, old, new): | |||
|
22 | if new > old: | |||
|
23 | for handler in self._click_handlers: | |||
|
24 | if callable(handler): | |||
|
25 | argspec = inspect.getargspec(handler) | |||
|
26 | nargs = len(argspec[0]) | |||
|
27 | ||||
|
28 | # Bound methods have an additional 'self' argument | |||
|
29 | if isinstance(handler, types.MethodType): | |||
|
30 | nargs -= 1 | |||
|
31 | ||||
|
32 | # Call the callback | |||
|
33 | if nargs == 0: | |||
|
34 | handler() | |||
|
35 | elif nargs == 1: | |||
|
36 | handler(self) | |||
|
37 | else: | |||
|
38 | raise TypeError('ButtonWidget click callback must ' \ | |||
|
39 | 'accept 0 or 1 arguments.') | |||
|
40 |
@@ -1,6 +1,7 b'' | |||||
1 | from widget import Widget, init_widget_js |
|
1 | from widget import Widget, init_widget_js | |
2 |
|
2 | |||
3 | from widget_bool import BoolWidget |
|
3 | from widget_bool import BoolWidget | |
|
4 | from widget_button import ButtonWidget | |||
4 | from widget_container import ContainerWidget |
|
5 | from widget_container import ContainerWidget | |
5 | from widget_float import FloatWidget |
|
6 | from widget_float import FloatWidget | |
6 | from widget_float_range import FloatRangeWidget |
|
7 | from widget_float_range import FloatRangeWidget |
General Comments 0
You need to be logged in to leave comments.
Login now