{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Widget Introduction\n", "Based on:\n", "https://github.com/ipython/ipywidgets/blob/master/docs/source/examples/Widget%20Events.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What are widgets?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Widgets are eventful python objects that have a representation in the browser, often as a control like a slider, textbox, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What can they be used for?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "You can use widgets to build **interactive GUIs** for your notebooks. \n", "You can also use widgets to **synchronize stateful and stateless information** between Python and JavaScript." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Activating widgets in SWAN\n", "Before using widgets, we need to enable the corresponding notebook extension. After running the following cell, please refresh your notebook page!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unrecognized JSON config file version, assuming version 1\n", "Enabling notebook extension jupyter-js-widgets/extension...\n", " - Validating: \u001b[32mOK\u001b[0m\n" ] } ], "source": [ "!jupyter nbextension enable --py widgetsnbextension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using widgets " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "To use the widget framework, you need to import `ipywidgets`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import *" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### repr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Widgets have their own display `repr` which allows them to be displayed using IPython's display framework. Constructing and returning an `IntSlider` automatically displays the widget (as seen below). Widgets are displayed inside the widget area, which sits between the code cell and output. You can hide all of the widgets in the widget area by clicking the grey *x* in the margin." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "IntSlider()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also explicitly display the widget using `display(...)`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import display\n", "w = IntSlider()\n", "display(w)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Multiple display() calls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you display the same widget twice, the displayed instances in the front-end will remain in sync with each other. Try dragging the slider below and watch the slider above." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display(w)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why does displaying the same widget twice work?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Widgets are represented in the back-end by a single object. Each time a widget is displayed, a new representation of that same object is created in the front-end. These representations are called views.\n", "\n", "![Kernel & front-end diagram](images/WidgetModelView.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Closing widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can close a widget by calling its `close()` method." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display(w)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "w.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Widget properties" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "All of the IPython widgets share a similar naming scheme. To read the value of a widget, you can query its `value` property." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "w = IntSlider()\n", "display(w)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, to set a widget's value, you can set its `value` property." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "w.value = 100" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Keys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to `value`, most widgets share `keys`, `description`, `disabled`, and `visible`. To see the entire list of synchronized, stateful properties of any specific widget, you can query the `keys` property." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['_view_name',\n", " 'orientation',\n", " 'color',\n", " '_view_module',\n", " 'disabled',\n", " 'visible',\n", " 'readout_format',\n", " '_model_module',\n", " 'font_style',\n", " 'layout',\n", " 'min',\n", " '_range',\n", " 'background_color',\n", " 'slider_color',\n", " 'continuous_update',\n", " 'font_family',\n", " '_dom_classes',\n", " 'description',\n", " '_model_name',\n", " 'max',\n", " 'readout',\n", " 'font_weight',\n", " 'step',\n", " 'font_size',\n", " 'msg_throttle',\n", " 'value']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w.keys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Shorthand for setting the initial values of widget properties" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "While creating a widget, you can set some or all of the initial values of that widget by defining them as keyword arguments in the widget's constructor (as seen below)." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Text(value='Hello World!', disabled=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linking two similar widgets" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If you need to display the same value two different ways, you'll have to use two different widgets. Instead of attempting to manually synchronize the values of the two widgets, you can use the `traitlet` `link` function to link two properties together. Below, the values of two widgets are linked together." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from traitlets import link\n", "a = FloatText()\n", "b = FloatSlider()\n", "display(a,b)\n", "\n", "mylink = link((a, 'value'), (b, 'value'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Unlinking widgets" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object. Try changing one of the widgets above after unlinking to see that they can be independently changed." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mylink.unlink()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" }, "widgets": { "state": { "20adc2d0be5745d8ad42332cada180e3": { "views": [ { "cell_index": 39 } ] }, "54c89b29902f4f0487c9327a29f0e279": { "views": [ { "cell_index": 27 } ] }, "601b124386f94c11a4b311a2273f003e": { "views": [ { "cell_index": 12 } ] }, "8fce4bf163bb480b8897acf177b6d53c": { "views": [ { "cell_index": 36 } ] }, "a73bc9e11eae414d888dd41d5dabf542": { "views": [ { "cell_index": 39 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }