Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to create buttons in Jupyter?

  • Last Updated : 24 Feb, 2021

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!