import addutils.toc ; addutils.toc.js(ipy_notebook=True)
from addutils import css_notebook
css_notebook()
You can explicitly display widgets using display()
:
import ipywidgets
from IPython.display import display
w1 = ipywidgets.IntSlider(border_radius=2, font_size=12, width=650,
border_color='blue', slider_color='red', description='Slider Widget:',
min=50, max=450, value=200)
display(w1)
Failed to display Jupyter Widget of type IntSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
The widget value can be read and set:
print ('Previous Value: ', w1.value)
print ('Widget will be reset to central position')
w1.value = (w1.min+w1.max)/2
Previous Value: 200 Widget will be reset to central position
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.
w1.keys
['_dom_classes', '_model_module', '_model_module_version', '_model_name', '_view_count', '_view_module', '_view_module_version', '_view_name', 'continuous_update', 'description', 'disabled', 'layout', 'max', 'min', 'orientation', 'readout', 'readout_format', 'step', 'style', 'value']
Also Check "Events"
To display the same value in two different ways, you can use the traitlet
link function to link two properties together from the server side:
from traitlets import link
w11 = ipywidgets.IntSlider(width=650, description='Slider Widget:', min=0, max=450)
w12 = ipywidgets.FloatText()
display(w11,w12)
mylink1 = link((w11, 'value'), (w12, 'value'))
Failed to display Jupyter Widget of type IntSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type FloatText
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
To Unlink just call the .unlink()
method on the link object:
mylink1.unlink()
When synchronizing traitlets attributes, you may experience a lag because of the latency due to the roundtrip to the server side. You can also directly link widget attributes in the browser using the link widgets, in either a unidirectional or a bidirectional fashion.
w13 = ipywidgets.IntSlider(width=650, description='Slider Widget:', min=0, max=450)
w14 = ipywidgets.FloatText()
display(w13,w14)
mylink2 = ipywidgets.jslink((w13, 'value'), (w14, 'value'))
Failed to display Jupyter Widget of type IntSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type FloatText
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
The interact
function (ipywidgets.interact
) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython's widgets.
At the most basic level, interact autogenerates UI controls for function arguments, and then calls the function with those arguments when you manipulate the controls interactively. To use interact, you need to define a function that you want to explore. Here is a function that prints its only argument x.
from ipywidgets import interact, interactive, fixed
def my_function(x):
return x
When you pass this function as the first argument to interact
along with an integer keyword argument (x=10), a slider is generated and bound to the function parameter.
interact(my_function, x=10);
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
If you pass True or False, interact will generate a checkbox.
interact(my_function, x=True);
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
If you pass a string, interact will generate a text area.
interact(my_function, x='Hello World');
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
interact
can also be used as a decorator. This allows you to define a function and interact with it in a single shot. As this example shows, interact
also works with functions that have multiple arguments.
@interact(x=True, y=1.0)
def g(x, y):
return (x, y)
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
When you want to explore a function using interact, fixing one or more of its arguments to specific values. This can be accomplished by wrapping values with the fixed function.
def my_function2(p, q):
return (p, q)
interact(my_function2, p=5, q=fixed(20));
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
interact
can be called with the synthax varible_name=(min, max, step)
. Floats or Integers can be passed.
@interact(x=(0.0,20.0,0.01))
def f(x=5.5):
return x
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
@interact(x=(5,10,1))
def f(x=5.5):
return x
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
To create a dropdown menu, pass a Tuple:
interact(my_function, x=['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday'])
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
<function __main__.my_function>
If you want a dropdown menu that passes non-string values to the Python function, you can pass a dictionary.
# Here we use an ordered dictionary to keep the right order in the days of the week
from collections import OrderedDict
d={'Monday':1, 'Tuesday':2, 'Wednesday':3, 'Thursday':4, 'Friday':5, 'Saturday':6, 'Sunday':7}
ordered_d = OrderedDict(sorted(d.items(), key=lambda t: t[1]))
interact(my_function, x=ordered_d);
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
In addition to interact
, IPython provides another function, interactive
, that is useful when you want to reuse the widgets that are produced or access the data that is bound to the UI controls. Unlike interact
, interactive
returns a Widget instance rather than immediately displaying the widget.
def f(a, b):
return a+b
w3 = interactive(f, a=(0.0,5.0,0.1), b=(10,100,5))
The interactive
container in this case has two children:
type(w3)
ipywidgets.widgets.interaction.interactive
w3.children
(FloatSlider(value=2.5, description='a', max=5.0), IntSlider(value=55, description='b', min=10, step=5), Output())
display(w3)
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
w3.kwargs
{'a': 2.5, 'b': 55}
w3.result
57.5
By replacing Float
with Int
in the widget name, you can find the Integer equivalent.
ipywidgets.FloatSlider(value=7.5, min=5.0, max=10.0, step=0.1, description='Test:')
Failed to display Jupyter Widget of type FloatSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.FloatSlider(value=7.5, min=5.0, max=10.0, step=0.1, description='Test:',
orientation='vertical')
Failed to display Jupyter Widget of type FloatSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.FloatProgress(value=9.5, min=5.0, max=10.0, step=0.1, description='Processing:')
Failed to display Jupyter Widget of type FloatProgress
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.BoundedFloatText(value=7.5, min=5.0, max=10.0, description='Bounded:')
Failed to display Jupyter Widget of type BoundedFloatText
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Button(description='DO NOT CLICK HERE !', tooltip='Ka-Boom', value=False,
border_color='black', color='white', background_color='red',
font_size=24, font_weight='bold')
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.ToggleButton(description='Toggle me', value=False, font_size=24)
Failed to display Jupyter Widget of type ToggleButton
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Checkbox(description='Check me', value=False)
Failed to display Jupyter Widget of type Checkbox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Valid(value=True)
Failed to display Jupyter Widget of type Valid
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
w4 = ipywidgets.Dropdown(options=['A', 'B', 'C'], value='B', description='Select:')
display(w4)
Failed to display Jupyter Widget of type Dropdown
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.RadioButtons(description='Distribute on:',
options=['DC01 - Alaska', 'DC02 - Island', 'DC03 - China'])
Failed to display Jupyter Widget of type RadioButtons
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Select(description='Distribute on:',
options=['DC01 - Alaska', 'DC02 - Island', 'DC03 - China'])
Failed to display Jupyter Widget of type Select
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.ToggleButtons(description='Distribute on:',
options=['DC01 - Alaska', 'DC02 - Island', 'DC03 - China'])
Failed to display Jupyter Widget of type ToggleButtons
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.SelectMultiple(description='Distribute on:',
options=['DC01 - Alaska', 'DC02 - Island', 'DC03 - China'])
Failed to display Jupyter Widget of type SelectMultiple
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Text(description='String:', value='Hello World')
Failed to display Jupyter Widget of type Text
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
ipywidgets.Textarea(description='String:', value='Hello World')
Failed to display Jupyter Widget of type Textarea
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
The widgets distributed with IPython can be styled by setting the following traits:
To display widget A inside widget B, widget A must be a child of widget B. Widgets that can contain other widgets have a children attribute. This attribute can be set via a keyword argument in the widget's constructor or after construction. Calling display on an object with children automatically displays the children.
float_range = ipywidgets.FloatSlider()
string = ipywidgets.Text(value='hi')
container = ipywidgets.Box(children=[float_range, string])
container.border_color = 'red'
container.border_style = 'dotted'
container.border_width = 3
container.height = 80
container.width = 400
display(container) # Displays the `container` and all of it's children.
Failed to display Jupyter Widget of type Box
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
If you need to display a more complicated set of widgets, there are specialized containers that you can use. To display multiple sets of widgets, you can use an Accordion or a Tab in combination with one Box per set of widgets (as seen below). The "pages" of these widgets are their children. To set the titles of the pages, use set_title.
name1 = ipywidgets.Text(description='Location:')
zip1 = ipywidgets.BoundedIntText(description='Zip:', min=0, max=99999)
page1 = ipywidgets.Box(children=[name1, zip1])
name2 = ipywidgets.Text(description='Location:')
zip2 = ipywidgets.BoundedIntText(description='Zip:', min=0, max=99999)
page2 = ipywidgets.Box(children=[name2, zip2])
accord = ipywidgets.Accordion(children=[page1, page2], width=400)
display(accord)
accord.set_title(0, 'From')
accord.set_title(1, 'To')
Failed to display Jupyter Widget of type Accordion
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
name = ipywidgets.Text(description='Name:', padding=4)
color = ipywidgets.Dropdown(description='Color:', padding=4, options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
page1 = ipywidgets.Box(children=[name, color], padding=4)
age = ipywidgets.IntSlider(description='Age:', padding=4, min=0, max=120, value=50)
gender = ipywidgets.RadioButtons(description='Gender:', padding=4, options=['male', 'female'])
page2 = ipywidgets.Box(children=[age, gender], padding=4)
tabs = ipywidgets.Tab(children=[page1, page2])
display(tabs)
tabs.set_title(0, 'Name')
tabs.set_title(1, 'Details')
tabs.background_color='DarkSlateGray'
Failed to display Jupyter Widget of type Tab
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Widgets display vertically by default:
b1 = ipywidgets.Button(description='One')
b2 = ipywidgets.Button(description='Two')
b3 = ipywidgets.Button(description='Three')
b4 = ipywidgets.Button(description='Four')
display(b1, b2, b3, b4)
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
To make widgets display horizontally, they can be children of an HBox widget:
ch = ipywidgets.HBox(children=[b1, b2, b3, b4])
display(ch)
Failed to display Jupyter Widget of type HBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
The visible property of widgets can be used to hide or show widgets that have already been displayed (as seen below). The visible property can be:
b3.visible=None
.on_click
and .on_submit
events¶The Button
is not used to represent a data type. Instead the button widget is used to handle mouse clicks. The .on_click
method of the Button
can be used to register function to be called when the button is clicked. The doc string of the .on_click
can be seen below:
print(ipywidgets.Button.on_click.__doc__)
Register a callback to execute when the button is clicked. The callback will be called with one argument, the clicked button widget instance. Parameters ---------- remove: bool (optional) Set to true to remove the callback from the list of callbacks.
Since button clicks are stateless, they are transmitted from the front-end to the back-end using custom messages.
b5 = ipywidgets.Button(description="Click Me!")
display(b5)
def on_button_clicked(b):
print('Button clicked !!')
b5.on_click(on_button_clicked)
Failed to display Jupyter Widget of type Button
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
The Text widget also has a special .on_submit
event. The .on_submit
event fires when the user hits return.
t1 = ipywidgets.Text()
display(t1)
def handle_submit(sender):
print(str(t1.value).upper())
t1.on_submit(handle_submit)
Failed to display Jupyter Widget of type Text
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Widget properties are IPython traitlets and are eventful. To handle changes, the .on_trait_change
method of the widget can be used to register a callback. The doc string for .on_trait_change
can be seen below.
print(ipywidgets.Widget.on_trait_change.__doc__)
DEPRECATED: Setup a handler to be called when a trait changes. This is used to setup dynamic notifications of trait changes. Static handlers can be created by creating methods on a HasTraits subclass with the naming convention '_[traitname]_changed'. Thus, to create static handler for the trait 'a', create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below). If `remove` is True and `handler` is not specified, all change handlers for the specified name are uninstalled. Parameters ---------- handler : callable, None A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new), handler(name, old, new), or handler(name, old, new, self). name : list, str, None If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name. remove : bool If False (the default), then install the handler. If True then unintall it.
The callback registered can have 4 possible signatures:
Example:
int_range = ipywidgets.IntSlider()
display(int_range)
def on_value_change(name, value):
#print(value)
print(value, ' -- ', end='')
int_range.observe(on_value_change, 'value')
Failed to display Jupyter Widget of type IntSlider
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Visit www.add-for.com for more tutorials and updates.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.