##// END OF EJS Templates
Updated widget events for pydata2014
Updated widget events for pydata2014

File last commit:

r17492:fc54bd56
r17492:fc54bd56
Show More
Widget Events.ipynb
210 lines | 6.1 KiB | text/plain | TextLexer
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 {
"metadata": {
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "cell_tags": [
[
"<None>",
null
]
],
Brian E. Granger
Work on widget tutorials
r16098 "name": "",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "signature": "sha256:b22622c3f45a7044f2bec2270852ca07a20e589d185d87a6c659f6e3f33547f4"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "from __future__ import print_function"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "outputs": [],
Jonathan Frederic
Updated widget events for pydata2014
r17492 "prompt_number": 12
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "The `ButtonWidget` isn't used to represent a data type. Instead the button widget is used to handle mouse clicks. The `on_click` method of the `ButtonWidget` can be used to register function to be called when the button is clicked. The docstring of the `on_click` can be seen below."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "from IPython.html import widgets\n",
"print(widgets.ButtonWidget.on_click.__doc__)"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Register a callback to execute when the button is clicked.\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 " The callback will be called with one argument,\n",
" the clicked button widget instance.\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "\n",
" Parameters\n",
" ----------\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 " remove : bool (optional)\n",
" Set to true to remove the callback from the list of callbacks.\n"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
}
],
Jonathan Frederic
Updated widget events for pydata2014
r17492 "prompt_number": 13
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Button clicks are transmitted from the front-end to the back-end using custom messages. By using the `on_click` method, a button that prints a message when it has been clicked is shown below."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "from IPython.display import display\n",
"button = widgets.ButtonWidget(description=\"Click Me!\")\n",
"display(button)\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "def on_button_clicked(b):\n",
" print(\"Button clicked.\")\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "button.on_click(on_button_clicked)"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Renamed widgets......
r14834 "outputs": [],
Jonathan Frederic
Updated widget events for pydata2014
r17492 "prompt_number": 14
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
Jonathan Frederic
Updated widget events for pydata2014
r17492 "cell_type": "markdown",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "The `TextWidget` also has a special `on_submit` event. The `on_submit` event fires when the user hits return."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
Jonathan Frederic
Updated widget events for pydata2014
r17492 "cell_type": "code",
"collapsed": false,
"input": [
"text = widgets.TextWidget()\n",
"display(text)\n",
"\n",
"def handle_submit(sender):\n",
" print(text.value)\n",
"\n",
"text.on_submit(handle_submit)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "cell_type": "heading",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "level": 1,
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Traitlet Events"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Widget properties are IPython traitlets. Traitlets are eventful. To handle changes, the `on_trait_change` method of the widget can be used to register a callback. The docstring for `on_trait_change` can be seen below. Both the `name` and `remove` properties are optional."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "print(widgets.Widget.on_trait_change.__doc__)"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Setup a handler to be called when a trait changes.\n",
Jonathan Frederic
Updated widget tutorials 1-2
r14708 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 " This is used to setup dynamic notifications of trait changes.\n",
"\n",
" Static handlers can be created by creating methods on a HasTraits\n",
" subclass with the naming convention '_[traitname]_changed'. Thus,\n",
" to create static handler for the trait 'a', create the method\n",
" _a_changed(self, name, old, new) (fewer arguments can be used, see\n",
" below).\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "\n",
" Parameters\n",
" ----------\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 " handler : callable\n",
" A callable that is called when a trait changes. Its\n",
" signature can be handler(), handler(name), handler(name, new)\n",
" or handler(name, old, new).\n",
" name : list, str, None\n",
" If None, the handler will apply to all traits. If a list\n",
" of str, handler will apply to all names in the list. If a\n",
" str, the handler will apply just to that name.\n",
" remove : bool\n",
" If False (the default), then install the handler. If True\n",
" then unintall it.\n",
" \n"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
}
],
Jonathan Frederic
Updated widget events for pydata2014
r17492 "prompt_number": 16
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "Mentioned in the doc string, the callback registered can have 4 possible signatures:\n",
"\n",
"- callback()\n",
"- callback(trait_name)\n",
"- callback(trait_name, new_value)\n",
"- callback(trait_name, old_value, new_value)\n",
"\n",
"Using this method, an example of how to output an IntSliderWiget's value as it is changed can be seen below."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "int_range = widgets.IntSliderWidget()\n",
"display(int_range)\n",
Jason Grout
Example notebooks updated.
r14505 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "def on_value_change(name, value):\n",
" print(value)\n",
Jason Grout
Example notebooks updated.
r14505 "\n",
Jonathan Frederic
Updated widget events for pydata2014
r17492 "int_range.on_trait_change(on_value_change, 'value')"
Jason Grout
Example notebooks updated.
r14505 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Updated widget events for pydata2014
r17492 "outputs": [],
"prompt_number": 17
Jason Grout
Example notebooks updated.
r14505 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Updated widget events for pydata2014
r17492 "[Next](Widget Styling.ipynb)"
Jason Grout
Example notebooks updated.
r14505 ]
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 }
],
"metadata": {}
}
]
}