{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Widget List\n", "\n", "This lecture will serve as a reference for widgets, providing a list of the GUI widgets available!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complete list" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For a complete list of the GUI widgets available to you, you can list the registered widget types. `Widget` is the base class." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "\n", "# Show all available widgets!\n", "for item in widgets.Widget.widget_types.items():\n", " print(item[0][2][:-5])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Numeric widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are 10 widgets distributed with IPython that are designed to display numeric values. Widgets exist for displaying integers and floats, both bounded and unbounded. The integer widgets share a similar naming scheme to their floating point counterparts. By replacing `Float` with `Int` in the widget name, you can find the Integer equivalent." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### IntSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.IntSlider(\n", " value=7,\n", " min=0,\n", " max=10,\n", " step=1,\n", " description='Test:',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='d'\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### FloatSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.FloatSlider(\n", " value=7.5,\n", " min=0,\n", " max=10.0,\n", " step=0.1,\n", " description='Test:',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='.1f',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sliders can also be **displayed vertically**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.FloatSlider(\n", " value=7.5,\n", " min=0,\n", " max=10.0,\n", " step=0.1,\n", " description='Test:',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='vertical',\n", " readout=True,\n", " readout_format='.1f',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IntRangeSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.IntRangeSlider(\n", " value=[5, 7],\n", " min=0,\n", " max=10,\n", " step=1,\n", " description='Test:',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='d',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FloatRangeSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.FloatRangeSlider(\n", " value=[5, 7.5],\n", " min=0,\n", " max=10.0,\n", " step=0.1,\n", " description='Test:',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='.1f',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IntProgress" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.IntProgress(\n", " value=7,\n", " min=0,\n", " max=10,\n", " step=1,\n", " description='Loading:',\n", " bar_style='', # 'success', 'info', 'warning', 'danger' or ''\n", " orientation='horizontal'\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### FloatProgress" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.FloatProgress(\n", " value=7.5,\n", " min=0,\n", " max=10.0,\n", " step=0.1,\n", " description='Loading:',\n", " bar_style='info',\n", " orientation='horizontal'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The numerical text boxes that impose some limit on the data (range, integer-only) impose that restriction when the user presses enter." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### BoundedIntText" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.BoundedIntText(\n", " value=7,\n", " min=0,\n", " max=10,\n", " step=1,\n", " description='Text:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### BoundedFloatText" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.BoundedFloatText(\n", " value=7.5,\n", " min=0,\n", " max=10.0,\n", " step=0.1,\n", " description='Text:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IntText" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.IntText(\n", " value=7,\n", " description='Any:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### FloatText" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.FloatText(\n", " value=7.5,\n", " description='Any:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Boolean widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are three widgets that are designed to display a boolean value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ToggleButton" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.ToggleButton(\n", " value=False,\n", " description='Click me',\n", " disabled=False,\n", " button_style='', # 'success', 'info', 'warning', 'danger' or ''\n", " tooltip='Description',\n", " icon='check'\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Checkbox" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Checkbox(\n", " value=False,\n", " description='Check me',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Valid\n", "\n", "The valid widget provides a read-only indicator." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Valid(\n", " value=False,\n", " description='Valid!',\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Selection widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several widgets that can be used to display single selection lists, and two that can be used to select multiple values. All inherit from the same base class. You can specify the **enumeration of selectable options by passing a list** (options are either (label, value) pairs, or simply values for which the labels are derived by calling `str`). You can **also specify the enumeration as a dictionary**, in which case the **keys will be used as the item displayed** in the list and the corresponding **value will be used** when an item is selected (in this case, since dictionaries are unordered, the displayed order of items in the widget is unspecified)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dropdown" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Dropdown(\n", " options=['1', '2', '3'],\n", " value='2',\n", " description='Number:',\n", " disabled=False,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following is also valid:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Dropdown(\n", " options={'One': 1, 'Two': 2, 'Three': 3},\n", " value=2,\n", " description='Number:',\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### RadioButtons" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.RadioButtons(\n", " options=['pepperoni', 'pineapple', 'anchovies'],\n", " # value='pineapple',\n", " description='Pizza topping:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Select" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Select(\n", " options=['Linux', 'Windows', 'OSX'],\n", " value='OSX',\n", " # rows=10,\n", " description='OS:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SelectionSlider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.SelectionSlider(\n", " options=['scrambled', 'sunny side up', 'poached', 'over easy'],\n", " value='sunny side up',\n", " description='I like my eggs ...',\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SelectionRangeSlider\n", "The value, index, and label keys are 2-tuples of the min and max values selected. The options must be nonempty." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datetime\n", "dates = [datetime.date(2015,i,1) for i in range(1,13)]\n", "options = [(i.strftime('%b'), i) for i in dates]\n", "widgets.SelectionRangeSlider(\n", " options=options,\n", " index=(0,11),\n", " description='Months (2015)',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### ToggleButtons" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.ToggleButtons(\n", " options=['Slow', 'Regular', 'Fast'],\n", " description='Speed:',\n", " disabled=False,\n", " button_style='', # 'success', 'info', 'warning', 'danger' or ''\n", " tooltips=['Description of slow', 'Description of regular', 'Description of fast'],\n", " # icons=['check'] * 3\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SelectMultiple\n", "Multiple values can be selected with shift and/or ctrl (or command) pressed and mouse clicks or arrow keys." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.SelectMultiple(\n", " options=['Apples', 'Oranges', 'Pears'],\n", " value=['Oranges'],\n", " # rows=10,\n", " description='Fruits',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## String widgets\n", "There are several widgets that can be used to display a string value. The `Text` and `Textarea` widgets accept input. The `HTML` and `HTMLMath` widgets display a string as HTML (`HTMLMath` also renders math). The `Label` widget can be used to construct a custom control label." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Text" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Text(\n", " value='Hello World',\n", " placeholder='Type something',\n", " description='String:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Textarea" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Textarea(\n", " value='Hello World',\n", " placeholder='Type something',\n", " description='String:',\n", " disabled=False\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Label\n", "The `Label` widget is useful if you need to build a custom description next to a control using similar styling to the built-in control descriptions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.HBox([widgets.Label(value=\"The $m$ in $E=mc^2$:\"), widgets.FloatSlider()])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.HTML(\n", " value=\"Hello World\",\n", " placeholder='Some HTML',\n", " description='Some HTML',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HTML Math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.HTMLMath(\n", " value=r\"Some math and HTML: \\(x^2\\) and $$\\frac{x+1}{x-1}$$\",\n", " placeholder='Some HTML',\n", " description='Some HTML',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file = open(\"images/WidgetArch.png\", \"rb\")\n", "image = file.read()\n", "widgets.Image(\n", " value=image,\n", " format='png',\n", " width=300,\n", " height=400,\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Button" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widgets.Button(\n", " description='Click me',\n", " disabled=False,\n", " button_style='', # 'success', 'info', 'warning', 'danger' or ''\n", " tooltip='Click me',\n", " icon='check'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusion\n", "\n", "Even more widgets are described in the notebook **Widget List - Advanced**. Use these as a future reference for yourself!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }