Bokeh – Adding Widgets. The bokeh. models. widgets module contains definitions of GUI objects similar to HTML form elements, such as button, slider, checkbox, radio button, etc. These controls provide an interactive interface to a plot.
Bokeh allows call back defined with two methods −
- Use the CustomJS callback so that the interactivity will work in standalone HTML documents.
- Use Bokeh server and set up event handlers.
In this section, we shall see how to add Bokeh widgets and assign JavaScript callbacks.
Types of Bokeh Adding Widgets
Button
The constructor takes the following parameters −
Button(label, icon, callback)
The label parameter is a string used as a button’s caption and callback .
The plot itself renders a line glyph between the x and y data series.
A custom JavaScript function named ‘callback’ has been defined using CutomJS() function. It receives a reference to the object that triggered callback (in this case the button) in the form variable cb_obj.
This function alters the source ColumnDataSource data and finally emits this update in source data.
from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource from bokeh.plotting import Figure, output_file, show from bokeh.models.widgets import Button x = [x*0.05 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = Figure(plot_width=400, plot_height=400) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) callback = CustomJS(args=dict(source=source), code=""" var data = source.data; x = data['x'] y = data['y'] for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], 4) } source.change.emit(); """) btn = Button(label="click here", callback=callback, name="1") layout = column(btn , plot) show(layout)
Output (initial)
Click on the button on top of the plot and see the updated plot figure which looks as follows −
Output (after click)
Slider
With the help of slider control, it is possible to select a number between the start and end properties assigned to it.
Slider(start, end, step, value)
In the following example, we register a callback function on the slider’s on_change event. The plot figure continuously updates as you slide the position.
from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource from bokeh.plotting import Figure, output_file, show from bokeh.models.widgets import Slider x = [x*0.05 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = Figure(plot_width=400, plot_height=400) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) handler = CustomJS(args=dict(source=source), code=""" var data = source.data; var f = cb_obj.value var x = data['x'] var y = data['y'] for (var i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], f) } source.change.emit(); """) slider = Slider(start=0.0, end=5, value=1, step=.25, title="Slider Value") slider.js_on_change('value', handler) layout = column(slider, plot) show(layout)
Output
RadioGroup
This widget presents a collection of mutually exclusive toggle buttons showing circular buttons to the left of the caption.
RadioGroup(labels, active)
Where the label is a list of captions and active is the index of the selected option.
Select
This widget is a simple dropdown list of string items, one of which can be selected. The selected string appears at the top window and it is the value parameter.
Select(options, value)
The list of string elements in the dropdown is given in the form of the options list object.
Following is a combined example of radio button and select widgets, both providing three different relationships between the x and y data series. The RadioGroup and Select widgets are registered with respective handlers through the on_change() method.
from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource from bokeh.plotting import Figure, output_file, show from bokeh.models.widgets import RadioGroup, Select x = [x*0.05 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = Figure(plot_width=400, plot_height=400) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) radiohandler = CustomJS(args=dict(source=source), code=""" var data = source.data; console.log('Tap event occurred at x-position: ' + cb_obj.active); //plot.title.text=cb_obj.value; x = data['x'] y = data['y'] if (cb_obj.active==0){ for (i = 0; i < x.length; i++) { y[i] = x[i]; } } if (cb_obj.active==1){ for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], 2) } } if (cb_obj.active==2){ for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], 4) } } source.change.emit(); """) selecthandler = CustomJS(args=dict(source=source), code=""" var data = source.data; console.log('Tap event occurred at x-position: ' + cb_obj.value); //plot.title.text=cb_obj.value; x = data['x'] y = data['y'] if (cb_obj.value=="line"){ for (i = 0; i < x.length; i++) { y[i] = x[i]; } } if (cb_obj.value=="SquareCurve"){ for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], 2) } } if (cb_obj.value=="CubeCurve"){ for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], 4) } } source.change.emit(); """) radio = RadioGroup( labels=["line", "SqureCurve", "CubeCurve"], active=0) radio.js_on_change('active', radiohandler) select = Select(title="Select:", value='line', options=["line", "SquareCurve", "CubeCurve"]) select.js_on_change('value', selecthandler) layout = column(radio, select, plot) show(layout)
Output
Tab widget
Just as in a browser, each tab can show a different web page, the Tab widget is the Bokeh model providing a different view to each figure. In the following example, two plot figures of sine and cosine curves are rendered in two different tabs −
from bokeh.plotting import figure, output_file, show from bokeh.models import Panel, Tabs import numpy as np import math x=np.arange(0, math.pi*2, 0.05) fig1=figure(plot_width=300, plot_height=300) fig1.line(x, np.sin(x),line_width=2, line_color='navy') tab1 = Panel(child=fig1, title="sine") fig2=figure(plot_width=300, plot_height=300) fig2.line(x,np.cos(x), line_width=2, line_color='orange') tab2 = Panel(child=fig2, title="cos") tabs = Tabs(tabs=[ tab1, tab2 ]) show(tabs)
Output
Next Topic – Click Here
Pingback: Bokeh - Customising legends - Adglob Infosystem Pvt Ltd