Jupyter Notebook - IPyWidgets



IPyWidgets is a Python library of HTML interactive widgets for Jupyter notebook. Each UI element in the library can respond to events and invokes specified event handler functions. They enhance the interactive feature of Jupyter notebook application.

In order to incorporate widgets in the notebook, we have to import the following module as shown below −

from ipywidgets import widgets

Some basic IPyWidgets are explained here −

Text input

The widgets.text() function renders widgets in the notebook. It is similar to text box form element in HTML. The object of this widget has on_submit() method which listens to activity of the text field and can invoke event handler given as an argument to it.

Button

This widget is similar to HTML button. When it is clicked, the event is registered by on_click() method which calls the click event handler.

IntSlider

A slider control which displays the incrementing integer values. There is also a FloatSlider and IntRangeSlider (changing integer between a range)

Label

This widget is useful to display non editable text in the notebook.

display()

This function from ipywidgets module renders the widget object in notebook’s input cell.

Interact

This function automatically renders a widget depending upon type of data argument given to it. First argument to this function is the event handler and second is a value passed to event handler itself.

Following example shows three label widgets, two text widgets and a button with ‘add’ caption. When the button is clicked, sum of numbers in two text input fields is displayed on the lowermost label.

Form Creation
Advertisements