Here is the data frame and i would like to slice the data frame to only plot data for the state user selected.
i am not able to figure how to update the frame without using customJS callbacks
Name
City
State
0
ABC
Abington
PA
32000
1
DEF
Phoenix
AZ
0
2
DEF
Glendale
AZ
13097
3
ABC
Phoenix
AZ
9475
4
EFG
Phoenix
AZ
3975
5
GHI
Phoenix
AZ
5226
6
IHJ
Goodyear
AZ
10949
7
IJK
Crowley
LA
2487
8
KLM
Altamonte Springs
FL
292094
9
ABC
Oak Lawn
IL
37937
10
ABC
Libertyville
IL
16698
11
DEF
Downers Grove
IL
14159
12
DEF
Barrington
IL
9652
13
ABC
Downers Grove
IL
160902
14
EFG
Park Ridge
IL
26916
15
GHI
Hazel Crest
IL
10260
16
IHJ
Chicago
IL
9282
17
IJK
Anchorage
AK
7422
In[2]:
declare imports
import pandas as pd
from bokeh.io import push_notebook, show, output_notebook, output_file
from bokeh.charts import Bar
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Button, TextInput, Slider, Dropdown, Select
from bokeh.layouts import widgetbox ,row, column
In[3]:
get data
df = pd.read_excel(’…/data.xlsx’)
df.head(19);
In[11]:
function to make a plot
def make_plot(cdf):
plot = Bar(cdf, label = ‘City’, values =‘Metric1’, title = ‘BarPlot’, legend=False, width=800, height=800)
push_notebook()
show(plot, notebook_handle = True)
return plot
function to update data
def update_plot(attr, old, new):
state = select.value
sdf = sdf[cdf[‘State’] == state]
make_plot(sdf)
In[13]:
#create a widget
instead of ipywidget can we use bokeh widget
create a function to update the plot based on the state selected
create a lists to assign to plot
stateslist = df[“State”].unique().tolist();
select = Select(title=“Select Name:”, options= stateslist)
In[15]:
plot it
plot = make_plot(df)
update values usinsg widgets
select.on_change(‘value’,update_plot)
picture the layout of plot and controls on ui
controls = column(select)
curdoc().add_root(row(plot,controls))