简体   繁体   中英

Bokeh: chart from pandas dataframe won't update on trigger

I have got a pandas dataframe whose columns I want to show as lines in a plot using a Bokeh server. Additionally, I would like to have a slider for shifting one of the lines against the other.

My problem is the update functionality when the slider value changes. I have tried the code from the sliders-example of bokeh, but it does not work.

Here is an example

import pandas as pd
from bokeh.io import vform
from bokeh.plotting import Figure, output_file, show
from bokeh.models import CustomJS, ColumnDataSource, Slider

df = pd.DataFrame([[1,2,3],[3,4,5]])
df = df.transpose()
myindex = list(df.index.values)
mysource = ColumnDataSource(df)

plot = Figure(plot_width=400, plot_height=400)

for i in range(len(mysource.column_names) - 1):
    name = mysource.column_names[i]    
    plot.line(x = myindex, y = str(name), source = mysource)

offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1)

def update_data(attrname, old, new):
    # Get the current slider values
    a = offset.value

    temp = df[1].shift(a)
    #to finish#

offset.on_change('value', update_data)

layout = vform(offset, plot)

show(layout)

Inside the update_data -function I have to update mysource , but I cannot figure out how to do that. Can anybody point me in the right direction?

Give this a try... change a=offset.value to a=cb_obj.get('value')

Then put source.trigger('change') after you do whatever it is you are trying to do in that update_data function instead of offset.on_change('value', update_data) .

Also change offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1, callback=CustomJS.from_py_func(offset))

Note this format I'm using works with flexx installed. https://github.com/zoofio/flexx if you have Python 3.5 you'll have to download the zip file, extract, and type python setup.py install as it isn't posted yet compiled for this version...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM