##// END OF EJS Templates
register bare views
register bare views

File last commit:

r17946:fd20d2a3
r17950:26c97f1e
Show More
Custom Widget - Hello World.ipynb
795 lines | 22.9 KiB | text/plain | TextLexer
/ examples / Interactive Widgets / Custom Widget - Hello World.ipynb
Jonathan Frederic
Added custom widget tutorial examples
r17494 {
"metadata": {
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "kernelspec": {
"codemirror_mode": {
"name": "python",
"version": 2
},
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "name": "",
Jonathan Frederic
Update examples, fixing bugs found in review.
r17946 "signature": "sha256:608e0df06fe91ef1c013fd236dda33f7ae11019453f72ae446e8ce15bce82eb2"
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
Jonathan Frederic
Updated widget notebooks for PyData2014
r17501 "cell_type": "markdown",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "metadata": {},
Jonathan Frederic
Updated widget notebooks for PyData2014
r17501 "source": [
"[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
]
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
Jonathan Frederic
Updated widget tutorial,...
r17509 "cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import print_function # For py 2.7 compat"
],
"language": "python",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
Jonathan Frederic
Updated widget tutorial,...
r17509 "cell_type": "heading",
"level": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Building a Custom Widget"
]
Jonathan Frederic
Updated widget notebooks for PyData2014
r17501 },
{
Jonathan Frederic
Added custom widget tutorial examples
r17494 "cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "The widget framework is built **on top of the Comm framework** (short for communication). The Comm framework is a framework that **allows you send/receive JSON messages** to/from the front-end (as seen below).\n",
"\n",
Jonathan Frederic
Added images
r17514 "![Widget layer](images/WidgetArch.png)\n",
Jonathan Frederic
Updated widget tutorial,...
r17509 "\n",
"To create a custom widget, you need to **define the widget both in the back-end and in the front-end**. "
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
Jonathan Frederic
Updated widget tutorial,...
r17509 "level": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Building a Custom Widget"
]
},
{
"cell_type": "markdown",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "To get started, you'll create a **simple hello world widget**. Later you'll build on this foundation to make more complex widgets."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
Jonathan Frederic
Added custom widget tutorial examples
r17494 "Back-end (Python)"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"DOMWidget and Widget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "To define a widget, you must inherit from the **Widget or DOMWidget** base class. If you intend for your widget to be **displayed in the IPython notebook**, you'll need to **inherit from the DOMWidget**. The DOMWidget class itself inherits from the Widget class. The Widget class is useful for cases in which the **Widget is not meant to be displayed directly in the notebook**, but **instead as a child of another rendering environment**. For example, if you wanted to create a three.js widget (a popular WebGL library), you would implement the rendering window as a DOMWidget and any 3D objects or lights meant to be rendered in that window as Widgets."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "_view_name"
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Inheriting from the DOMWidget does not tell the widget framework what front-end widget to associate with your back-end widget. Instead, you must tell it yourself by defining a **specially named Traitlet, `_view_name`** (as seen below)."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.html import widgets\n",
"from IPython.utils.traitlets import Unicode\n",
"\n",
"class HelloWidget(widgets.DOMWidget):\n",
" _view_name = Unicode('HelloView', sync=True)"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
Jonathan Frederic
Updated widget tutorial,...
r17509 "cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"sync=True traitlets"
]
},
{
Jonathan Frederic
Added custom widget tutorial examples
r17494 "cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "**Traitlets is** an IPython library for defining **type-safe properties** on configurable objects. For this tutorial you do not need to worry about the *configurable* piece of the traitlets machinery. The **`sync=True` keyword argument** tells the widget framework to **handle synchronizing that value to the front-end**. Without `sync=True`, the front-end would have no knowledge of `_view_name`."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Other traitlet types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Unicode, used for _view_name, is not the only Traitlet type, there are many more some of which are listed below: \n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "\n",
"- Any\n",
"- Bool\n",
"- Bytes\n",
"- CBool\n",
"- CBytes\n",
"- CComplex\n",
"- CFloat\n",
"- CInt\n",
"- CLong\n",
"- CRegExp\n",
"- CUnicode\n",
"- CaselessStrEnum\n",
"- Complex\n",
"- Dict\n",
"- DottedObjectName\n",
"- Enum\n",
"- Float\n",
"- FunctionType\n",
"- Instance\n",
"- InstanceType\n",
"- Int\n",
"- List\n",
"- Long\n",
"- Set\n",
"- TCPAddress\n",
"- Tuple\n",
"- Type\n",
"- Unicode\n",
"\n",
Jonathan Frederic
Updated widget tutorial,...
r17509 "**Not all of these traitlets can be synchronized** across the network, **only the JSON-able** traits and **Widget instances** will be synchronized."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 2,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Front-end (JavaScript)"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Models and views"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "The IPython widget framework front-end relies heavily on [Backbone.js](http://backbonejs.org/). **Backbone.js is an MVC (model view controller) framework**. Widgets defined in the back-end are automatically **synchronized with generic Backbone.js models** in the front-end. The traitlets are added to the front-end instance **automatically on first state push**. The **`_view_name` trait** that you defined earlier is used by the widget framework to create the corresponding Backbone.js view and **link that view to the model**."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Import the WidgetManager"
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "You first need to **import the `widget` and `manager` modules**. You will use it later to register your view by name (the same name you used in the back-end). To import the modules, use the `require` method of [require.js](http://requirejs.org/) (as seen below)."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
"});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
Jonathan Frederic
Updated widget tutorial,...
r17509 "cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Define the view"
]
},
{
Jonathan Frederic
Added custom widget tutorial examples
r17494 "cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Next define your widget view class. **Inherit from the `DOMWidgetView`** by using the `.extend` method. Register the view class with the widget manager by calling **`.register_widget_view`**. The **first parameter is the widget view name** (`_view_name` that you defined earlier in Python) and the **second is a handle to the class type**."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" // Define the HelloView\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var HelloView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" });\n",
" \n",
" // Register the HelloView with the widget manager.\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Updated widget tutorial,...
r17509 },
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Render method"
]
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Lastly, **override the base `render` method** of the view to define custom rendering logic. A handle to the widget's default div element can be acquired via **`this.$el`**. The `$el` property is a **[jQuery](http://jquery.com/) object handle** (which can be thought of as a supercharged version of the normal DOM element's handle)."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var HelloView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" // Render the view.\n",
" render: function(){ \n",
" this.$el.text('Hello World!'); \n",
" },\n",
" });\n",
" \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 2,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should be able to display your widget just like any other widget now."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"HelloWidget()"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 2,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Making the widget stateful"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "There is not much that you can do with the above example that you can't do with the IPython display framework. To change this, you will make the widget stateful. Instead of displaying a static \"hello world\" message, it will **display a string set by the back-end**. First you need to **add a traitlet in the back-end**. Use the name of **`value` to stay consistent** with the rest of the widget framework and to **allow your widget to be used with interact**."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class HelloWidget(widgets.DOMWidget):\n",
" _view_name = Unicode('HelloView', sync=True)\n",
" value = Unicode('Hello World!', sync=True)"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Accessing the model from the view"
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "To access the model associate with a view instance, use the **`model` property** of the view. **`get` and `set`** methods are used to interact with the Backbone model. **`get` is trivial**, however you have to **be careful when using `set`**. **After calling the model `set`** you need call the **view's `touch` method**. This associates the `set` operation with a particular view so **output will be routed to the correct cell**. The model also has a **`on` method** which allows you to listen to events triggered by the model (like value changes)."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Rendering model contents"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "By **replacing the string literal with a call to `model.get`**, the view will now display the **value of the back-end upon display**. However, it will not update itself to a new value when the value changes."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var HelloView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" render: function(){ \n",
" this.$el.text(this.model.get('value')); \n",
" },\n",
" });\n",
" \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Updated widget tutorial,...
r17509 },
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Dynamic updates"
]
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "To get the view to **update itself dynamically**, register a function to update the view's value when the model's `value` property changes. This can be done using the **`model.on` method**. The `on` method takes three parameters, an event name, callback handle, and callback context. The Backbone **event named `change`** will fire whenever the model changes. By **appending `:value`** to it, you tell Backbone to only listen to the change event of the `value` property (as seen below)."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var HelloView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" render: function(){ \n",
" this.value_changed();\n",
" this.model.on('change:value', this.value_changed, this);\n",
" },\n",
" \n",
" value_changed: function() {\n",
" this.$el.text(this.model.get('value')); \n",
" },\n",
" });\n",
" \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 2,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Test"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w = HelloWidget()\n",
"w"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"w.value = 'test'"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
Jonathan Frederic
Updated widget tutorial,...
r17509 "level": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Finishing"
]
},
{
"cell_type": "heading",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "level": 2,
"metadata": {},
"source": [
"Bidirectional communication"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "The examples above dump the value directly into the DOM. There is no way for you to interact with this dumped data in the front-end. To create an example that **accepts input**, you will have to do something more than blindly dumping the contents of value into the DOM. In this part of the tutorial, you will **use a jQuery spinner** to display and accept input in the front-end. IPython currently lacks a spinner implementation so this widget will be unique."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Update the Python code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "You will need to change the type of the **value traitlet to `Int`**. It also makes sense to **change the name of the widget** to something more appropriate, like `SpinnerWidget`."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.utils.traitlets import CInt\n",
"class SpinnerWidget(widgets.DOMWidget):\n",
" _view_name = Unicode('SpinnerView', sync=True)\n",
" value = CInt(0, sync=True)"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 3,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Updating the Javascript code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "The [jQuery docs for the spinner control](https://jqueryui.com/spinner/) say to use **`.spinner` to create a spinner** in an element. Calling **`.spinner` on `$el` will create a spinner inside `$el`**. Make sure to **update the widget name here too** so it's the same as `_view_name` in the back-end."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var SpinnerView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" render: function(){ \n",
" \n",
" // jQuery code to create a spinner and append it to $el\n",
" this.$input = $('<input />');\n",
" this.$el.append(this.$input);\n",
" this.$spinner = this.$input.spinner({\n",
" change: function( event, ui ) {}\n",
" });\n",
" \n",
" this.value_changed();\n",
" this.model.on('change:value', this.value_changed, this);\n",
" },\n",
" \n",
" value_changed: function() {\n",
" \n",
" },\n",
" });\n",
" \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
Jonathan Frederic
Updated widget tutorial,...
r17509 "cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Getting and setting the value"
]
},
{
Jonathan Frederic
Added custom widget tutorial examples
r17494 "cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "To **set the value of the spinner on update from the back-end**, you need to use **jQuery's `spinner` API**. `spinner.spinner('value', new)` will set the value of the spinner. Add that code to the **`value_changed` method** to make the spinner **update with the value stored in the back-end((. Using jQuery's spinner API, you can add a function to handle the **spinner `change` event** by passing it in when constructing the spinner. Inside the `change` event, call **`model.set`** to set the value and then **`touch`** to inform the framework that this view was the view that caused the change to the model. **Note: The `var that = this;` is a JavaScript trick to pass the current context into closures.**"
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " var SpinnerView = widget.DOMWidgetView.extend({\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 " \n",
" render: function(){ \n",
"\n",
" var that = this;\n",
" this.$input = $('<input />');\n",
" this.$el.append(this.$input);\n",
" this.$spinner = this.$input.spinner({\n",
" change: function( event, ui ) {\n",
" that.handle_spin();\n",
" },\n",
" spin: function( event, ui ) {\n",
" that.handle_spin();\n",
" }\n",
" });\n",
" \n",
" this.value_changed();\n",
" this.model.on('change:value', this.value_changed, this);\n",
" },\n",
" \n",
" value_changed: function() {\n",
" this.$spinner.spinner('value', this.model.get('value'));\n",
" },\n",
" \n",
" handle_spin: function() {\n",
" this.model.set('value', this.$spinner.spinner('value'));\n",
" this.touch();\n",
" },\n",
" });\n",
" \n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "});"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "heading",
"level": 2,
Jonathan Frederic
Updated widget tutorial,...
r17509 "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
Jonathan Frederic
Added custom widget tutorial examples
r17494 "source": [
"Test"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w = SpinnerWidget(value=5)\n",
"w"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"w.value"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"w.value = 20"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Added custom widget tutorial examples
r17494 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget tutorial,...
r17509 "Trying to **use the spinner with another widget**."
Jonathan Frederic
Added custom widget tutorial examples
r17494 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget notebooks for PyData2014
r17501 "from IPython.display import display\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "w1 = SpinnerWidget(value=0)\n",
Jonathan Frederic
Updated widget examples for recent changes and,...
r17726 "w2 = widgets.IntSlider()\n",
Jonathan Frederic
Added custom widget tutorial examples
r17494 "display(w1,w2)\n",
"\n",
"from IPython.utils.traitlets import link\n",
"mylink = link((w1, 'value'), (w2, 'value'))"
],
"language": "python",
"metadata": {},
Jonathan Frederic
Clear output
r17515 "outputs": []
Jonathan Frederic
Updated widget notebooks for PyData2014
r17501 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
]
Jonathan Frederic
Added custom widget tutorial examples
r17494 }
],
"metadata": {}
}
]
}