Bokeh- Datatable updating Label text (0.12.16 ->0.13.0 repair job)

时间:2018-07-25 05:22:10

标签: javascript python-3.x dataframe bokeh kpi

I recently discovered that the new bokeh implemented more detailed hover options for stacked bar charts. Giddy, I updated bokeh right away and incorporated into a nested stacked vbar. Unbeknownst to me though, an unrelated callback that I had implemented under the previous release no longer works, and I do not know how to fix it.

I have a data table, its rows feature several columns with numbers. Selecting rows will sum the total for each selected row's columns, and display it in a Label. This aspect no longer works in 0.13.0. Please see a simplified recreation below:

Dataframe(df):
NAME  |  Sales  |  Returns | YEAR
John      33        3        2018
Mike      12        3        2018
Jim       19        9        2018
Tim        3        1        2017
Tom       20        0        2017
import pandas as pd
from bokeh.models import (ColumnDataSource, LabelSet, TapTool, OpenURL,CustomJS, NumberFormatter,
                              TableColumn, DataTable, Slider, CDSView,HoverTool,PreText, BoxSelectTool,
                              Label, Text, Button)
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import layout, column
from datetime import date
from bokeh.core.properties import expr
from bokeh.palettes import Spectral10

source=ColumnDataSource(data=df)

columns=[TableColumn(field="NAME", title="NAME"),
TableColumn(field="Sales", title= "Sales"),
TableColumn(field="Returns",title="Returns")]

data_table=DataTable(source=source, columns=columns)

def statsline(attr,old,new):
    selection=source.selected.indices
    if selection:
        sales=sum(source.data['Sales'][selection])
        returns=sum(source.data['Returns'][selection])

        income.text="SELECTION | Sales: " + str(sales) + " | Returns: " + str(returns)
p_stat=figure(plot_width=1400, plot_height=60, toolbar_location=None)

income=Label(x_units='screen',y_units='screen',x=90, y=5,
             text="Sales: " + str(df["Sales"][df["YEAR"]==date.today().year].sum()) +"   |   
             Returns: " +str(df["Returns"][df["YEAR"]==date.today().year].sum()),
             text_font_size='20pt')
p_stat.add_layout(income)
source.on_change('selected', statsline)

layout=layout([p_stat],[data_table])
curdoc().add_root(layout)

Thanks in advance. Also, is there a less hacky way to display KPIs in bokeh? I'm running out of ideas.

1 个答案:

答案 0 :(得分:0)

在Bokeh 0.13.0中有几个与DataTable相关的回归,将在下一版本中解决。暂时(而且还将继续),此模式将起作用:

source.selected.on_change('indices', ...)