diff --git a/examples/Interactive Widgets/Widget Events.ipynb b/examples/Interactive Widgets/Widget Events.ipynb index dcb0c08..47f279d 100644 --- a/examples/Interactive Widgets/Widget Events.ipynb +++ b/examples/Interactive Widgets/Widget Events.ipynb @@ -7,7 +7,7 @@ ] ], "name": "", - "signature": "sha256:8cade57fabc6819dc950bc28502028554861fb1440d5d832922b95fd2b8bf25c" + "signature": "sha256:b22622c3f45a7044f2bec2270852ca07a20e589d185d87a6c659f6e3f33547f4" }, "nbformat": 3, "nbformat_minor": 0, @@ -18,36 +18,26 @@ "cell_type": "code", "collapsed": false, "input": [ - "from __future__ import print_function # 2.7 compatability\n", - "\n", - "from IPython.html import widgets # Widget definitions\n", - "from IPython.display import display # Used to display widgets in the notebook" + "from __future__ import print_function" ], "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "Traitlet Events" - ] + "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "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." + "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": [ - "print(widgets.Widget.on_trait_change.__doc__)" + "from IPython.html import widgets\n", + "print(widgets.ButtonWidget.on_click.__doc__)" ], "language": "python", "metadata": {}, @@ -56,94 +46,89 @@ "output_type": "stream", "stream": "stdout", "text": [ - "Setup a handler to be called when a trait changes.\n", - "\n", - " This is used to setup dynamic notifications of trait changes.\n", + "Register a callback to execute when the button is clicked.\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", + " The callback will be called with one argument,\n", + " the clicked button widget instance.\n", "\n", " Parameters\n", " ----------\n", - " 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" + " remove : bool (optional)\n", + " Set to true to remove the callback from the list of callbacks.\n" ] } ], - "prompt_number": 2 + "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "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." + "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": [ - "int_range = widgets.IntSliderWidget()\n", - "display(int_range)\n", + "from IPython.display import display\n", + "button = widgets.ButtonWidget(description=\"Click Me!\")\n", + "display(button)\n", "\n", - "def on_value_change(name, value):\n", - " print(value)\n", + "def on_button_clicked(b):\n", + " print(\"Button clicked.\")\n", "\n", - "int_range.on_trait_change(on_value_change, 'value')" + "button.on_click(on_button_clicked)" ], "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 3 + "prompt_number": 14 }, { - "cell_type": "heading", - "level": 1, + "cell_type": "markdown", "metadata": {}, "source": [ - "Specialized Events" + "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", - "level": 2, + "level": 1, "metadata": {}, "source": [ - "Button Click Event" + "Traitlet Events" ] }, { "cell_type": "markdown", "metadata": {}, "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." + "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." ] }, { "cell_type": "code", "collapsed": false, "input": [ - "print(widgets.ButtonWidget.on_click.__doc__)" + "print(widgets.Widget.on_trait_change.__doc__)" ], "language": "python", "metadata": {}, @@ -152,94 +137,72 @@ "output_type": "stream", "stream": "stdout", "text": [ - "Register a callback to execute when the button is clicked.\n", + "Setup a handler to be called when a trait changes.\n", "\n", - " The callback will be called with one argument,\n", - " the clicked button widget instance.\n", + " 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", "\n", " Parameters\n", " ----------\n", - " remove : bool (optional)\n", - " Set to true to remove the callback from the list of callbacks.\n" + " 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" ] } ], - "prompt_number": 4 + "prompt_number": 16 }, { "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." + "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." ] }, { "cell_type": "code", "collapsed": false, "input": [ - "button = widgets.ButtonWidget(description=\"Click Me!\")\n", - "display(button)\n", + "int_range = widgets.IntSliderWidget()\n", + "display(int_range)\n", "\n", - "def on_button_clicked(b):\n", - " print(\"Button clicked.\")\n", + "def on_value_change(name, value):\n", + " print(value)\n", "\n", - "button.on_click(on_button_clicked)" + "int_range.on_trait_change(on_value_change, 'value')" ], "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 + "outputs": [], + "prompt_number": 17 }, { "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." + "[Next](Widget Styling.ipynb)" ] - }, - { - "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": {}