I am trying to work on a POC model in which I can add some interactivity (like selection of the student based on which the vbar plot will change). I am using a basic student data with their marks.
The data is as follows:
Column 1
Name
Ayan
Deepa
Sayan
Shobhit
Column 2
Marks
98
96
92
94
What I am able to achieve with the below code:
I am able to create the functions and able to get the output in Bokeh Server output. I am also able to create a on_change call back which re creates the dataset based on user input in the dropdown selection.
Where I need help:
I am unable to update the source in my my plots. I tried various ways from various online sites but I am unable to do so.
Some issues faced are:
- When I create the ColumnDataSource with the dataframe the output plot is coming blank
- If I am using dataframe instead of ColumnDataSource the update function is showing it cant change a df or list
Code:
## Packages
from bokeh.core.properties import value
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.layouts import layout, column, gridplot, Row, widgetbox, row
from bokeh.models import TapTool, HoverTool, ColumnDataSource, Range1d, BoxSelectTool, LinearAxis, Range1d
from bokeh.models.widgets import Button, RadioButtonGroup, Select, Slider, CheckboxGroup, Panel, Tabs
from bokeh.models.annotations import LabelSet
_tools_to_show = 'box_zoom,pan,save,hover,resize,reset,tap,wheel_zoom'
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import datetime
## Creating Dataset
def make_dataset(Input):
global Piv_INCS2_CD_Plot3old
global Piv_INCS2_CD_Plot3
global Week_List
global P2_Major
global Total_Incidents_Created
global Resolution_SLO_Miss_Percent
global new_src
global Piv_INCS2_CD_Plot3_List
global Old
global Old_Filter
global Old_CDS
global Name
global Marks
global Names2
global Old_CDS_Name
global Old_CDS_Marks
print("select 2 =", select.value )
print("Input 2 =", Input)
Old = pd.read_csv('Check_Data.csv', encoding='ISO-8859-1')
Old_Filter = pd.DataFrame(Old[Old.Name == Input])
Old_Filter.to_csv('Old_Filter.csv')
Name = [Input]
print("Name = ", Name)
Names2 = Old_Filter["Name"].tolist()
Marks = Old_Filter["Marks"].tolist()
print("Names = ", Name )
print("Marks = ", Marks )
Old_CDS = ColumnDataSource(data = Old_Filter)
print("OLD_CDS = ", Old_CDS)
Old_CDS_Name = ColumnDataSource(data = {'Name':Name})
Old_CDS_Marks = ColumnDataSource(data = {'Marks': Marks})
return Old_CDS
## Creating Plot
def plot(Old_CDS):
global p3
p3 = figure(plot_height=630, plot_width=1000, title="Marks Trend",
toolbar_location=None, tools="")
p3.vbar(x = "Name", top = "Marks", width = 0.9, source=Old_CDS)
p3.xgrid.grid_line_color = None
p3.y_range.start = 0
return p3 # returns the plot
## On Change Function
def update(attr, old, new):
global Piv_INCS2_CD_Plot3_New
global Week_List_New
global Old_CDS_1
global p3
global lay
global Old_CDS_Name_2
Old_CDS_1 = make_dataset(select.value)
Old_CDS.data.update(Old_CDS_1.data)
## Selection Option
options=[("Ayan","Ayan"),("Deepa","Deepa")]
select=Select(title="Name",options=options)
print("select=", select.value )
## Changing value based on user input
select.on_change("value",update)
## Defining intial user selection
Initial_Input = "Ayan"
Old_CDS_2 = make_dataset(Initial_Input)
## Defining Layout
p3 = plot(Old_CDS_2)
lay = row(p3, select)
curdoc().add_root(lay)
Expected result: I should be able to view the vbar chart in the page and when I change the user from dropdown the plot changes
