In [1]:
from __future__ import print_function # For py 2.7 compat
from IPython.html import widgets # Widget definitions
from IPython.display import display # Used to display widgets in the notebook
from IPython.utils.traitlets import Unicode # Used to declare attributes of our widget
In [2]:
class DateWidget(widgets.DOMWidget):
_view_name = Unicode('DatePickerView', sync=True)
value = Unicode(sync=True)
In [3]:
%%javascript
require(["widgets/js/widget"], function(WidgetManager){
// Define the DatePickerView
var DatePickerView = IPython.DOMWidgetView.extend({
render: function(){
// Create the date picker control.
this.$date = $('<input />')
.attr('type', 'date')
.appendTo(this.$el);
},
update: function() {
// Set the value of the date control and then call base.
this.$date.val(this.model.get('value')); // ISO format "YYYY-MM-DDTHH:mm:ss.sssZ" is required
return DatePickerView.__super__.update.apply(this);
},
// Tell Backbone to listen to the change event of input controls (which the HTML date picker is)
events: {"change": "handle_date_change"},
// Callback for when the date is changed.
handle_date_change: function(event) {
this.model.set('value', this.$date.val());
this.touch();
},
});
// Register the DatePickerView with the widget manager.
WidgetManager.register_widget_view('DatePickerView', DatePickerView);
});
In [4]:
my_widget = DateWidget()
display(my_widget)
In [5]:
my_widget
In [6]:
my_widget.value
Out[6]:
In [7]:
my_widget.value = "1998-12-07" # December 1st, 1998
In [31]:
In [ ]: