Basic Interactive widgets in Jupyter Notebooks

Lots of widgets are available in ipywidgets for jupyter notebooks. The complete list can be obtained here on the official documentation. This section provides the syntax and usage of few basic widgets –

IntSlider

Creates an integer slider.

To use with interact decorator, put the default value of the parameter to an integer (like 1, 0, -1, 2, etc.).

To use with interact function, here is the code for the widget –

Python3




widgets.IntSlider(
    value=7,
    min=0,
    max=10,
    step=1,
    description='Test:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='d'
)


Output:

FloatSlider

Creates a float slider.

To use with interact decorator, put the default value of the parameter to an integer (like 1.0, 0.5, -1.2, 2.001, etc.).

To use with interact function, here is the code for the widget –

Python3




widgets.FloatSlider(
    value=7.5,
    min=0,
    max=10.0,
    step=0.1,
    description='Test:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,


Output:

Text

An entry box for text.

To use with interact decorator, put the default value of the parameter to a string.

To use with interact function, here is the code for the widget –

Python3




widgets.Text(
    value='Hello World',
    placeholder='Type something',
    description='String:',
    disabled=False  
)


Output:

Dropdown

Creates a dropdown menu.

To use with interact decorator, put the default value of the parameter to a sequence (like list or tuple).

To use with the interact function, here is the code for the widget –

Python3




widgets.Dropdown(
    options=['1', '2', '3'],
    value='2',
    description='Number:',
    disabled=False,
)


Output:

DropBox

Checkbox

Creates a checkbox for boolean values (True/False).

To use with interact decorator, put the default value of the parameter to True or False.

To use with the interact function, here is the code for the widget –

Python3




widgets.Checkbox(
    value=False,
    description='Check me',
    disabled=False,
    indent=False
)


Output:

Checkbox

We use some of these widgets in the proceeding examples. To know more about these widgets and all the other jupyter widgets, visit the link to official documentation.

Apart from the widgets discussed here, backends for matplotlib, plotly and bokeh libraries are available to allow implementing their specific interactive plotting widgets. This is covered in the following article – How to Use JupyterLab Inline Interactive Plots

Methods for Interactive controls in Jupyter Notebooks

To add interactive controls, we need to follow the following steps –

  • Step 1: Wrap the code which you want to be interactive inside a function with parameters that you want to alter. Now use these parameter variables inside the code to set the properties or whatever needs to be done using the parameters.
  • Step 2: Use the interact or interact_manual functions from ipywidgets module to call this function.

The interact and interact_manual methods are same except for one difference that the output auto updates as we change the parameters through widgets in case of interact while in case of interact_manual a button is provided to update the output.

Hence use interact_manual when it is computationally expensive to produce the output (so you update only when you are ready to) otherwise use interact.

Interactive Controls in Jupyter Notebooks

This article explains the significance of interactive controls in Jupyter Notebooks and presents a few different methods of adding them to the notebooks for Python programming language. A list of basic controls/widgets and finally examples are provided to demonstrate all that is presented throughout this article. This article expects basic familiarity with Python and Jupyter Notebooks.

Similar Reads

Interactive Controls for Jupyter Notebooks

We often need to explore figures and data in Jupyter notebooks by observing it for different values of parameters involved. The obvious solution is to change the value of parameters in the code and then run it and repeat this process for the different values we are targeting. In practice, this process becomes inconvenient and frustrating as the number of parameters and the possible values for each parameter that we want to explore grows. One solution for this problem is Jupiter interactive controls which can be added by using Jupyter widgets....

Basic Interactive widgets in Jupyter Notebooks

Lots of widgets are available in ipywidgets for jupyter notebooks. The complete list can be obtained here on the official documentation. This section provides the syntax and usage of few basic widgets –...

Custom Interactive Controls in jupyter notebooks

...

Conclusion

...