mirror of
https://github.com/jupyter/notebook.git
synced 2025-03-13 13:17:50 +08:00
Add button widget
This commit is contained in:
parent
e619bdf313
commit
c20859a5c9
31
IPython/html/static/notebook/js/widgets/button.js
Normal file
31
IPython/html/static/notebook/js/widgets/button.js
Normal 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);
|
||||
|
||||
});
|
@ -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
|
||||
|
40
IPython/html/widgets/widget_button.py
Normal file
40
IPython/html/widgets/widget_button.py
Normal 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.')
|
||||
|
Loading…
Reference in New Issue
Block a user