In this article, we will learn about how to add a button in bokeh. Now, Bokeh provides us with a variety of widgets that can be used for various purposes. One of them is button. The button is one of the widgets of bokeh.models module that helps us in creating a button in our python notebook. Lets us see an example in order to understand the concept better. But before that, if you are using local device for the above implementation, then be sure to have python installed in the device and after that, run this code in the command prompt for the bokeh functionalities to work properly in the code editor.
pip install bokeh
After the installation is done, let’s move to the code and learn the implementation.
Example 1: Adding a button in Bokeh:
Now, in the code, after importing the packages and creating a variable(button) inside which we are specifying different properties of the button, we are using js_on_click handler which is used for button clicks. So, as soon as someone clicks the button, the handler gets triggered, and after that customJS callback activates and prints the message in the console which can be checked using “inspect element” by the right click of the mouse.
Now, we can add buttons of varying colors such as warning(yellow), success(yellow), primary(blue) etc.
Example 2: Adding multiple buttons in Bokeh.
Let us take another example where we will be adding multiple buttons, row-wise and column-wise in our plot. In the code below, we are importing a package from bokeh.layouts module is known as row which helps us to show the buttons in a row-wise manner.
In the above code, after importing all the necessary packages we are using variable buttons which is an array or list of 3 buttons, each with a different size, color, and label. After that using show(row(buttons)), e is showing all the buttons in ‘row-wise’ manner.
Apart from that, we can also show all the buttons in column-wise format. For that, we need to import column package from bokeh.layouts, and instead of show(row(buttons)), we need to write show(column(buttons)) and all the buttons will be printed column-wise.
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.