以下代码适用于0.12.15。即使使用了taptool,从数据表到标签的回调仍然有效。在代码的0.13.0版本中(用source.on_change('selected',statsline)
将source.selected.on_change('indices', statsline)
替换为import pandas as pd
from bokeh.models import (ColumnDataSource, LabelSet, TapTool, OpenURL, NumberFormatter,
TableColumn, DataTable, Slider, CDSView,HoverTool,PreText,Label)
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
df= {'NAME':['John','Jim','Jake','Tim'],
'Sales':[1,2,3,44],
'YEAR': [1990,1991,1990,2018],
'Returns':[203,391,44,0]
}
df=pd.DataFrame(df)
source=ColumnDataSource(data=df)
columns=[TableColumn(field="NAME", title="NAME"),
TableColumn(field="Sales", title= "Sales"),
TableColumn(field="Returns",title="Retuarns")]
data_table=DataTable(source=source, columns=columns, width=200)
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]) +" | Returns: " +str(df["Returns"][df["YEAR"]==date.today().year]),
text_font_size='20pt')
p_stat.add_layout(income)
source.on_change('selected', statsline)
p=figure(plot_width=200, plot_height=300)
p.square(x='YEAR', y='Sales', source=source)
tap_url=TapTool()
url='https://en.wikipedia.org/wiki/@NAME'
tap_url.callback=OpenURL(url=url)
p.add_tools(tap_url)
layout=layout([p_stat],[data_table,p])
curdoc().add_root(layout)
,单击图上的任意位置(字形或空白区域)后,从数据表到标签的回调不再起作用。解决这个问题?
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.35.0</version>
</dependency>