0

It is kind of a complex example, but I desperately hope to get help...

I'm using jupyter-notebook 5.2.0, bokeh version is 0.12.9 and ipywidgets is 7.0.1.

Here is my DataFrame df:

import numpy as np
import pandas as pd
import datetime
import string

start = int(datetime.datetime(2017,1,1).strftime("%s"))
end = int(datetime.datetime(2017,12,31).strftime("%s"))

# set parameters of DataFrame df for simualtion
size, numcats = 100,10

rints = np.random.randint(start, end + 1, size = size)
df = pd.DataFrame(rints, columns = ['zeit'])
df["bytes"] = np.random.randint(5,20,size=size)
df["attr1"] = np.random.randint(5,100,size=size)
df["ind"] = ["{}{}".format(i,j) for i in string.ascii_uppercase for j in string.ascii_uppercase][:len(df)]

choices = list(string.ascii_uppercase)[:numcats]
df['who']= np.random.choice(choices, len(df))
df["zeit"] = pd.to_datetime(df["zeit"], unit='s')
df.zeit = df.zeit.dt.date

df.sort_values('zeit', inplace = True)
df = df.reset_index(drop=True)
df.head(3)

enter image description here

Now, let's create a bar plot, also using hover tool:

from bokeh.io import show, output_notebook, push_notebook
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
import ipywidgets as widgets
output_notebook()

# setup figure
hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("ind", "@ind"),
    ("who", "@who"),
    ("bytes", "@bytes"),
    ("attr1", "@attr1"),
])

fig = figure(x_range=list(df.ind), plot_height=250, title="Test Bars", 
             toolbar_location=None, tools=[hover])
x = fig.vbar(x="ind", top="bytes", width=0.9, source=ColumnDataSource(df))
h=show(fig, notebook_handle=True)

enter image description here

I'm using a ipywidgets.widgets.SelectionRangeSlider to select a range of dates:

import ipywidgets as widgets

# create slider
dates = list(pd.date_range(df.zeit.min(), df.zeit.max(), freq='D'))
options = [(i.strftime('%d.%m.%Y'), i) for i in dates]
index = (0, len(dates)-1)
myslider = widgets.SelectionRangeSlider(
    options = options,
    index = index,
    description = 'Test',
    orientation = 'horizontal',
    layout={'width': '500px'}
)

def update_source(df, start, end):
    x = df[(df.zeit >= start) & (df.zeit < end)]
    #data = pd.DataFrame(x.groupby('who')['bytes'].sum())
    #data.sort_values(by="bytes", inplace=True)
    #data.reset_index(inplace=True)
    #return data
    return x

def gui(model, bars):
    def myupdate(control1):
        start = control1[0].date()
        end = control1[1].date()
        #display(update_source(model, start, end).head(4))
        data = update_source(model, start, end)
    return myupdate

widgets.interactive(gui(df, x), control1 = myslider)

enter image description here

The problem is, I can't get an update to the graph from the widget:

x.data_source = ColumnDataSource(update_source(df, myslider.value[0].date(), myslider.value[1].date()))
push_notebook(handle=h)

At least, it does something with the plot, as hover is not working anymore...

What am I missing? Or is this a bug?

Thanks for any help

Markus

Community
  • 1
  • 1
Markus
  • 578
  • 6
  • 26
  • You should try keeping your existing CDS an changing the `.data` property of it, not replacing the CDS all together. That is a much more exercised usage pattern. – bigreddot Oct 20 '17 at 18:18
  • Okay, I try `newdf = update_source(df, myslider.value[0].date(), myslider.value[1].date())` and `x.data_source.data["ind"] = newdf.ind` and ` gives a `BokehUserWarning`: `ColumnDataSource's columns must be of the same length.` and the plot does not update neither. I think the basic problem is, that the indices of vbar are to become less in number... Is there some working example? – Markus Oct 20 '17 at 21:30
  • `BokehUserWarning` is not raised, if you do `x.data_source = ColumnDataSource(newdf)`. The plot is updated correctly, if you further add `fig.x_range.factors = newdf.ind` and `push_notebook(handle=h)`. But hover is not working. Any idea how to get hover working? – Markus Oct 20 '17 at 22:14

1 Answers1

0

Figured out how to do it using bokeh: https://github.com/bokeh/bokeh/issues/7082, but unfortunately it only works sometimes...

Best to use CDSViewer.

Markus
  • 578
  • 6
  • 26