Widget Basics.ipynb
416 lines
| 9.6 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14323 | { | |
"metadata": { | |||
Jonathan Frederic
|
r17509 | "celltoolbar": "Slideshow", | |
Brian E. Granger
|
r16098 | "name": "", | |
Jonathan Frederic
|
r17509 | "signature": "sha256:7a953d1eb1417e7212ddeb70602b36355521ca1907ac33b089850ccea35bd8ab" | |
Jonathan Frederic
|
r14323 | }, | |
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17501 | "[Index](Index.ipynb) - [Next](Widget List.ipynb)" | |
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
"%%html\n", | |||
"<style>div.text_cell.cell.rendered.slideshow-slide { margin-top: 5em !important; }</style>" | |||
], | |||
"language": "python", | |||
Jonathan Frederic
|
r17501 | "metadata": {}, | |
Jonathan Frederic
|
r17509 | "outputs": [ | |
{ | |||
"html": [ | |||
"<style>div.text_cell.cell.rendered.slideshow-slide { margin-top: 5em !important; }</style>" | |||
], | |||
"metadata": {}, | |||
"output_type": "display_data", | |||
"text": [ | |||
"<IPython.core.display.HTML object>" | |||
] | |||
} | |||
], | |||
"prompt_number": 11 | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r17501 | "source": [ | |
"# Simple Widget Introduction\n", | |||
Jonathan Frederic
|
r17493 | "\n", | |
"## What are widgets?\n", | |||
Jonathan Frederic
|
r17509 | "Widgets are elements that exists in both the front-end and the back-end.\n", | |
Jonathan Frederic
|
r17493 | "\n", | |
Jonathan Frederic
|
r17509 | "** Insert Frontend-Backend Picture **" | |
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"## What can they be used for?\n", | |||
"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": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
Jonathan Frederic
|
r17493 | "## Using widgets \n", | |
Jonathan Frederic
|
r17509 | "To use the widget framework, you need to **import `IPython.html.widgets`**." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "from IPython.html.widgets import *" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r14342 | "outputs": [], | |
Jonathan Frederic
|
r17501 | "prompt_number": 1 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"repr" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "Widgets have their own display `repr` which allows them to be displayed using IPython's display framework. Constructing and returning an `IntSliderWidget` 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." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "IntSliderWidget()" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r17493 | "outputs": [], | |
Jonathan Frederic
|
r17501 | "prompt_number": 2 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"display()" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17493 | "You can also explicitly display the widget using `display(...)`." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "from IPython.display import display\n", | |
"w = IntSliderWidget()\n", | |||
"display(w)" | |||
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jason Grout
|
r14505 | "outputs": [], | |
Jonathan Frederic
|
r17501 | "prompt_number": 3 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Multiple display() calls" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "If you display the same widget twice, the displayed instances in the front-end **will remain in sync** with each other." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "display(w)" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 4 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r14708 | "cell_type": "markdown", | |
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"## Why does displaying the same widget twice work?\n", | |||
"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", | |||
"** Insert Backend-Frontend Views Figure **" | |||
] | |||
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Closing widgets" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
Jonathan Frederic
|
r14708 | "metadata": {}, | |
"source": [ | |||
Jonathan Frederic
|
r17493 | "You can close a widget by calling its `close()` method." | |
Jonathan Frederic
|
r14708 | ] | |
}, | |||
{ | |||
Jason Grout
|
r14486 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "w.close()" | |
Jason Grout
|
r14486 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r14708 | "outputs": [], | |
Jonathan Frederic
|
r17501 | "prompt_number": 5 | |
Jason Grout
|
r14486 | }, | |
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r14323 | "source": [ | |
Jonathan Frederic
|
r17493 | "## Widget properties\n", | |
Jonathan Frederic
|
r17509 | "All of the IPython widgets **share a similar naming scheme**. To read the value of a widget, you can query its `value` property." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "w.value" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [ | |||
{ | |||
"metadata": {}, | |||
"output_type": "pyout", | |||
Jonathan Frederic
|
r17501 | "prompt_number": 6, | |
Jonathan Frederic
|
r14323 | "text": [ | |
Jonathan Frederic
|
r17501 | "0" | |
Jonathan Frederic
|
r14323 | ] | |
} | |||
], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 6 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17493 | "Similarly, to set a widget's value, you can set its `value` property." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "w.value = 100" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 7 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
Jonathan Frederic
|
r17509 | "cell_type": "heading", | |
"level": 3, | |||
"metadata": { | |||
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
"source": [ | |||
"Keys" | |||
] | |||
}, | |||
{ | |||
Jonathan Frederic
|
r14323 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17509 | "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**." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "w.keys" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [ | |||
{ | |||
"metadata": {}, | |||
"output_type": "pyout", | |||
Jonathan Frederic
|
r17501 | "prompt_number": 8, | |
Jonathan Frederic
|
r14323 | "text": [ | |
Jonathan Frederic
|
r17493 | "['_view_name',\n", | |
" 'orientation',\n", | |||
" 'msg_throttle',\n", | |||
" 'min',\n", | |||
" 'max',\n", | |||
" '_css',\n", | |||
" 'value',\n", | |||
" 'readout',\n", | |||
" 'disabled',\n", | |||
" 'visible',\n", | |||
" 'step',\n", | |||
" 'description']" | |||
Jonathan Frederic
|
r14323 | ] | |
} | |||
], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 8 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "markdown", | |||
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r14323 | "source": [ | |
Jonathan Frederic
|
r17509 | "### Shorthand for setting the initial values of widget properties\n", | |
"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)." | |||
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "TextWidget(value='Hello World!', disabled=True)" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jason Grout
|
r14505 | "outputs": [], | |
Jonathan Frederic
|
r17501 | "prompt_number": 9 | |
Jessica B. Hamrick
|
r16324 | }, | |
{ | |||
"cell_type": "markdown", | |||
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jessica B. Hamrick
|
r16324 | "source": [ | |
Jonathan Frederic
|
r17493 | "## Linking two similar widgets\n", | |
Jonathan Frederic
|
r17509 | "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 three widgets are linked together." | |
Jessica B. Hamrick
|
r16324 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "from IPython.utils.traitlets import link\n", | |
"a = FloatTextWidget()\n", | |||
"b = FloatSliderWidget()\n", | |||
"c = FloatProgressWidget()\n", | |||
"display(a,b,c)\n", | |||
"\n", | |||
"\n", | |||
"mylink = link((a, 'value'), (b, 'value'), (c, 'value'))" | |||
Jessica B. Hamrick
|
r16324 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 10 | |
Jonathan Frederic
|
r17493 | }, | |
{ | |||
"cell_type": "markdown", | |||
Jonathan Frederic
|
r17509 | "metadata": { | |
"slideshow": { | |||
"slide_type": "slide" | |||
} | |||
}, | |||
Jonathan Frederic
|
r17493 | "source": [ | |
Jonathan Frederic
|
r17509 | "### Unlinking widgets\n", | |
Jonathan Frederic
|
r17493 | "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object." | |
] | |||
Jessica B. Hamrick
|
r16324 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r17493 | "mylink.unlink()" | |
Jessica B. Hamrick
|
r16324 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r17501 | "prompt_number": 11 | |
Jonathan Frederic
|
r17493 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r17501 | "[Index](Index.ipynb) - [Next](Widget List.ipynb)" | |
Jonathan Frederic
|
r17493 | ] | |
Jonathan Frederic
|
r14323 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |