Widgets can buttons, Checkbox Group, Interactive data tables, Drop down menu.. that can be added to Bokeh applications to provide a front end user interface to a data visualization. I will be showing in this tutorial how can we implement such widgets
from bokeh.io import push_notebook, show, output_notebook
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Button
output_notebook()
button = Button(label="Foo", button_type="warning")
show(widgetbox(button))
Let's notice the button_type allowed are primary, success, warning, danger or link
from bokeh.models.widgets import CheckboxGroup
checkbox_group = CheckboxGroup(
labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
show(widgetbox(checkbox_group))
from bokeh.models.widgets import CheckboxButtonGroup
checkbox_button_group = CheckboxButtonGroup(
labels=["Option 1", "Option 2", "Option 3"], active=[1,2])
show(widgetbox(checkbox_button_group))
import pandas as pd
student = pd.read_csv("student.csv")
student.head()
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
dt=student[['school','G3']]
dt.head()
source = ColumnDataSource(student)
columns = [
TableColumn(field="school", title="School"),
TableColumn(field="G3", title="Final Grad")
]
columns
data_table = DataTable(source=source, columns=columns, width=400, height=280)
show(widgetbox(data_table))
from bokeh.models.widgets import Dropdown
menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
dropdown = Dropdown(label="Dropdown button", button_type="danger", menu=menu)
show(widgetbox(dropdown))
from bokeh.models.widgets import MultiSelect
multi_select = MultiSelect(title="Option:", value=["tun", "dza"],
options=[("tun", "Tunisia"), ("dza", "Algeria"), ("can", "Canada"), ("fr", "France")])
show(widgetbox(multi_select))
It can be used when we have only one choice option to pick.
from bokeh.models.widgets import RadioButtonGroup
radio_button_group = RadioButtonGroup(
labels=["Option 1", "Option 2", "Option 3"], active=2)
show(widgetbox(radio_button_group))
from bokeh.models.widgets import RadioGroup
radio_group = RadioGroup(
labels=["Option 1", "Option 2", "Option 3"], active=1)
show(widgetbox(radio_group))
from bokeh.models.widgets import Select
select = Select(title="Option:", value="Tunisia", options=["Tunisia", "Algeria", "Canada", "France"])
show(widgetbox(select))
select
from bokeh.models.widgets import Slider
slider = Slider(start=0, end=10, value=1, step=.1, title="Value")
show(widgetbox(slider))
from bokeh.models.widgets import RangeSlider
range_slider = RangeSlider(start=0, end=100, value=(10,80), step=1, title="Range")
show(widgetbox(range_slider))