mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-06 11:35:24 +08:00
Updated widget events for pydata2014
This commit is contained in:
parent
5c103ab202
commit
a8cad2fa7d
@ -7,7 +7,7 @@
|
|||||||
]
|
]
|
||||||
],
|
],
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:8cade57fabc6819dc950bc28502028554861fb1440d5d832922b95fd2b8bf25c"
|
"signature": "sha256:b22622c3f45a7044f2bec2270852ca07a20e589d185d87a6c659f6e3f33547f4"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
@ -18,15 +18,96 @@
|
|||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"collapsed": false,
|
"collapsed": false,
|
||||||
"input": [
|
"input": [
|
||||||
"from __future__ import print_function # 2.7 compatability\n",
|
"from __future__ import print_function"
|
||||||
"\n",
|
|
||||||
"from IPython.html import widgets # Widget definitions\n",
|
|
||||||
"from IPython.display import display # Used to display widgets in the notebook"
|
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 1
|
"prompt_number": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"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."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"from IPython.html import widgets\n",
|
||||||
|
"print(widgets.ButtonWidget.on_click.__doc__)"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"Register a callback to execute when the button is clicked.\n",
|
||||||
|
"\n",
|
||||||
|
" The callback will be called with one argument,\n",
|
||||||
|
" the clicked button widget instance.\n",
|
||||||
|
"\n",
|
||||||
|
" Parameters\n",
|
||||||
|
" ----------\n",
|
||||||
|
" remove : bool (optional)\n",
|
||||||
|
" Set to true to remove the callback from the list of callbacks.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 13
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"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."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"from IPython.display import display\n",
|
||||||
|
"button = widgets.ButtonWidget(description=\"Click Me!\")\n",
|
||||||
|
"display(button)\n",
|
||||||
|
"\n",
|
||||||
|
"def on_button_clicked(b):\n",
|
||||||
|
" print(\"Button clicked.\")\n",
|
||||||
|
"\n",
|
||||||
|
"button.on_click(on_button_clicked)"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"prompt_number": 14
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The `TextWidget` also has a special `on_submit` event. The `on_submit` event fires when the user hits return."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "heading",
|
"cell_type": "heading",
|
||||||
@ -40,7 +121,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"As mentioned in Part 1, the widget attributes 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."
|
"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."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -83,7 +164,7 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 2
|
"prompt_number": 16
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -114,132 +195,14 @@
|
|||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 3
|
"prompt_number": 17
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "heading",
|
|
||||||
"level": 1,
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Specialized Events"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "heading",
|
|
||||||
"level": 2,
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Button Click Event"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"The `ButtonWidget` is a special widget, like the `ContainerWidget` and `TabWidget`, that 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."
|
"[Next](Widget Styling.ipynb)"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [
|
|
||||||
"print(widgets.ButtonWidget.on_click.__doc__)"
|
|
||||||
],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"output_type": "stream",
|
|
||||||
"stream": "stdout",
|
|
||||||
"text": [
|
|
||||||
"Register a callback to execute when the button is clicked.\n",
|
|
||||||
"\n",
|
|
||||||
" The callback will be called with one argument,\n",
|
|
||||||
" the clicked button widget instance.\n",
|
|
||||||
"\n",
|
|
||||||
" Parameters\n",
|
|
||||||
" ----------\n",
|
|
||||||
" remove : bool (optional)\n",
|
|
||||||
" Set to true to remove the callback from the list of callbacks.\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"prompt_number": 4
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"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."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [
|
|
||||||
"button = widgets.ButtonWidget(description=\"Click Me!\")\n",
|
|
||||||
"display(button)\n",
|
|
||||||
"\n",
|
|
||||||
"def on_button_clicked(b):\n",
|
|
||||||
" print(\"Button clicked.\")\n",
|
|
||||||
"\n",
|
|
||||||
"button.on_click(on_button_clicked)"
|
|
||||||
],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"output_type": "stream",
|
|
||||||
"stream": "stdout",
|
|
||||||
"text": [
|
|
||||||
"Button clicked.\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"output_type": "stream",
|
|
||||||
"stream": "stdout",
|
|
||||||
"text": [
|
|
||||||
"Button clicked.\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"output_type": "stream",
|
|
||||||
"stream": "stdout",
|
|
||||||
"text": [
|
|
||||||
"Button clicked.\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"prompt_number": 5
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Event handlers can also be used to create widgets. In the example below, clicking a button spawns another button with a description equal to how many times the parent button had been clicked at the time."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [
|
|
||||||
"def new_button(clicked):\n",
|
|
||||||
" button = widgets.ButtonWidget()\n",
|
|
||||||
" button.clicks = 0\n",
|
|
||||||
" clicked.clicks += 1\n",
|
|
||||||
" button.description = \"%d\" % clicked.clicks\n",
|
|
||||||
" display(button)\n",
|
|
||||||
" button.on_click(new_button)\n",
|
|
||||||
"button = widgets.ButtonWidget(description = \"Start\")\n",
|
|
||||||
"button.clicks = 0\n",
|
|
||||||
"display(button)\n",
|
|
||||||
"button.on_click(new_button)\n",
|
|
||||||
" "
|
|
||||||
],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"prompt_number": 6
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {}
|
"metadata": {}
|
||||||
|
Loading…
Reference in New Issue
Block a user