Understanding Jupyter Notebook Widgets
Last Updated :
18 Dec, 2023
Jupyter Notebook Widgets help us create an interactive user interface in a Jupyter Notebook. Jupyter Widgets are interactive browser controls for Jupyter notebooks. Jupyter Notebook Widgets are collective items that can be added to Jupyter Notebooks to improve user knowledge by giving users the ability to handle and visualize data dynamically. In this article, we will learn about Jupyter Notebook Widgets and how to use them.
Jupyter Notebook Widgets
Jupyter Notebook widgets are interactive components or controls that allow users to interact with data and dynamically modify it in a Jupyter Notebook. They can be buttons, sliders, checkboxes, dropdown menus, text boxes, and more. Widgets enable users to create rich and responsive user interfaces in notebooks, making data exploration and analysis more intuitive and engaging.
Widgets
Widgets are interactive user interface elements that authorize user input in the same way that sliders, buttons, and text fields do.
Here are some related terms to widgets:
- Ipywidgets Library - This library provides a range of Jupyter notebooks.
- Widget Events - It contains multiple events, different tags, images, and videos, and keeps visitors informed about your schedule.
- Wedget Container - Container "box" place: you can drag and drop other widgets, containing added containers, to arrange the layout of the screen. Containers allow you to Group content accompanying the same layout (style, position, etc.)
- Widget Callbacks - Functions may contribute widgets to execute actions in response to user interactions.
- Output widget - This is used to display dynamic content.
Widgets can be used to create a variety of user interfaces, from simple forms to complex dashboards. They can be used to interact with data, control the execution of code, and visualize results.
Here are some examples of how widgets can be used:
- A slider can be used to adjust the threshold of a data filter.
- A text box can be used to enter a search query.
- A checkbox can be used to select or deselect a column in a data table.
- A dropdown menu can be used to select a model to train.
- A button can be used to submit a form or run a code cell.
- A data table can be used to view and edit data.
- A plot can be used to visualize the results of a machine learning model.
Types of Widgets
There are many different types of Jupyter Notebook widgets, each with its own functionality. Some of the most common types of widgets include:
- Sliders: Sliders allow users to select a value from a continuous range.
- Text boxes: Text boxes allow users to enter text.
- Checkboxes: Checkboxes allow users to select or deselect an option.
- Dropdown menus: Dropdown menus allow users to select an option from a list of options.
- Buttons: Buttons allow users to perform actions, such as submitting a form or running a code cell.
- Data tables: Data tables allow users to view and edit tabular data.
- Plots: Plots allow users to visualize data in a variety of ways.
- Media players: Media players allow users to play audio and video files.
- Custom widgets: It is also possible to create custom widgets with specific functionality.
Installation and setup
Installing the Python ipywidgets package will automatically configure Jupyter Noteboook.
pip install ipywidgets
Import ipywidgets
import ipywidgets as widgets
Understanding Jupyter Notebook Widgets
Jupyter Notebook provides various types of widgets that you can use to create interactive elements in your notebook.
display widegts
The display means As an insider, the display function is a crucial part of making Jupyter Notebook widgets interactive.using this render,creating an interger slider as a below image.widgets are objects that show interactive user interface items, in the way that sliders, buttons, text recommendation fields, and output containers. To create these widgets visible and working in the notebook, you use the display function from the IPython IPython.display module to show and show the system.
Python3
import ipywidgets as widgets
from ipywidgets import IntSlider
import IPython.display as display
slider = IntSlider(value=5, min=0, max=10)
display.display(slider)
Output:

keys of Widgets
In addition to benefit, most widgets share keys, description, and disabled. To visualize the entire list of integrated, stateful characteristics of some specific widget, you can query the answers property.
.keys
Python3
slider = IntSlider(value=5, min=0, max=10, step = 1)
slider.keys
Output:
['_dom_classes',
'_model_module',
'_model_module_version',
'_model_name',
'_view_count',
'_view_module',
'_view_module_version',
'_view_name',
'behavior',
'continuous_update',
'description',
'description_allow_html',
'disabled',
'layout',
'max',
'min',
'orientation',
'readout',
'readout_format',
'step',
'style',
'tabbable',
'tooltip',
'value']
Text Widget
Text widgets allow users to input text or display information. You can use Text or Textarea widgets for single-line or multi-line text input, respectively.
Python3
widgets.Text(value ="Hello Geeksforgeek!")
Output:
BoundedIntText widget
Python3
widgets.BoundedIntText(
value=7,
min=0,
max=1000,
step=3,
description='Geeksforgeek count:',
disabled=False
)
Output:

ToggleButton
Python3
widgets.ToggleButton(
value=False,
description='Click me',
disabled=False,
button_style='',
tooltip='Description',
icon='check'
)
Output:

Upload Widgets
The FileUpload allows to upload any type of file(s) like a text file and any extension into memory in the kernel.
Python3
widgets.FileUpload(
accept='',
multiple=False
)
Output:

Frequency Slider
Import below library
Python3
import ipywidgets as widgets
from ipywidgets import HBox, VBox
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
freq_slider = widgets.FloatSlider(
value=3.,
min=1.5,
max=20.0,
step=0.2,
description='Frequency::',
readout_format='.1f',
)
freq_slider
Output:
Range_slider
Python3
range_slider = widgets.FloatRangeSlider(
value=[-1., +1.],
min=-10., max=+10., step=0.2,
description='xlim:',
readout_format='.1f',
)
range_slider
Output:

Grid_button
Python3
grid_button = widgets.ToggleButton(
value=False,
description='geeksforgeek',
icon='check'
)
grid_button
Output:

Color_buttons
Python3
color_buttons = widgets.ToggleButtons(
options=['red', 'blue', 'green','orange'],
description='MultiColor:',
)
color_buttons
Output:

Title_Textbox
Python3
textbox = widgets.Text(
value='Hello Gfg',
description='Title_bar:',
)
textbox
Output:

Color_picker
Python3
picker = widgets.ColorPicker(
concise=True,
description='Gfg Background color:',
value='red',
)
picker
Output:
Using slider to data filtering
To create an IntSlider widget for filtering data within a Pandas DataFrame using a Jupyter Notebook,
Python3
import pandas as pd
import ipywidgets as widgets
from IPython.display import display
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 30, 35, 40, 45]
}
df = pd.DataFrame(data)
def filter_dataframe(age):
filtered_df = df[df['Age'] >= age]
display(filtered_df)
age_slider = widgets.IntSlider(
value=df['Age'].min(), # Set an initial value
min=df['Age'].min(), # Minimum age
max=df['Age'].max(), # Maximum age
description='Minimum Age:'
)
widgets.interactive(filter_dataframe, age=age_slider)
display(age_slider)
Output:

Interactive functions
Automatically creates user interface exploring code and data.It is a perform basic operation.That provide a parameter tuning .using this method then import below Library -
from ipywidgets import interact, interactive, fixed, interact_manual
And perform a many operation,for example:-
Start a few basic function interact()
Python3
def func1(x):
return 8+x
interact(func1, x=20);
Output:

Python3
interact(func1, x=['good','bad','gfg','suraj']);
Output:

Using Interact Ploting the graph
Python3
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
def plot(freq):
x = np.linspace(0, 2*np.pi)
y = np.sin(x * freq)
plt.plot(x, y)
interact(plot, freq = widgets.FloatSlider(value=8.5,
min=1,
max=6.0,
step=0.5))
Output:

Conclusion:
Jupyter Notebook Widgets provide a strong way to build interactive and dynamic interfaces within Jupyter notebooks. They assign users to manipulate and create data in real-time, improving the data exploration and study experience. By following this educational course, you have well-informed yourself about what widgets are, their uses, and how to use them, and you have visualized an example of creating an interactive widget. Experiment with widgets to build attractive and common notebooks for your data reasoning tasks. We again performed a simple EDA task utilizing widgets, which added more convenience to visualization.
Similar Reads
How to Use Swing Applet in Java?
In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers. Approach to Using Swing Applet in JavaWe need to import the packages for Swing and AWT Components.Once the packages are imported, we
4 min read
Elementary Operations on Matrices
Elementary Operations on Matrices are the operations performed on the rows and columns of the matrix that do not change the value of the matrix. A matrix is a way of representing numbers in the form of an array, i.e. the numbers are arranged in the form of rows and columns. In a matrix, the rows and
9 min read
Append a row to a matrix using R
In this article, we will examine various methods to append a row into a matrix in the R Programming Language. What is a matrix?A matrix is a two-dimensional data set, which is a collection of rows and columns. It contains n rows and m columns. Inside the matrix, rows are arranged horizontally and co
4 min read
Convert a matrix into a lower triangular matrix using R
In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are ve
5 min read
Addition of Matrices Worksheet : Solved and Unsolved
Addition of Matrices Worksheet will help students grasp the concept of matrix addition, improve their problem-solving skills, and prepare them for more advanced topics in mathematics. Matrices are a fundamental concept in mathematics, particularly in linear algebra. They are rectangular arrays of nu
10 min read
Matrix Operations
Matrix Operations are basic calculations performed on matrices to solve problems or manipulate their structure. Common operations include: Addition: Add two matrices of the same size.Subtraction: Subtract two matrices of the same size.Scalar Multiplication: Multiply each element of a matrix by a con
7 min read
Matrices and Matrix Arithmetic for Machine Learning
In machine learning, the data often comes in multi-dimensional arrays so matrices are best to handle such inputs. So we need to have some general idea on basic arithmetic operations in matrices. In this article, we will discuss about matrices and matrix arithmetic for machine learning. What is a Mat
6 min read
How to Create a Matrix From a Nested Loop in MATLAB?
Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea
2 min read
How to Perform Matrix Operations using Math.js?
Matrix operations can be computed in JavaScript using the math.js library, which provides a range of functions for handling matrices. With math.js, you can perform basic operations such as addition, subtraction, and multiplication, as well as more advanced operations like transposition, inversion, a
3 min read
Matrix Multiplication Practice Questions
A matrix is a set of numbers arranged in rows and columns to form a rectangular array. Multiplying a matrix by another matrix is called "matrix multiplication". In this article, we will learn what matrix multiplication is. And practice some questions related to it. What is Matrix Multiplication?In l
5 min read
Practice Questions on Subtraction of Matrices
Subtraction of Matrices is one of the operations that are performed between two matrices. It is similar to addition of matrices. This article provides practice questions on subtraction of matrices along with solved examples and concepts related to it. This article serves as a one stop solution for p
11 min read
Matrix multiplication in R using for loop
The matrix multiplication is a basic operation in the linear algebra. In R Programming Language we can perform matrix multiplication using a for loop, which provides a basic understanding of the underlying process involved. In R language, we can perform matrix multiplication using a for loop, which
6 min read
Operations on Matrices in R
Matrices in R are a bunch of values, either real or complex numbers, arranged in a group of fixed number of rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets. A matrix with
8 min read
Matrix in R - Arithmetic Operations
Arithmetic operations include addition (+), subtraction (-), multiplication(*), division (/) and modulus(%). In this article we are going to see the matrix creation and arithmetic operations on the matrices in R programming language. ApproachCreate first matrix Syntax: matrix_name <- matrix(data
3 min read
CopyOnWriteArrayList addAll() method in Java with Examples
boolean addAll(Collection c) This method is used to append all the elements identified by the collection c at the end of the list, in the same order returned by the collection's iterator. Parameters: This method accepts a mandatory parameter c which is the collection containing the elements, which a
4 min read
Subtraction of Matrices
Subtraction of matrices is addition of the negative of a matrix to another matrix which means A - B = A + (-B). The subtraction of the matrix is subtracting the corresponding row-column element of one matrix with same row-column element of another matrix. In this article we will explore subtraction
6 min read
Combining Matrices in R
Combining matrices involves the concatenation of two or more smaller matrices, either row or column wise to form a larger matrix. It is basically a data manipulation operation where the involved matrices must be of compatible sizes to execute the operation. Matrices can be combined either horizontal
3 min read
How to Find Multiplication of Two Matrices of any Size in PHP?
Multiplying two matrices is a common operation in linear algebra. To multiply two matrices of any size in PHP, you can create a function that takes two matrices as input and returns their product. Below are the approaches to find the Multiplication of two Matrices of any size in PHP: Table of Conten
4 min read
Merge two matrices by row names in R
In this article, we will examine various methods to merge two matrices by row names in the R programming language. What is a matrix?A matrix is defined as it is a two-dimensional data set which is the collection of rows and columns. A matrix can have the ability to contain or accept data of the same
4 min read