Open In App

Understanding Jupyter Notebook Widgets

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:



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:

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:

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.




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




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.




widgets.Text(value ="Hello Geeksforgeek!")

Output:

BoundedIntText widget




widgets.BoundedIntText(
value=7,
min=0,
max=1000,
step=3,
description='Geeksforgeek count:',
disabled=False
)

Output:

ToggleButton




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.




widgets.FileUpload(
accept='',
multiple=False
)

Output:

Frequency Slider

Import below library




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




range_slider = widgets.FloatRangeSlider(
    value=[-1., +1.],
    min=-10., max=+10., step=0.2,
    description='xlim:',
    readout_format='.1f',
)
range_slider

Output:

Grid_button




grid_button = widgets.ToggleButton(
    value=False,
    description='geeksforgeek',
    icon='check'
)
grid_button

Output:

Color_buttons




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

Output:

Title_Textbox




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

Output:

Color_picker




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,




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()




def func1(x):
    return 8+x
interact(func1, x=20);

Output:




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

Output:

Using Interact Ploting the graph




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.


Article Tags :