Commit 414cd64f authored by David Davó's avatar David Davó

Added widget list notebook

parent 3cca0604
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Output Widget.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Widget List"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Numeric widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are many widgets distributed with ipywidgets 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": {},
"source": [
"### IntSlider \n",
"- The slider is displayed with a specified, initial `value`. Lower and upper bounds are defined by `min` and `max`, and the value can be incremented according to the `step` parameter.\n",
"- The slider's label is defined by `description` parameter \n",
"- The slider's `orientation` is either 'horizontal' (default) or 'vertical'\n",
"- `readout` displays the current value of the slider next to it. The options are **True** (default) or **False** \n",
" - `readout_format` specifies the format function used to represent slider value. The default is '.2f'\n",
" "
]
},
{
"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": [
"An example of sliders **displayed vertically**."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "edeb5d23254545089e099fd8d34c1c6c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FloatSlider(value=7.5, continuous_update=False, description='Test:', max=10.0, orientation='vertical', readout…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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": [
"### FloatLogSlider"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `FloatLogSlider` has a log scale, which makes it easy to have a slider that covers a wide range of positive magnitudes. The `min` and `max` refer to the minimum and maximum exponents of the `base`, and the `value` refers to the actual value of the slider."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.FloatLogSlider(\n",
" value=10,\n",
" base=10,\n",
" min=-10, # max exponent of base\n",
" max=10, # min exponent of base\n",
" step=0.2, # exponent step\n",
" description='Log Slider'\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",
" description='Loading:',\n",
" bar_style='', # 'success', 'info', 'warning', 'danger' or ''\n",
" style={'bar_color': 'maroon'},\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",
" description='Loading:',\n",
" bar_style='info',\n",
" style={'bar_color': '#ffff00'},\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.\n",
"\n",
"### 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' # (FontAwesome names without the `fa-` prefix)\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Checkbox \n",
"- `value` specifies the value of the checkbox\n",
"- `indent` parameter places an indented checkbox, aligned with other controls. Options are **True** (default) or **False** \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.Checkbox(\n",
" value=False,\n",
" description='Check me',\n",
" disabled=False,\n",
" indent=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`).\n",
"\n",
"<div class=\"alert alert-info\">\n",
"Changes in *ipywidgets 8*:\n",
" \n",
"Selection widgets no longer accept a dictionary of options. Pass a list of key-value pairs instead.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Dropdown"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "88f7ef21d1a64a6789642a64330ed396",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Dropdown(description='Number:', index=1, options=('1', '2', '3'), value='2')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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, displaying the words `'One', 'Two', 'Three'` as the dropdown choices but returning the values `1, 2, 3`."
]
},
{
"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', # Defaults to 'pineapple'\n",
"# layout={'width': 'max-content'}, # If the items' names are long\n",
" description='Pizza topping:',\n",
" disabled=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### With dynamic layout and very long labels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.Box(\n",
" [\n",
" widgets.Label(value='Pizza topping with a very long label:'), \n",
" widgets.RadioButtons(\n",
" options=[\n",
" 'pepperoni', \n",
" 'pineapple', \n",
" 'anchovies', \n",
" 'and the long name that will fit fine and the long name that will fit fine and the long name that will fit fine '\n",
" ],\n",
" layout={'width': 'max-content'}\n",
" )\n",
" ]\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",
"\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 <kbd>shift</kbd> and/or <kbd>ctrl</kbd> (or <kbd>command</kbd>) 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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are several widgets that can be used to display a string value. The `Text`, `Textarea`, and `Combobox` 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": {},
"source": [
"### Combobox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.Combobox(\n",
" # value='John',\n",
" placeholder='Choose Someone',\n",
" options=['Paul', 'John', 'George', 'Ringo'],\n",
" description='Combobox:',\n",
" ensure_option=True,\n",
" disabled=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Password\n",
"\n",
"The `Password` widget hides user input on the screen. **This widget is not a secure way to collect sensitive information because:**\n",
"\n",
"+ The contents of the `Password` widget are transmitted unencrypted.\n",
"+ If the widget state is saved in the notebook the contents of the `Password` widget is stored as plain text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.Password(\n",
" value='password',\n",
" placeholder='Enter password',\n",
" description='Password:',\n",
" disabled=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Label\n",
"\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()])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### HTML"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.HTML(\n",
" value=\"Hello <b>World</b>\",\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 <i>HTML</i>: \\(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": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9ead3a1e39f342c6bc6c3f8f5eedc4be",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Image(value=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x00\\xe1\\x00\\x00\\x01A\\x08\\x00\\x00\\x00\\x00\\xa4\\xb3\\x97…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"file = open(\"/tmp/haskell.png\", \"rb\")\n",
"image = file.read()\n",
"widgets.Image(\n",
" value=image,\n",
" format='png',\n",
" width=300,\n",
" height=400,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9df9ac04b4a64495b9ae6dfa5dfbe0e5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Image(value=b'https://imgs.xkcd.com/comics/haskell.png', format='url', height='400', width='300')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"im = widgets.Image(\n",
" value=\"https://imgs.xkcd.com/comics/haskell.png\".encode(),\n",
" format=\"url\",\n",
" width=300,\n",
" height=400,\n",
")\n",
"\n",
"im"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"im.value=\"https://imgs.xkcd.com/comics/haskell.png\".encode()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Button"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"button = widgets.Button(\n",
" description='Click me',\n",
" disabled=False,\n",
" button_style='', # 'success', 'info', 'warning', 'danger' or ''\n",
" tooltip='Click me',\n",
" icon='check' # (FontAwesome names without the `fa-` prefix)\n",
")\n",
"button"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `icon` attribute can be used to define an icon; see the [fontawesome](https://fontawesome.com/icons) page for available icons. \n",
"A callback function `foo` can be registered using `button.on_click(foo)`. The function `foo` will be called when the button is clicked with the button instance as its single argument."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Output\n",
"\n",
"The `Output` widget can capture and display stdout, stderr and [rich output generated by IPython](http://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#module-IPython.display). For detailed documentation, see the [output widget examples](https://ipywidgets.readthedocs.io/en/latest/examples/Output Widget.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Play (Animation) widget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Play` widget is useful to perform animations by iterating on a sequence of integers with a certain speed. The value of the slider below is linked to the player."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"play = widgets.Play(\n",
" value=50,\n",
" min=0,\n",
" max=100,\n",
" step=1,\n",
" interval=500,\n",
" description=\"Press play\",\n",
" disabled=False\n",
")\n",
"slider = widgets.IntSlider()\n",
"widgets.jslink((play, 'value'), (slider, 'value'))\n",
"widgets.HBox([play, slider])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Date picker\n",
"\n",
"The date picker widget works in Chrome, Firefox and IE Edge, but does not currently work in Safari because it does not support the HTML date input field."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.DatePicker(\n",
" description='Pick a Date',\n",
" disabled=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Color picker"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.ColorPicker(\n",
" concise=False,\n",
" description='Pick a color',\n",
" value='blue',\n",
" disabled=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## File Upload\n",
"\n",
"The `FileUpload` allows to upload any type of file(s) into memory in the kernel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.FileUpload(\n",
" accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
" multiple=False # True to accept multiple files upload else False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The upload widget exposes a `value` attribute that contains the files uploaded. The value attribute is a tuple with a dictionary for each uploaded file. For instance:\n",
"\n",
"```python\n",
"uploader = widgets.FileUpload()\n",
"display(uploader)\n",
"\n",
"# upload something...\n",
"\n",
"# once a file is uploaded, use the `.value` attribute to retrieve the content:\n",
"uploader.value\n",
"#=> (\n",
"#=> {\n",
"#=> 'name': 'example.txt',\n",
"#=> 'type': 'text/plain',\n",
"#=> 'size': 36,\n",
"#=> 'last_modified': datetime.datetime(2020, 1, 9, 15, 58, 43, 321000, tzinfo=datetime.timezone.utc), \n",
"#=> 'content': <memory at 0x10c1b37c8>\n",
"#=> },\n",
"#=> )\n",
"```\n",
"\n",
"Entries in the dictionary can be accessed either as items, as one would any dictionary, or as attributes:\n",
"\n",
"```\n",
"uploaded_file = uploader.value[0]\n",
"uploaded_file[\"size\"]\n",
"#=> 36\n",
"uploaded_file.size\n",
"#=> 36\n",
"```\n",
"\n",
"The contents of the file uploaded are in the value of the `content` key. They are a [memory view](https://docs.python.org/3/library/stdtypes.html#memory-views):\n",
"\n",
"```python\n",
"uploaded_file.content\n",
"#=> <memory at 0x10c1b37c8>\n",
"```\n",
"\n",
"You can extract the content to bytes:\n",
"\n",
"```python\n",
"uploaded_file.content.tobytes()\n",
"#=> b'This is the content of example.txt.\\n'\n",
"```\n",
"\n",
"If the file is a text file, you can get the contents as a string by [decoding it](https://docs.python.org/3/library/codecs.html):\n",
"\n",
"```python\n",
"import codecs\n",
"codecs.decode(uploaded_file.content, encoding=\"utf-8\")\n",
"#=> 'This is the content of example.txt.\\n'\n",
"```\n",
"\n",
"You can save the uploaded file to the filesystem from the kernel:\n",
"\n",
"```python\n",
"with open(\"./saved-output.txt\", \"wb\") as fp:\n",
" fp.write(uploaded_file.content)\n",
"```\n",
"\n",
"To convert the uploaded file into a Pandas dataframe, you can use a [BytesIO object](https://docs.python.org/3/library/io.html#binary-i-o):\n",
"\n",
"```python\n",
"import io\n",
"import pandas as pd\n",
"pd.read_csv(io.BytesIO(uploaded_file.content))\n",
"```\n",
"\n",
"If the uploaded file is an image, you can visualize it with an [image](#Image) widget:\n",
"\n",
"```python\n",
"widgets.Image(value=uploaded_file.content.tobytes())\n",
"```\n",
"\n",
"<div class=\"alert alert-info\">\n",
"Changes in *ipywidgets 8*:\n",
" \n",
"The `FileUpload` changed significantly in ipywidgets 8:\n",
" \n",
"- The `.value` traitlet is now a list of dictionaries, rather than a dictionary mapping the uploaded name to the content. To retrieve the original form, use `{f[\"name\"]: f.content.tobytes() for f in uploader.value}`.\n",
"- The `.data` traitlet has been removed. To retrieve it, use `[f.content.tobytes() for f in uploader.value]`.\n",
"- The `.metadata` traitlet has been removed. To retrieve it, use `[{k: v for k, v in f.items() if k != \"content\"} for f in w.value]`.\n",
"</div>\n",
"\n",
"<div class=\"alert alert-warning\">\n",
"Warning: When using the `FileUpload` Widget, uploaded file content might be saved in the notebook if widget state is saved.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Controller\n",
"\n",
"The `Controller` allows a game controller to be used as an input device."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"widgets.Controller(\n",
" index=0,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Container/Layout widgets\n",
"\n",
"These widgets are used to hold other widgets, called children. Each has a `children` property that may be set either when the widget is created or later."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Box"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"items = [widgets.Label(str(i)) for i in range(4)]\n",
"widgets.Box(items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### HBox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"items = [widgets.Label(str(i)) for i in range(4)]\n",
"widgets.HBox(items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### VBox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"items = [widgets.Label(str(i)) for i in range(4)]\n",
"left_box = widgets.VBox([items[0], items[1]])\n",
"right_box = widgets.VBox([items[2], items[3]])\n",
"widgets.HBox([left_box, right_box])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GridBox\n",
"\n",
"This box uses the HTML Grid specification to lay out its children in two dimensional grid. The example below lays out the 8 items inside in 3 columns and as many rows as needed to accommodate the items."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"items = [widgets.Label(str(i)) for i in range(8)]\n",
"widgets.GridBox(items, layout=widgets.Layout(grid_template_columns=\"repeat(3, 100px)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accordion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()], titles=('Slider', 'Text'))\n",
"accordion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tabs\n",
"\n",
"In this example the children are set after the tab is created. Titles for the tabs are set in the same way they are for `Accordion`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']\n",
"children = [widgets.Text(description=name) for name in tab_contents]\n",
"tab = widgets.Tab()\n",
"tab.children = children\n",
"tab.titles = [str(i) for i in range(len(children))]\n",
"tab"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stacked\n",
"\n",
"The `Stacked` widget can have multiple children widgets as for `Tab` and `Accordion`, but only shows one at a time depending on the value of ``selected_index``:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"button = widgets.Button(description='Click here')\n",
"slider = widgets.IntSlider()\n",
"stacked = widgets.Stacked([button, slider])\n",
"stacked # will show only the button"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This can be used in combination with another selection-based widget to show different widgets depending on the selection:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dropdown = widgets.Dropdown(options=['button', 'slider'])\n",
"widgets.jslink((dropdown, 'index'), (stacked, 'selected_index'))\n",
"widgets.VBox([dropdown, stacked])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accordion, Tab, and Stacked use `selected_index`, not value\n",
"\n",
"Unlike the rest of the widgets discussed earlier, the container widgets `Accordion` and `Tab` update their `selected_index` attribute when the user changes which accordion or tab is selected. That means that you can both see what the user is doing *and* programmatically set what the user sees by setting the value of `selected_index`.\n",
"\n",
"Setting `selected_index = None` closes all of the accordions or deselects all tabs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the cells below try displaying or setting the `selected_index` of the `tab` and/or `accordion`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tab.selected_index = 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"accordion.selected_index = None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nesting tabs and accordions\n",
"\n",
"Tabs and accordions can be nested as deeply as you want. If you have a few minutes, try nesting a few accordions or putting an accordion inside a tab or a tab inside an accordion. \n",
"\n",
"The example below makes a couple of tabs with an accordion children in one of them"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tab_nest = widgets.Tab()\n",
"tab_nest.children = [accordion, accordion]\n",
"tab_nest.titles = ('An accordion', 'Copy of the accordion')\n",
"tab_nest"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbsphinx": "hidden"
},
"source": [
"[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Output Widget.ipynb)"
]
}
],
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
......@@ -5,7 +5,8 @@
"id": "b11ef99e-b811-4215-a1ee-aa161cf901f9",
"metadata": {},
"source": [
"# Widget List"
"# Widget List\n",
"This is a translation from IPython's [Widget List](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html) to IHaskell"
]
},
{
......@@ -113,27 +114,9 @@
],
"source": [
"{-# LANGUAGE OverloadedStrings #-}\n",
"import IHaskell.Display.Widgets"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "380ba438-5bf2-4fac-a529-054156100fe8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"3 + 2"
"import Data.Text\n",
"import IHaskell.Display.Widgets\n",
"import IHaskell.Display.Widgets.Layout as L"
]
},
{
......@@ -161,10 +144,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "d2954629-2d81-4817-b366-bca2e62d8d6e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0ca75bfe-dccf-4057-aee6-fc3ad864fa50",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"intSlider <- mkIntSlider\n",
"setField intSlider IntValue 7\n",
......@@ -191,14 +186,14 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 3,
"id": "6a96cd33-8007-42b1-b618-0b96bb8bbe31",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "db3f2fff-e7ca-43c2-bc2f-d66adc3ae541",
"model_id": "17e6dcf7-d111-4234-a79c-b0021cf6a4ba",
"version_major": 2,
"version_minor": 0
}
......@@ -233,14 +228,14 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 4,
"id": "f09bec94-8bb3-45ce-99fe-a3bcb48852ec",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "89dc421c-8035-44dc-a8e6-ebf101b84b25",
"model_id": "75086202-81cf-47b3-bb11-e6079a2dc1c4",
"version_major": 2,
"version_minor": 0
}
......@@ -271,14 +266,14 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 5,
"id": "7ba73761-5487-44c7-a8c2-5d315a33fa6a",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "458324e9-42b3-4465-8ec8-9b8a4ef5b26c",
"model_id": "7cf9eade-699d-4c63-abdd-bcdbac4d02a1",
"version_major": 2,
"version_minor": 0
}
......@@ -311,14 +306,14 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 6,
"id": "1098100a-c2f8-4883-a8ce-3a03560447bb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "38a81ed3-b631-43bc-9879-3487f7cf2f5b",
"model_id": "35bd1e0c-c8df-4f78-8af7-eab38918af83",
"version_major": 2,
"version_minor": 0
}
......@@ -357,14 +352,14 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 7,
"id": "1f47e516-a0df-4365-9093-a9649adbcfbb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dccff154-61d3-4fe5-a671-4224ff68f913",
"model_id": "32742773-cba9-473d-afe4-90f60382a051",
"version_major": 2,
"version_minor": 0
}
......@@ -379,30 +374,1348 @@
"setField intProgress MinInt 0\n",
"setField intProgress MaxInt 10\n",
"setField intProgress Description \"Now loading\"\n",
"setField intProgress BarStyle DefaultBar\n",
"setField intProgress BarStyle InfoBar\n",
"intProgress"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 8,
"id": "eef18bbb-c96b-47f1-bdab-a12b0c66b865",
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"header": "MessageHeader {mhIdentifiers = [\"70a6e091-e992-4fb5-819f-0f726b662d5b\"], mhParentHeader = Just (MessageHeader {mhIdentifiers = [\"70a6e091-e992-4fb5-819f-0f726b662d5b\"], mhParentHeader = Nothing, mhMetadata = Metadata (fromList [(\"recordTiming\",Bool False),(\"deletedCells\",Array []),(\"cellId\",String \"eef18bbb-c96b-47f1-bdab-a12b0c66b865\")]), mhMessageId = UUID {uuidToString = \"057f3036-687c-49e1-93d5-7a0c06d5adee\"}, mhSessionId = UUID {uuidToString = \"70a6e091-e992-4fb5-819f-0f726b662d5b\"}, mhUsername = \"\", mhMsgType = ExecuteRequestMessage, mhBuffers = []}), mhMetadata = Metadata (fromList []), mhMessageId = UUID {uuidToString = \"749ddfdc-d48e-4478-a40a-918b74691e2d\"}, mhSessionId = UUID {uuidToString = \"70a6e091-e992-4fb5-819f-0f726b662d5b\"}, mhUsername = \"\", mhMsgType = ExecuteErrorMessage, mhBuffers = []}",
"output_type": "error",
"traceback": [
"<interactive>:1:2: error: Not in scope: data constructor ‘StyleWidget’"
]
"data": {
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"s <- mkProgressStyle\n",
"setField s BarColor $ Just \"#ffff00\"\n",
"setField intProgress Style (StyleWidget s)"
]
},
{
"cell_type": "markdown",
"id": "b47fbb20-5c0a-4124-84fd-d06f2a10b635",
"metadata": {},
"source": [
"If a numerical text box imposes some kind of limit on the input (range, non-float, etc), that restriction is checked when the user presses enter or changes focus\n",
"### BoundedIntText"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3c09cc52-5187-4698-96f6-1c12729a7cfe",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8027bfef-13a9-4e79-8b9e-668a409cbc7e",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"boundedIntText <- mkBoundedIntText\n",
"setField boundedIntText IntValue 7\n",
"setField boundedIntText MinInt 0\n",
"setField boundedIntText MaxInt 10\n",
"setField boundedIntText StepInt $ Just 1\n",
"setField boundedIntText Description \"Text: \"\n",
"setField boundedIntText Disabled False\n",
"boundedIntText"
]
},
{
"cell_type": "markdown",
"id": "83bf9e9c-4d9b-4682-a94d-018e765bd7a8",
"metadata": {},
"source": [
"### BoundedFloatText"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "652ece64-05e4-484b-af80-4971ea5fa27c",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f3aeb885-83ae-4958-8cdb-a6654631eb8f",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"boundedFloatText <- mkBoundedFloatText\n",
"setField boundedFloatText FloatValue 7.5\n",
"setField boundedFloatText MinFloat 0.0\n",
"setField boundedFloatText MaxFloat 10.0\n",
"setField boundedFloatText StepFloat $ Just 0.1\n",
"setField boundedFloatText Description \"Text: \"\n",
"setField boundedFloatText Disabled False\n",
"boundedFloatText"
]
},
{
"cell_type": "markdown",
"id": "8e1b04bd-9dfa-44f4-9d68-8b0617fa797c",
"metadata": {},
"source": [
"### IntText"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9d306ca8-6abb-4644-98d8-38a11d4b5bad",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f4c82ca8-8fe4-4b3d-aef9-c68309d1e4d0",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"intText <- mkIntText\n",
"setField intText IntValue 7\n",
"setField intText Description \"Any:\"\n",
"setField intText Disabled False\n",
"intText"
]
},
{
"cell_type": "markdown",
"id": "b55e67cd-8e88-4ee2-b9cf-bd31b9d7ea26",
"metadata": {},
"source": [
"### FloatText"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "811aab8a-c6ee-41d6-b9a9-99a4d0924a93",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b3da1120-ee80-4b08-bdea-d60942e4dd77",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"floatText <- mkFloatText\n",
"setField floatText FloatValue 7.5\n",
"setField floatText Description \"Any:\"\n",
"setField floatText Disabled False\n",
"floatText"
]
},
{
"cell_type": "markdown",
"id": "6ba33cd5-b40c-4f47-ae0f-935c6b2471f5",
"metadata": {},
"source": [
"https://twitter.com/Jose6o/status/1425485091824939012/photo/1## Boolean Widgets\n",
"The following three widgets display a boolean value\n",
"\n",
"### ToggleButton"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7be1e91b-f28e-4be3-83cb-ac0954a87db8",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2a5b4e90-a76b-4c75-8672-568a01d72b75",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"toggleButton <- mkToggleButton\n",
"setField toggleButton BoolValue False\n",
"setField toggleButton Description \"Click me\"\n",
"setField toggleButton Disabled False\n",
"-- DefaultButton | PrimaryButton | SuccessButton | INfoButton | WarningButton | DangerButton\n",
"setField toggleButton ButtonStyle DefaultButton\n",
"setField toggleButton Tooltip $ Just \"Description\"\n",
"setField toggleButton Icon \"check\"\n",
"toggleButton"
]
},
{
"cell_type": "markdown",
"id": "b1e9442f-52a9-4bb9-b6ae-1e593ae1f451",
"metadata": {},
"source": [
"### Checkbox"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0514c055-060d-493d-aa91-55dc00cb0d5f",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c4ae482b-41d2-4e9d-8c68-f11b25184694",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"checkBox <- mkCheckBox\n",
"setField checkBox BoolValue False\n",
"setField checkBox Description \"Check me out!\"\n",
"setField checkBox Disabled False\n",
"setField checkBox Indent False\n",
"checkBox"
]
},
{
"cell_type": "markdown",
"id": "fcedc017-cab4-4ccf-a097-8cce0d049e85",
"metadata": {},
"source": [
"### Valid\n",
"It provides a read-only indicator."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "606594cb-08a3-41a2-b060-9d2d0ef3f588",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "91091dcc-5bd8-49c8-bc8e-c5e425611c15",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"valid <- mkValid\n",
"setField valid BoolValue False\n",
"setField valid Description \"Valid?\"\n",
"valid"
]
},
{
"cell_type": "markdown",
"id": "6c1111c3-3470-44f4-8782-c68093e54ed7",
"metadata": {},
"source": [
"## Selection widgets\n",
"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 of options labels.\n",
"\n",
"The selected index is specified with the field `OptionalIndex` in case it can be `Nothing` and `Index` if it's not a Maybe.\n",
"\n",
"### Dropdown"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "9472cea4-69f1-409c-8244-1e79a5f56050",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "14b017da-6ed0-43e5-8050-356315f3cb22",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"(StyleWidget s) <- getField intProgress Style\n",
":t s\n",
"setField s "
"dropdown <- mkDropdown\n",
"setField dropdown OptionsLabels [\"1\", \"2\", \"3\"]\n",
"setField dropdown OptionalIndex $ Just 2\n",
"setField dropdown Description \"Number:\"\n",
"setField dropdown Disabled False\n",
"dropdown"
]
},
{
"cell_type": "markdown",
"id": "a28e4444-a57e-47da-ad6b-0156754c2e87",
"metadata": {},
"source": [
"### RadioButtons"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "013b11dd-f193-4011-9539-ce177c2df0e0",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d634bbbc-f3ef-40d9-b32c-91e94bb10862",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"radioButtons <- mkRadioButtons\n",
"setField radioButtons OptionsLabels [\"pepperoni\", \"pineapple\", \"anchovies\"]\n",
"setField radioButtons OptionalIndex Nothing\n",
"setField radioButtons Description \"Topping:\"\n",
"setField radioButtons Disabled False\n",
"radioButtons"
]
},
{
"cell_type": "markdown",
"id": "4850f444-e7ae-49df-9655-d528efbe4de1",
"metadata": {},
"source": [
"Here is an exemple with dynamic layout and very long labels"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "abd06526-4838-4d2e-b191-136fad9e10f5",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6495b321-d08c-4d59-9c7c-4c5fc0b3ecf5",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"radioButtons' <- mkRadioButtons\n",
"setField radioButtons' OptionsLabels [\n",
" \"pepperoni\",\n",
" \"pineapple\",\n",
" \"anchovies\",\n",
" \"Spam, sausage, Spam, Spam, Spam, bacon, Spam, tomato and Spam\"\n",
" ]\n",
"label <- mkLabel\n",
"setField label StringValue \"Pizza topping with a very long label\"\n",
"\n",
"layout <- mkLayout\n",
"setField layout L.Width $ Just \"max-content\"\n",
"\n",
"box <- mkBox\n",
"setField box Children [ChildWidget label, ChildWidget radioButtons']\n",
"setField box Layout layout\n",
"box"
]
},
{
"cell_type": "markdown",
"id": "43f0cf55-6a16-4df8-bc41-e872cd580ed6",
"metadata": {},
"source": [
"### Select"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "bcd0e446-cb09-49a9-b41c-08cf81094bfc",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ad26f8fa-56a9-47d3-8e01-776a6578b107",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"select <- mkSelect\n",
"setField select OptionsLabels [\"Linux\", \"Windows\", \"OSX\"]\n",
"setField select OptionalIndex $ Just 0\n",
"setField select Description \"OS:\"\n",
"setField select Disabled False\n",
"select"
]
},
{
"cell_type": "markdown",
"id": "925a3f0a-e244-44dc-a132-00d020409461",
"metadata": {},
"source": [
"### SelectionSlider"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "4a59fe64-0b50-49d8-897b-b96a56bc5a43",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "216ef633-6e15-42b7-9e7f-3ed9c4fc542b",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"selectionSlider <- mkSelectionSlider\n",
"setField selectionSlider OptionsLabels [\"Scrambled\", \"Sunny side up\", \"Poached\", \"Over easy\"]\n",
"setField selectionSlider Index 1\n",
"setField selectionSlider Description \"I like my eggs...\"\n",
"setField selectionSlider Disabled False\n",
"setField selectionSlider ContinuousUpdate False\n",
"setField selectionSlider Orientation HorizontalOrientation\n",
"setField selectionSlider ReadOut True\n",
"selectionSlider"
]
},
{
"cell_type": "markdown",
"id": "90248741-f933-4232-a57f-facd527183d3",
"metadata": {},
"source": [
"### SelectionRangeSlider"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "d8fa0893-4614-4f28-94f6-b5504a063a42",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6554880c-8c78-482e-97e6-815a6cda050c",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"selectionRangeSlider <- mkSelectionRangeSlider\n",
"setField selectionRangeSlider OptionsLabels [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n",
"setField selectionRangeSlider Indices [0,4]\n",
"setField selectionRangeSlider Description \"When is the shop open?\"\n",
"setField selectionRangeSlider Disabled False\n",
"selectionRangeSlider"
]
},
{
"cell_type": "markdown",
"id": "3fef32ea-011f-4d3c-a98e-fc25692dbe41",
"metadata": {},
"source": [
"### ToggleButtons"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "0423d92a-3d9b-4f82-aeed-c869617817b3",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "be181eb5-6318-4f6f-a3ef-1ecc108d92ab",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"toggleButtons <- mkToggleButtons\n",
"setField toggleButtons OptionsLabels [\"Slow\", \"Regular\", \"Fast\"]\n",
"setField toggleButtons Description \"Speed:\"\n",
"setField toggleButtons Disabled False\n",
"-- PrimaryButton | SuccessButton | InfoButton | WarningButton | DangerButton | DefaultButton\n",
"setField toggleButtons ButtonStyle DefaultButton\n",
"setField toggleButtons Tooltips [\"Description of slow\", \"Description of regular\", \"Description of fast\"]\n",
"toggleButtons"
]
},
{
"cell_type": "markdown",
"id": "121404fa-3461-448a-a782-14a41e467863",
"metadata": {},
"source": [
"### SelectMultiple\n",
"Multiple values can be selected with `shift` and/or `ctrl` (or `command` with OSX) pressed and mouse click or arrow keys"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "8332e274-bb0d-4be2-b9b4-a08c633f6a4b",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "33163396-cca1-425d-8a0d-206e8c2a6d0b",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"selectMultiple <- mkSelectMultiple\n",
"setField selectMultiple OptionsLabels [\"Apples\", \"Oranges\", \"Pears\"]\n",
"setField selectMultiple Indices [1]\n",
"setField selectMultiple Description \"Fruits\"\n",
"setField selectMultiple Disabled False\n",
"selectMultiple"
]
},
{
"cell_type": "markdown",
"id": "f571f5a5-a320-466f-a228-54d3ebb2d141",
"metadata": {},
"source": [
"## String widgets\n",
"There are multiple widgets that can be used to display a string value. The `Text`, `TextArea`\n",
"and `Combobox` widgets accept input. The `HTML` and `HTMLMath` display the received string\n",
"as HTML. The `Label` widget can be used to construct a custom control label, but it doesn't display\n",
"input.\n",
"\n",
"### Text"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "ce3ba3b3-cfea-41f8-ab61-a58b7db1d322",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8341efad-5770-48a2-90e8-98c30f2e5b75",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"text <- mkText\n",
"setField text StringValue \"Hello World!\"\n",
"setField text Placeholder \"Type something\"\n",
"setField text Description \"String:\"\n",
"setField text Disabled False\n",
"text"
]
},
{
"cell_type": "markdown",
"id": "7fbd9d92-8822-4512-907f-4e0d39f7b726",
"metadata": {},
"source": [
"### Textarea"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "0528b6ca-caac-4895-a071-6bb2d3a1838d",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1fb6e804-a04b-4df7-a46f-a25299c916a0",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"textarea <- mkTextArea\n",
"setField textarea StringValue \"Hello World!\"\n",
"setField textarea Placeholder \"Type something\"\n",
"setField textarea Description \"Long string:\"\n",
"setField textarea Disabled False\n",
"textarea"
]
},
{
"cell_type": "markdown",
"id": "6f54b8f3-b24e-4a06-b222-64ab6bd818e0",
"metadata": {},
"source": [
"### Combobox"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "6872c8b4-59e9-4eb6-9d32-9526f20940b2",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "82e2c989-5941-45b2-8067-7946c736365a",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"combobox <- mkCombobox\n",
"setField combobox Placeholder \"Choose Someone\"\n",
"setField combobox Options [\"Paul\", \"John\", \"George\", \"Ringo\"]\n",
"setField combobox Description \"Combobox:\"\n",
"setField combobox EnsureOption True\n",
"setField combobox Disabled False\n",
"combobox"
]
},
{
"cell_type": "markdown",
"id": "6920d129-ea7d-40bb-bfa1-e0432fe8cc93",
"metadata": {},
"source": [
"### Password\n",
"\n",
"The `Password` widget hides user input on the screen. Nevertheless, this widget is **not a secure way** to collect sensitive information **because**:\n",
"- The contents are transmitted unencrypted\n",
"- If you save the notebook, the contents are stored as plain text"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "56554ed4-9003-4af0-a407-9ccc6dbb60c5",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "aa365888-508e-4b83-9839-e4d8a961d07c",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"password <- mkPassword\n",
"setField password StringValue \"Password\"\n",
"setField password Placeholder \"Enter password\"\n",
"setField password Description \"Password:\"\n",
"setField password Disabled False\n",
"password"
]
},
{
"cell_type": "markdown",
"id": "666ad3b5-ae4a-4883-81ce-2e9c18daf3a1",
"metadata": {},
"source": [
"### Label\n",
"The `Label` widget is useful if you need to build a very customized description next to a control widget."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "b8c01e21-1438-47c5-ab9f-f70b08040f85",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b75249f3-4812-4a78-b0c4-7386f318eb7e",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"label <- mkLabel\n",
"setField label StringValue \"The $m$ in $E=mc^2$:\"\n",
"\n",
"floatSlider <- mkFloatSlider\n",
"\n",
"hbox <- mkHBox\n",
"setField hbox Children [ChildWidget label, ChildWidget floatSlider]\n",
"hbox"
]
},
{
"cell_type": "markdown",
"id": "a4107143-a81a-40f9-8f39-9da44ec1571d",
"metadata": {},
"source": [
"### HTML"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "42d32123-22a5-4322-a8e5-efe1d2bc3deb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ea71a9c1-69ee-4277-a877-66433316e0d7",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"html <- mkHTML\n",
"setField html StringValue \"Hello <b>World!</b>\"\n",
"setField html Placeholder \"Some HTML\"\n",
"setField html Description \"Some HTML\"\n",
"html"
]
},
{
"cell_type": "markdown",
"id": "92221dce-d246-4119-9c48-409477ad2743",
"metadata": {},
"source": [
"### HTML Math\n",
"Like HTML, but it also renders LaTeX math commands"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "7c277fa9-f8be-4f7b-bffe-26bc74298572",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7ccc4575-7d27-44b8-8d01-cc92ee72b951",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"htmlMath <- mkHTMLMath\n",
"-- Remember to escape the \\ with \\\\\n",
"setField htmlMath StringValue \"Some math and <i>HTML</i>: $x^2$ and $$\\\\frac{x+1}{x-1}$$\"\n",
"setField htmlMath Placeholder \"Some HTML\"\n",
"setField htmlMath Description \"Some HTML\"\n",
"htmlMath"
]
},
{
"cell_type": "markdown",
"id": "8060d219-fadc-4647-bd7e-7af4f5f8d614",
"metadata": {},
"source": [
"## Image"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "01eff752-f657-4a11-ad1f-8daa4bff927e",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "484ce260-8160-4e37-9fa5-87ce1be3d63f",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"image <- mkImage\n",
"setField image BSValue \"https://imgs.xkcd.com/comics/haskell.png\"\n",
"-- PNG | SVG | JPG | IURL\n",
"setField image ImageFormat IURL\n",
"image"
]
},
{
"cell_type": "markdown",
"id": "fd20dcb7-5691-4b6e-b569-473624f9e6a1",
"metadata": {},
"source": [
"## Button"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "bf84c8f3-75b2-468c-a4f3-53358bd3241f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ViewModule ::: Text\n",
"ViewModuleVersion ::: Text\n",
"ModelModule ::: Text\n",
"ModelModuleVersion ::: Text\n",
"ModelName ::: Text\n",
"ViewName ::: Text\n",
"DOMClasses ::: [Text]\n",
"Tabbable ::: Maybe Bool\n",
"Tooltip ::: Maybe Text\n",
"Layout ::: IPythonWidget 'LayoutType\n",
"DisplayHandler ::: IO ()\n",
"Description ::: Text\n",
"Style ::: StyleWidget\n",
"Disabled ::: Bool\n",
"Icon ::: Text\n",
"ButtonStyle ::: ButtonStyleValue\n",
"ClickHandler ::: IO ()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f08d0a92-dc49-482d-b3b0-21f0e8586ecc",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"button <- mkButton\n",
"setField button Description \"Click me\"\n",
"setField button Disabled False\n",
"-- PrimaryButton | SuccessButton | InfoButton | WarningButton | DangerButton | DefaultButton\n",
"setField button ButtonStyle DefaultButton\n",
"setField button Tooltip $ Just \"Click me\"\n",
"setField button Icon \"mouse\"\n",
"properties button\n",
"button"
]
},
{
"cell_type": "markdown",
"id": "37f880eb-2c98-446b-a38f-582b302b6fc3",
"metadata": {},
"source": [
"The `Icon` attribute is used to define an icon; see the [fontawesome](https://fontawesome.com/v5.15/icons) page for available icons.\n",
"\n",
"You can set a callback function Setting the `ClickHandler ::: IO ()` attribute.\n",
"\n",
"## Output\n",
"The `Output` widget is complicated and has many features. You can see detailed documentation in its dedicated Notebook.\n",
"\n",
"## Play (Animation) widget\n",
"The `Play` widget is like an automated textbox, where someone is making click on increment every few miliseconds. Here you can see an example:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "acdc051c-4c72-4fda-a29e-286ec8aa86f9",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "72d9a858-33a8-4063-bcfc-0eaaa55cb59f",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "432e8db8-d084-495c-8497-dcbfbbc17250",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "21b4b703-6dfd-4040-ade0-5b8748ce3f7b",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"play <- mkPlay\n",
"setField play IntValue 50\n",
"setField play MinInt 0\n",
"setField play MaxInt 100\n",
"setField play StepInt $ Just 1\n",
"setField play Interval 500\n",
"setField play Description \"Press play\"\n",
"setField play Disabled False\n",
"\n",
"slider <- mkIntSlider\n",
"jslink (WidgetFieldPair play IntValue) (WidgetFieldPair slider IntValue)\n",
"\n",
"play\n",
"slider"
]
},
{
"cell_type": "markdown",
"id": "a2db1169-1fb5-4140-81eb-0537230036bc",
"metadata": {},
"source": [
"## Date Picker\n",
"This widget only works with browser that support the HTML date input field (Chrome, Firefox and IE Edge, but not Safari)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "1572fa8f-992a-4bb2-976a-26c7db3599c8",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9e580396-332d-4134-a96c-28e7b794e8a8",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"datePicker <- mkDatePicker\n",
"setField datePicker Description \"Pick a date\"\n",
"setField datePicker Disabled False\n",
"datePicker"
]
},
{
"cell_type": "markdown",
"id": "b9fdf795-abb1-4220-8b72-0aa641f032a3",
"metadata": {},
"source": [
"## Color picker"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "8d354b46-707d-4b67-add3-04c1e906ce31",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f4ea60a1-8f29-4d16-a923-4633eb70fb00",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colorPicker <- mkColorPicker\n",
"setField colorPicker Concise False\n",
"setField colorPicker Description \"Pick a color\"\n",
"setField colorPicker StringValue \"Blue\"\n",
"setField colorPicker Disabled False\n",
"colorPicker"
]
},
{
"cell_type": "markdown",
"id": "73b26165-d9a4-407a-81c7-1842675e27fc",
"metadata": {},
"source": [
"## Controller\n",
"The `Controller` allows a game controller to be used as an input device"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "5b204580-a4ab-4b48-b34e-2bcb5abae9ad",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3d86f13f-d3b1-4127-a35d-529e63f2f2d4",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"controller <- mkController\n",
"setField controller Index 0\n",
"controller"
]
},
{
"cell_type": "markdown",
"id": "3cd0edb8-9a47-4bc4-8ffc-a61e12fe66b5",
"metadata": {},
"source": [
"## Container and Layout widgets\n",
"\n",
"These widgets are used to hold other widgets, called children. They can display multiple widgets or change its CSS styling.\n",
"\n",
"### Box"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "262e9246-0ce1-43dc-9ec8-a5b621957355",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9c641e30-d752-4d7e-a303-da184025fd1f",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"labels <- flip mapM [1..4] $ \\i->do\n",
" l <- mkLabel\n",
" setField l StringValue $ pack $ (\"Label #\" ++ show i)\n",
" return $ ChildWidget l\n",
"\n",
"box <- mkBox\n",
"setField box Children labels\n",
"box"
]
},
{
"cell_type": "markdown",
"id": "1749af43-63b1-4ee7-aed5-db5a8667c473",
"metadata": {},
"source": [
"### HBox"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "db37b5ce-3154-4194-8241-a2b15e41fdcf",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6e1559d7-9ab0-49b0-b891-38593cccf255",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"hbox <- mkHBox\n",
"setField hbox Children labels\n",
"hbox"
]
},
{
"cell_type": "markdown",
"id": "6db2f1e2-dba0-42cc-8bf7-c883e0fdb939",
"metadata": {},
"source": [
"### VBox"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "97500421-d638-4123-88c0-0fda9cd307b2",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "55d359f8-f415-4aea-ac40-96803a1b6dbd",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"vbox <- mkVBox\n",
"setField vbox Children labels\n",
"vbox"
]
},
{
"cell_type": "markdown",
"id": "22e85f1a-8f07-443b-bd99-f17f52e78ea3",
"metadata": {},
"source": [
"### GridBox\n",
"\n",
"This box uses the HTML Grid specification to create a two-dimensional grid. To set its grid values, we need to create a layout widget. Let's do a 3x3 grid:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "5a922615-b529-41ef-9af3-346b909f18bb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "aa17ef27-62e5-4fcd-89c1-a389c478866b",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"labels <- flip mapM [1..8] $ \\i->do\n",
" l <- mkLabel\n",
" setField l StringValue $ pack $ (\"Label #\" ++ show i)\n",
" return $ ChildWidget l\n",
" \n",
"layout <- mkLayout\n",
"setField layout L.GridTemplateColumns $ Just \"repeat(3, 10em)\"\n",
" \n",
"gridBox <- mkGridBox\n",
"setField gridBox Children labels\n",
"setField gridBox Layout layout\n",
"gridBox"
]
},
{
"cell_type": "markdown",
"id": "269dcfce-ff9d-4c7a-85d1-6814dc813fd3",
"metadata": {},
"source": [
"### Accordion\n",
"\n",
"Unlike the other container widgets, `Accordion` and `Tab` update their `selected_index` attribute when a tab or accordion element is selected. You can see what the user is doing, or set what the user is seeing.\n",
"\n",
"You can set `selected_index` to `Nothing` to close all accordions or deselect all tabs."
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "6e0c7074-d20a-44ae-8ea9-ee7aeacf6f27",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e401cdf9-41f8-427c-9d93-96a3b2faa1c8",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"accordion <- mkAccordion\n",
"slider <- mkIntSlider\n",
"text <- mkText\n",
"setField accordion Children [ChildWidget slider, ChildWidget text]\n",
"setField accordion Titles [\"Slider\", \"Text\"]\n",
"accordion"
]
},
{
"cell_type": "markdown",
"id": "0701634d-d2be-451b-9bab-8826b0c90213",
"metadata": {},
"source": [
"### Tabs"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "f006c848-a5bb-4956-98c9-d68ec87ff703",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "192af7b2-3729-43f4-bedb-7699d7a0b208",
"version_major": 2,
"version_minor": 0
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tabs <- mkTab\n",
"\n",
"texts <- flip mapM [0..5] $ \\i->do\n",
" t <- mkText\n",
" setField t StringValue $ pack $ (\"P\" ++ show i)\n",
" return $ ChildWidget t\n",
"\n",
"setField tabs Children texts\n",
"setField tabs Titles [pack $ show i | i <- [0..5]]\n",
"setField tabs SelectedIndex $ Just 2\n",
"tabs"
]
}
],
......
......@@ -50,4 +50,36 @@ First you have to check if the attribute is only for one widget, or is from a co
> Some widgets receive messages from the frontend when a value is modified (such as sliders, text areas, buttons...). You'll have to modify the `comm` function instantiated from the class `IHaskellWidget`. You can find an example at [IntSlider.hs](./Int/BoundedInt/IntSlider.hs)
## FAQ
When using widgets in ihaskell, you'll encounter a lot of compilation errors. If you are not very familiar with Haskell, they can be a bit hard to decipher, this is a mini guide that will (hopefully) appear when you paste the error in Google.
\ No newline at end of file
When using widgets in ihaskell, you'll encounter a lot of compilation errors. If you are not very familiar with Haskell, they can be a bit hard to decipher, this is a mini guide that will (hopefully) appear when you paste the error in Google.
### setField: No instance for...
You probably got this error when trying to use setField like this:
```
<interactive>:1:1: error:
• No instance for (Data.Vinyl.Lens.RecElem
Data.Vinyl.Core.Rec
'ihaskell-widgets-0.3.0.0:IHaskell.Display.Widgets.Singletons.Index
'ihaskell-widgets-0.3.0.0:IHaskell.Display.Widgets.Singletons.Index
'[]
'[]
(Data.Vinyl.TypeLevel.RIndex 'ihaskell-widgets-0.3.0.0:IHaskell.Display.Widgets.Singletons.Index '[]))
arising from a use of ‘setField’
• In the expression: setField select Index 0
In an equation for ‘it’: it = setField select Index 0
```
What this error means is that there is no field called `Index` for this particular widget. You can display on screen all
the fields available for a widget using `properties widget`.
### setField: Couldn't match expected type SField f with actual type
If you get an error like this, you probably forgot to put the field name in the second argument of `setField`.
```
<interactive>:1:25: error:
• Couldn't match expected type ‘ihaskell-widgets-0.3.0.0:IHaskell.Display.Widgets.Singletons.SField f’ with actual type ‘[a0]’
• In the second argument of ‘setField’, namely ‘["Apples", "Oranges", "Pears"]’
In the expression: setField selectMultiple ["Apples", "Oranges", "Pears"]
In an equation for ‘it’: it = setField selectMultiple ["Apples", "Oranges", "Pears"]
• Relevant bindings include it :: ihaskell-widgets-0.3.0.0:IHaskell.Display.Widgets.Types.FieldType f -> IO () (bound at <interactive>:1:1)
```
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment