0

I am trying to plot summary stats from a pandas dataframe using bokeh visualisation. My aim is to use a slider to filter the data to be plotted and to update the plot accordingly. However, when I launch my bokeh app, neither is happening.

Here is some mock data that I am using:-

Device,Column1,Column2,Column3,Column4,Hour,Day
aaa,98,59,56,57,22,Day one
aaa,29,10,22,14,15,Day one
aaa,21,4,72,25,13,Day one
aaa,87,87,13,83,1,Day one
aaa,41,46,81,25,9,Day one
aaa,85,42,16,12,15,Day one
aaa,73,5,32,19,12,Day one
aaa,84,16,30,34,5,Day one
aaa,28,11,46,63,15,Day one
aaa,50,86,83,48,17,Day one
aaa,74,37,79,48,19,Day one
aaa,40,88,67,79,17,Day one
bbb,1,19,34,6,2,Day one
bbb,5,26,75,25,13,Day one
bbb,27,1,10,84,19,Day one
bbb,62,87,4,51,2,Day one
bbb,71,24,66,6,9,Day one
bbb,49,75,18,67,7,Day one
bbb,51,15,74,35,12,Day one
bbb,38,94,65,32,8,Day one
bbb,23,1,21,26,2,Day one
bbb,83,67,76,7,1,Day one
bbb,87,25,5,86,12,Day one
bbb,58,28,33,71,22,Day one
bbb,18,8,16,13,14,Day one
bbb,89,20,71,39,19,Day one
bbb,60,22,69,79,10,Day one
bbb,93,9,9,9,12,Day one
bbb,76,34,60,21,21,Day one
bbb,39,37,52,23,15,Day one
bbb,45,87,61,96,20,Day one
ccc,92,96,29,97,8,Day one
ccc,62,72,83,45,15,Day one
ccc,42,58,28,94,12,Day one
ccc,45,76,78,24,18,Day one
ccc,58,11,33,55,8,Day one
ccc,4,14,86,9,2,Day one
ccc,21,22,2,78,22,Day one
ccc,11,3,33,41,6,Day one
ccc,57,96,72,48,10,Day one
ddd,28,59,23,66,13,Day one
ddd,62,93,71,42,11,Day one
ddd,6,96,67,50,19,Day one
ddd,4,61,28,29,7,Day one
ddd,71,80,78,2,13,Day one
ddd,59,88,19,56,5,Day one
eee,78,44,27,46,13,Day one
eee,70,54,62,31,14,Day one
eee,4,54,6,12,11,Day one
eee,49,82,37,10,1,Day one
eee,44,81,1,69,17,Day two
eee,59,60,19,9,11,Day two
eee,60,37,40,82,12,Day two
eee,8,17,26,52,18,Day two
eee,10,98,48,24,11,Day two
eee,79,85,73,54,19,Day two
eee,25,82,66,73,6,Day two
eee,8,50,21,28,18,Day two
eee,95,26,14,8,20,Day two
eee,57,22,77,68,5,Day two
eee,77,11,87,35,20,Day two
eee,13,13,16,19,17,Day two
fff,36,81,30,61,7,Day two
fff,24,66,75,94,9,Day two
fff,21,34,46,52,16,Day two
fff,44,92,2,40,0,Day two
fff,6,68,15,34,17,Day two
fff,41,68,16,57,12,Day two
fff,12,84,30,81,0,Day two
fff,42,8,38,43,2,Day two
fff,4,22,66,64,20,Day two
fff,95,37,96,36,21,Day two
fff,44,71,94,39,2,Day two
fff,7,69,28,4,0,Day two
fff,59,63,52,64,16,Day two
fff,29,3,24,83,14,Day two
fff,32,67,74,93,21,Day two
fff,6,73,15,49,14,Day two
fff,70,83,86,90,5,Day two
fff,16,73,82,72,2,Day two
fff,97,72,5,53,10,Day two
fff,36,83,12,58,12,Day two
fff,67,58,2,91,12,Day two
fff,19,81,28,64,11,Day two
fff,2,53,74,11,8,Day two
fff,68,27,78,8,19,Day two
fff,34,77,9,15,12,Day two
fff,50,51,23,88,22,Day two
fff,79,38,14,26,10,Day two
fff,4,41,50,85,9,Day two
fff,41,29,66,49,18,Day two
fff,34,3,51,17,5,Day two
fff,87,79,48,97,4,Day two
fff,66,41,51,16,12,Day two
fff,29,84,22,38,5,Day two
fff,51,50,87,29,2,Day two
fff,63,33,2,61,12,Day two
fff,38,6,20,86,1,Day two
fff,31,87,38,93,20,Day two
ggg,80,93,77,96,3,Day two

EDIT

I have amended my code to add in additional features such as swapping columns, but my problem still persists. I am unable to update the source data to filter by hour, followed by updating the plot accordingly.

from bokeh.util.sampledata import DataFrame
import numpy as np
import pandas as pd
from bokeh.io import curdoc, show

from bokeh.plotting import figure
from bokeh.layouts import row, column, widgetbox
from bokeh.palettes import Category20 as palette
from bokeh.models import Select,CustomJS, CheckboxButtonGroup, ColumnDataSource,IndexFilter,Slider,CDSView,WidgetBox ### Widgets
df = pd.read_csv("exampledata.csv")

#getting grouped means
grouped_devices=df.groupby(['Device','Day','Hour'])['Column1','Column2','Column3','Column4'].mean()
grouped_devices_counts=df.groupby(['Device','Day','Hour']).size()#get counts of devices

grouped_devices_counts=grouped_devices_counts.reset_index()
grouped_devices_counts = grouped_devices_counts.rename(columns= {0: 'Count'})#works
#merge
group_device_summary=pd.merge(grouped_devices,grouped_devices_counts, on=['Day','Hour','Device'])#works

hournum=11
group_device_summary_dayone=group_device_summary[(group_device_summary['Day']=="Day one")] 
data=group_device_summary_dayone.to_dict()
source=ColumnDataSource(group_device_summary_dayone)


p=figure(toolbar_location="above")
# Add a circle glyph to the figure p
scatter=p.circle('Column1', 'Column2', source=source,color='red',legend_label='Day one')

handlerx = CustomJS(args=dict(scatter=scatter), code="""
   scatter.glyph.x = {field: cb_obj.value};
""")
handlery = CustomJS(args=dict(scatter=scatter), code="""
   scatter.glyph.y = {field: cb_obj.value};
""")
#slider=Slider(start=0,end=23, step=1,value=hournum)
sliderhour=Slider(start=0,end=23,step=1,value=hournum)
sliderhandler = CustomJS(
    #### THIS IS THE PART I NEED HELP WITH ######
     

    args=dict(source=source),code="""
    data = source.data;
    hournum=cb_obj.value;
    newdata = group_device_summary_dayone[(group_device_summary_dayone['Hour']==hournum)];
    source.data = newdata;


    """
    )

selectx = Select(title="df-column:", options=list(group_device_summary_dayone.columns))
selectx.js_on_change('value', handlerx)
selecty = Select(title="df-column:", options=list(group_device_summary_dayone.columns))
selecty.js_on_change('value', handlery)
sliderhour.js_on_change('value',sliderhandler)
show(column(selectx,selecty,sliderhour,p))


Can anyone point out what I need to amend which would allow the slider function to update my dataframe?

metaltoaster
  • 380
  • 2
  • 15
  • 1
    There are no dataframes in the browser. Browsers do not know anything about Pandas or Python code. If you use real Python/Pandas code e.g. `dfe['Hour']==hournum` then you will need to run and deploy a Bokeh server, so that you can use real Python callbacks instead of JavaScript callbacks. Otherwise doing things like that in JS is possible but you will need to write all the explicit update loops, etc. on the bare JS arrays. – bigreddot Jul 28 '21 at 15:39
  • Thank you for your response. Could you point me in the direction of any relevant materials or related posts that explains/demos how to do this, please? Very new to python and bokeh in general – metaltoaster Jul 29 '21 at 12:34
  • Do you mean for running a Bokeh server? Then this is the best place to start https://docs.bokeh.org/en/latest/docs/user_guide/server.html Or if you mean using JS callbacks, then here: https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html – bigreddot Jul 29 '21 at 21:39

0 Answers0