Sunday, December 29, 2024
Google search engine
HomeLanguagesHow to create buttons in Jupyter?

How to create buttons in Jupyter?

In this article, we will learn How to create an interactive button in Jupyter using Python Program. An Interactive Button is a button which when clicked by a user performs a specific function. For the following task, we need the ipywidgets library module. ipywidgets, also known as jupyter-widgets or simply widgets, are interactive HTML widgets for Jupyter notebooks and the IPython kernel.

If your console does not have the module, you can install it by using the following command:

pip install ipywidgets

In most cases, installing the Python ipywidgets package will also automatically configure classic Jupyter Notebook and JupyterLab 3.0 to display ipywidgets. 

Example 1: Creating a simple button.

Syntax: widgets.Button(description=’My Button’)

Code:

Python3




# import module
import ipywidgets as widgets
  
# creating button
widgets.Button(description = 'My Button')


Output:

Example 2: 

Here, we will create an interactive button that will help us to select a random language from a list of given Languages.

  • interact_manual(): Automatically creates a User Interface Control for exploring code and data interactively.
  • choice(): Returns a randomly selected element from the specified sequence.

Code:

Python3




import ipywidgets as widgets
from ipywidgets import interact, interact_manual, fixed
  
from random import choice
  
def lang():
  langSelect = ["English","Deustche","Espanol","Italiano","한국어","日本人"]
  print(choice(langSelect))
    
interact_manual(lang)


Output:

RELATED ARTICLES

Most Popular

Recent Comments