Add button widget

This commit is contained in:
Jonathan Frederic 2013-10-21 20:02:17 +00:00
parent e619bdf313
commit c20859a5c9
3 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,31 @@
require(["notebook/js/widget"], function(){
var ButtonWidgetModel = IPython.WidgetModel.extend({});
IPython.notebook.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel);
var ButtonView = IPython.WidgetView.extend({
// Called when view is rendered.
render : function(){
var that = this;
this.$el = $("<button />")
.addClass('btn')
.click(function() {
that.model.set('clicks', that.model.get('clicks') + 1)
});
this.update(); // Set defaults.
},
// Handles: Backend -> Frontend Sync
// Frontent -> Frontend Sync
update : function(){
this.$el.html(this.model.get('description'));
},
});
IPython.notebook.widget_manager.register_widget_view('ButtonView', ButtonView);
});

View File

@ -1,6 +1,7 @@
from widget import Widget, init_widget_js
from widget_bool import BoolWidget
from widget_button import ButtonWidget
from widget_container import ContainerWidget
from widget_float import FloatWidget
from widget_float_range import FloatRangeWidget

View File

@ -0,0 +1,40 @@
from base import Widget
from IPython.utils.traitlets import Unicode, Bool, Int
class ButtonWidget(Widget):
target_name = Unicode('ButtonWidgetModel')
default_view_name = Unicode('ButtonView')
_keys = ['clicks', 'description', 'disabled']
clicks = Int(0)
description = Unicode('') # Description of the button (label).
disabled = Bool(False) # Enable or disable user changes
_click_handlers = []
def handle_click(self, callback, remove=False):
if remove:
self._click_handlers.remove(callback)
else:
self._click_handlers.append(callback)
def _clicks_changed(self, name, old, new):
if new > old:
for handler in self._click_handlers:
if callable(handler):
argspec = inspect.getargspec(handler)
nargs = len(argspec[0])
# Bound methods have an additional 'self' argument
if isinstance(handler, types.MethodType):
nargs -= 1
# Call the callback
if nargs == 0:
handler()
elif nargs == 1:
handler(self)
else:
raise TypeError('ButtonWidget click callback must ' \
'accept 0 or 1 arguments.')