Introduction to Python Widgets
Syntax of Widgets
Given below is the syntax of widgets:
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.
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.
Displaying a slider which has border values and can be slid along, which is an integer slider.
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 ’)
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.
Let us create a parabola representation which changes its plot shape with respect to the given input values.
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.
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.
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)
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.
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)
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.
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 –