Widget Events.ipynb
233 lines
| 5.3 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14323 | { | |
"metadata": { | |||
Jonathan Frederic
|
r14342 | "cell_tags": [ | |
[ | |||
"<None>", | |||
null | |||
] | |||
], | |||
Jonathan Frederic
|
r17726 | "kernelspec": { | |
"codemirror_mode": { | |||
"name": "python", | |||
"version": 2 | |||
}, | |||
"display_name": "Python 2", | |||
"language": "python", | |||
"name": "python2" | |||
}, | |||
Brian E. Granger
|
r16098 | "name": "", | |
Jonathan Frederic
|
r17946 | "signature": "sha256:05a3e92089b37f68e3134587ffef6ef73830e5f8b3c515ba24640d7c803820c3" | |
Jonathan Frederic
|
r14323 | }, | |
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
Jonathan Frederic
|
r17501 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
"[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)" | |||
] | |||
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r17501 | "source": [ | |
"Widget Events" | |||
] | |||
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 2, | |||
"metadata": {}, | |||
"source": [ | |||
"Special events" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17492 | "from __future__ import print_function" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17726 | "The `Button` is not used to represent a data type. Instead the button widget is used to **handle mouse clicks**. The **`on_click` method** of the `Button` can be used to register function to be called when the button is clicked. The doc string of the `on_click` can be seen below." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17492 | "from IPython.html import widgets\n", | |
Jonathan Frederic
|
r17726 | "print(widgets.Button.on_click.__doc__)" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Example" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "Since button clicks are **stateless**, they 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
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17492 | "from IPython.display import display\n", | |
Jonathan Frederic
|
r17726 | "button = widgets.Button(description=\"Click Me!\")\n", | |
Jonathan Frederic
|
r17492 | "display(button)\n", | |
Jonathan Frederic
|
r14323 | "\n", | |
Jonathan Frederic
|
r17492 | "def on_button_clicked(b):\n", | |
" print(\"Button clicked.\")\n", | |||
Jonathan Frederic
|
r14323 | "\n", | |
Jonathan Frederic
|
r17492 | "button.on_click(on_button_clicked)" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"on_sumbit" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r17492 | "cell_type": "markdown", | |
Jonathan Frederic
|
r14323 | "metadata": {}, | |
"source": [ | |||
Jonathan Frederic
|
r17726 | "The **`Text`** also has a special **`on_submit` event**. The `on_submit` event **fires when the user hits return**." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
Jonathan Frederic
|
r17492 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17726 | "text = widgets.Text()\n", | |
Jonathan Frederic
|
r17492 | "display(text)\n", | |
"\n", | |||
"def handle_submit(sender):\n", | |||
" print(text.value)\n", | |||
"\n", | |||
"text.on_submit(handle_submit)" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jonathan Frederic
|
r17492 | }, | |
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "heading", | |
Jonathan Frederic
|
r17501 | "level": 2, | |
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r14323 | "source": [ | |
Jonathan Frederic
|
r17501 | "Traitlet events" | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "**Widget properties are IPython traitlets** and **traitlets are eventful**. To handle changes, the **`on_trait_change` method** of the widget can be used to **register a callback**. The doc string for `on_trait_change` can be seen below." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17492 | "print(widgets.Widget.on_trait_change.__doc__)" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Signatures" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "Mentioned in the doc string, the callback registered can have **4 possible signatures**:\n", | |
Jonathan Frederic
|
r17492 | "\n", | |
"- callback()\n", | |||
"- callback(trait_name)\n", | |||
"- callback(trait_name, new_value)\n", | |||
"- callback(trait_name, old_value, new_value)\n", | |||
"\n", | |||
Jonathan Frederic
|
r17726 | "Using this method, an example of how to output an `IntSlider`'s value as it is changed can be seen below." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17726 | "int_range = widgets.IntSlider()\n", | |
Jonathan Frederic
|
r17492 | "display(int_range)\n", | |
Jason Grout
|
r14505 | "\n", | |
Jonathan Frederic
|
r17492 | "def on_value_change(name, value):\n", | |
" print(value)\n", | |||
Jason Grout
|
r14505 | "\n", | |
Jonathan Frederic
|
r17492 | "int_range.on_trait_change(on_value_change, 'value')" | |
Jason Grout
|
r14505 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17515 | "outputs": [] | |
Jason Grout
|
r14505 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17501 | "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)" | |
Jason Grout
|
r14505 | ] | |
Jonathan Frederic
|
r14323 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |