I am a newbie on bokeh, trying to create my first application using the World Bank indicator database. So far I have managed to create a plot that gets updated as I change the value of the slider. However, when I try to add a couple dropdown lists (for my x and y axes) the application is not responding anymore.Both the slider or the dropdown lists fail to update the plot. I have looked at many resources online including tutorials or the documentation but I can’t seem to figure out what I a doing wrong. Any help with be more than greatly appreciated…Also attached the current status of my application.
import pandas as pd
from bokeh.io import output_file,output_notebook,show
from bokeh.models import ColumnDataSource, Slider, Select
from bokeh.plotting import figure
from bokeh.models import HoverTool
from bokeh.models import CategoricalColorMapper
from bokeh.io import curdoc
from numpy.random import random, normal, lognormal
from bokeh.layouts import column, row
from bokeh.layouts import widgetbox
#slice data to year 2000-2020
wdi= wdi_o[67680:380159]
#melting the data
wdi_m= wdi.melt(id_vars=["Country Name","Indicator Name","Country Code","Indicator Code"],var_name="Year")
#cleanup and formatting the wdi_m dataset
wdi_m.convert_dtypes().dtypes
wdi_m=wdi_m.fillna(0).replace("Unnamed: 65",0)
wdi_m.Year=wdi_m.Year.astype("int")
wdi_m= wdi_m.set_index(["Indicator Name","Year"])
#create ColumnDataSource
source= ColumnDataSource(data={
'x' : wdi_m.loc[["Access to electricity (% of population)","Year"]].value,
'y' : (wdi_m.loc[["GDP (current US$)","Year"]].value)/(10**9),
"c" : wdi_m.loc[["Access to electricity (% of population)","Year"],"Country Name"]
})
#create a hovertool
hover= HoverTool(tooltips=[
("GDP ($ billion)", "@y"),
("Access to electricity (%)", "@x"),
("Country Name", "@c")
])
# Create the figure: plot
plot = figure( plot_height=400, plot_width=700, tools= [hover, "pan", "wheel_zoom"])
# Add circle glyphs to the plot
plot.circle(x="x", y="y", fill_alpha=0.8, source=source)
liste= list(wdi["Indicator Name"])
# Define the callback: update_plot
def update_plot(attr, old, new):
yr= slider.value
x= x_select.value
y= y_select.value
plot.xaxis.axis_label = x
plot.yaxis.axis_label = y
new_data= {
'x': data.loc[x,yr].value,
'y': data.loc[y,yr].value,
'c': data.loc[[y,yr],"Country Name"]
}
source.data = new_data
plot.title.text= 'World Bank Indicator for %d' % yr
#Create the Slider dropdown
slider= Slider(start=2000, end=2020, value=2012, step=1, title = " Year")
# Attach the callback to the 'value' property of slider
slider.on_change('value', update_plot)
# Create a dropdown Select widget for the x data: x_select
x_select = Select(
options=liste[0:2],
value='Access to electricity (% of population)',
title='x-axis data'
)
x_select.on_change('value', update_plot)
# Create a dropdown Select widget for the y data: y_select
y_select = Select(
options= ["GDP (current US$)", "Adjusted net national income (current US$"],
value="GDP (current US$)",
title='y-axis data'
)
y_select.on_change("value",update_plot)
# Create layout and add to current document
layout = row(column(slider, x_select, y_select), plot)
curdoc().title = 'BankData'
curdoc().add_root(layout)