Widget Events.ipynb
225 lines
| 5.2 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14323 | { | |
"metadata": { | |||
Jonathan Frederic
|
r14342 | "cell_tags": [ | |
[ | |||
"<None>", | |||
null | |||
] | |||
], | |||
Jonathan Frederic
|
r17509 | "celltoolbar": "Slideshow", | |
Brian E. Granger
|
r16098 | "name": "", | |
Jonathan Frederic
|
r17515 | "signature": "sha256:43e4910dd01111e1206f4f7940a201d6d5e69485df79de96e0ad927eb0046226" | |
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
|
r17509 | "The `ButtonWidget` is not 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 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", | |
"print(widgets.ButtonWidget.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", | |
"button = widgets.ButtonWidget(description=\"Click Me!\")\n", | |||
"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
|
r17509 | "The **`TextWidget`** 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": [ | |||
"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": {}, | |||
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", | |||
"Using this method, an example of how to output an IntSliderWiget's value as it is changed can be seen below." | |||
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17492 | "int_range = widgets.IntSliderWidget()\n", | |
"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": {} | |||
} | |||
] | |||
} |