Updated April 4, 2023
Introduction to Python Widgets
Widgets are small parts on web applications that allow user input. The widgets are eventful objects of Python that have a browser representation, often as control such as a slider, textbox, etc. Interactive GUIs for Python notebook are created using these Widgets. Synchronize stateless and stateful information between JavaScript Python and is done using widgets. Widget input allows us to add parameters and dashboards to the notebooks. Widget API has calls that can be given with various types of input, access bound values, and removing them. Exploration of different parameters using a single query are the results of widgets. Widgets are of many types. Few are Text, drop down, Combo box, Multi-select, which are widely used. All these widgets are quiet neat to understand and implement.
Syntax of Widgets
Given below is the syntax of widgets:
import widget_name
Example:
Ipywidgets packages furnish a lot of common interface controls. These interfaces are used in exploring code and data interactively. These can be customised and assembled for creating user interfaces that are complex. Let’s see various schemes to create user interfaces using Python Ipywidgets. The package of widgets in Python can be installed in Anaconda by default. Manual installation is done with conda install Ipywidgets. Or you can also use pip install Ipywidgets, but then an extension for Jupyter Notebook is needed.
Now, getting into the actual methodology:
The block of code shows how we import the widgets. These are few widgets which are used in general.
Code:
import Ipywidgets as widgets
from Ipywidgets import Hbox, Vbox
import numpy as np
import matplotlib.pyplot as plt
from Ipython.display import display
%matplotlib inline
Examples of Python Widgets
Widgets have an @interact decorator, which controls the function of arguments. The f() function receives an integer as an argument. That displays a slider by default.
Example #1
Displaying a slider which has border values and can be slid along, which is an integer slider.
Code:
import Ipywidgets as wg
import Ipython.display
name = wg.Text (value = ‘Name’)
age = wg.IntSlider (description = ‘Age: ’)
display (name, age)
print (name.value + ‘ is already ‘+ str (age.value) + ‘ now ’)
Output:
The above displays an age bar or a slider, where you can slide the age of John and hold at one. It is still changeable. At age.value, you need to consider taking the value as a string as it is a number or integer, and it needs to be typecasted because all the other parameters in the command line are integers.
You can also import the FloatText widget, which is the same as above but includes a floating-point slider. Wg.FloatText() is used.
Example #2
Let us create a parabola representation which changes its plot shape with respect to the given input values.
Code:
import numpy as np
%matplotlib inline
From matplotlib.pyplot as plt
def myPlot (c):
x = np.linspace(-5,5,20)
y = c * x**2 # we are going to have a parabola here.
plt.plot (x, y, ‘ r-- ‘) # let us make a plot using x and y values
plt.ylabel (‘ y (x) ’) # create y label and make It y (x)
plt.xlabel (‘ x ‘) # x label and named it x
plt.ylim([ 0, 80 ])
plt.xlim ([ -5, 5 ])
myPlot(10) #running the plot with a random value.
Output:
But our point is to see the plot change with respect to c.
For that, we include a cSlider which sides along with the possible given values and updates the plot automatically. We can give a FloatSlider as well.
Code:
c_slide = wg.FloatSlider ( value = 1.0, min = 0.5, max = 3.5, steps = 0.2 )
#Then run the function by calling myPlot and include parameters to use.
wg.interact (myPlot, c = c_slide)
Output:
Now, in the above, you can see the slider, which has border values and slides along, back and forth, which represents c values. With respect to changes in c, you can visualize the change in plot shape.
- c_slide.key: This is the keywords which display all the different types of widgets you might want to change. This will display quiet a big list.
- wg.Widgets.widget_types: This is the keyword used to displays all the types of Widgets that can be used.
Example #3
Code:
import time
progress = wg.IntProgress ( description = ‘ Loading: ’ )
progress.orientation = ‘horizontal’ #we can use vertical as well.
display(progress)
for i in range (100)
progress.value = i
time.sleep (0.1)
Output:
The loading bar in the above block of code can be taken as vertical, which shows a vertical orientation.
Try typing “import this” on Python IDE; you’ll find a poem highlighting the philosophies of Python.
Output:
Conclusion
Demonstration of many features of packages is done by importing Ipywidgets. Widgets can be styled and customised. They can be created using JavaScript and Python Notebook – Jupyter in this case. If you remove the keyword widget in case the same cell cannot be accessed, create subsequent command. Nevertheless, a widget can be created in another cell.
Recommended Articles
This is a guide to Python Widgets. Here we discuss the introduction to Python Widgets along with the examples, respectively. You may also have a look at the following articles to learn more –