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:
Approach:
In the code below, apart from importing show and button, we are importing another package in our python shell and that is customJS. customJS provides the user to have customized behaiviors in response to change of a particular event . It is a javascript callback that works in bokeh server apps. In the implementation, we will be using js_on_click(handler) which sets up a javascript handler for button clicks. It activates when the button created is clicked and inside which, customJS will be used as a handler and the message will be printed in the console.
js_on_click(handler)
Code:
Python3
from bokeh.io import show
from bokeh.models import Button, CustomJS
button = Button(label = "Click on the button" ,
button_type = "danger" )
button.js_on_click(CustomJS(code = "console.log('button: You have clicked on the button!')" ))
show(button)
|
Output:

Code Explain:
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.
Code:
Python
from bokeh.io import show
from bokeh.models import Button
from bokeh.layouts import row
buttons = [Button(label = "Button 1" , button_type = "danger" ),
Button(label = 'Button 2' , button_type = 'success' , width = 200 , height = 60 ),
Button(label = 'Button 3' , button_type = 'primary' , width = 100 , height = 100 )]
show(row(buttons))
|
Output:

Code Explain:
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.
Similar Reads
Adding labels to a Bokeh plot
Bokeh includes several layout options for arranging plots and widgets. They make it possible to arrange multiple components to create interactive dashboards or data applications. The layout functions let you build a grid of plots and widgets. You can nest as many rows, columns, or grids of plots tog
3 min read
Bokeh - Adding Widgets
Bokeh is a Python data visualization library for creating interactive charts & plots. It helps us in making beautiful graphs from simple plots to dashboards. Using this library, we can create javascript-generated visualization without writing any scripts. What is a widget? Widgets are interactiv
11 min read
Bulma Button Addons
Bulma is an open-source CSS framework that provides pre-built components which can be combined together to make responsive websites and web applications. To use buttons as add-ons, we have to add a has-addons class on the field container. This will attach the buttons together. Bulma Button addons Cl
1 min read
Bulma Button Group with Addons
In this article, we'll see the Bulma button group with addons. As we know that we can add addons with buttons in form controls of Bulma, similarly we can also group two or more button addons to make a group. For making a group with add-ons, we can create two or more field containers with has-addons
3 min read
Bootstrap 5 Input Group Button Addons
Bootstrap 5 Input Groups are used to extend form controls by adding the buttons, text, or button groups before or after the text inputs, custom selects, or file inputs. Input Group Button Addons are used to extend form controls by adding buttons or button groups. Bootstrap 5 Input group Button Addon
2 min read
How to Teach Addition to Kids
Teaching addition to kids involves introducing them to combining numbers to find a total. It is important to use engaging and interactive methods, such as visual aids, hands-on activities, and simple exercises. Starting with small numbers and gradually increasing complexity helps build their confide
7 min read
How to Add Color Bars in Bokeh?
Bokeh is one of the promising libraries of Python in recent times. It provides high performance and efficiency in data visualizations. One of the big advantages of bokeh is that we can get the output file in various formats such as HTML, notebooks etc. In this article, we will be learning about how
7 min read
How to add color-picker in Bokeh?
In this article, we are going to see how to add a color picker widget in bokeh. Widgets are the best way to make charts more interactive. We can also add widgets in the Bokeh application to provide the best front-end user visualization. Using widgets we can do many things like update charts, connect
1 min read
Make an area plot in Python using Bokeh
Bokeh is a Python interactive data visualization. Unlike Matplotlib and Seaborn, Bokeh renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Plotting the Area Plots A
2 min read
Adding X and Y Axis Labels to Bokeh Figures
Bokeh is a powerful Python library for creating interactive visualizations in web browsers. When creating plots or figures in Bokeh, adding clear and descriptive labels for the X and Y axes is crucial for improving the readability and interpretability of the graph. Customizing these axis labels can
4 min read