I tried using Bokeh, and now I want to find the word and change the color of its glyph. My code is as follows:
import bokeh.plotting as bp
from bokeh.models import HoverTool, CustomJS
from bokeh.models.widgets import TextInput
from bokeh.io import vform
words = ["werner", "herbert", "klaus"]
x=[1,2,3]
y=[1,2,3]
color = ['green', 'blue', 'red']
word_input= TextInput(value="word", title="Point out a word")
source = bp.ColumnDataSource(data= dict(x=x,y=y,words=words, color='color'))
hover= HoverTool(tooltips=[("word", "@words")])
bp.output_file("plot.html", mode="cdn")
p = bp.figure(plot_height = 600, plot_width = 800, title="word2vec", tools=[hover], logo =None)
p.circle('x','y', radius= 0.1, color = color, source=source, line_color=None)
callback= CustomJS(args=dict(source=source), code ="""
var data = source.get('data');
var glyph = cb_obj.get('value')
words = data['words']
colors=data['color']
for (i=0; i< words.length;i++){
if(glyph==words[i]){colors[i]='yellow'}
}
source.trigger('change');
""")
layout = vform(word_input, p)
bp.show(layout)
This code just doesn't work, and I can't figure out why not.
What am I doing wrong? I sent another question on this day, and this is kind of the first step to solving it.
source
share