2

I am using python3.10 with Bokeh2.4.3. My goal is to create a simple scatter plot with only a few points (around 10-15 data points). But if I click one of those data points, I want it to open a new tab with another plot i.e. each data point of the first plot is actually a link for other plots. I did add the hover functionality but cannot figure out how to add an onclick event such that when I am hovering if I click, it will open another Bokeh plot.

Here is what I tried:

from bokeh.plotting import figure, show, output_file, save
import pandas
from bokeh.models.tools import HoverTool

df = pandas.read_csv("list_of_all_five_data_file.csv")

df1 = pandas.read_csv("sample_data1.csv")
df2 = pandas.read_csv("sample_data2.csv")
df3 = pandas.read_csv("sample_data3.csv")
df4 = pandas.read_csv("sample_data4.csv")
df5 = pandas.read_csv("sample_data5.csv")

output_file("main.html")

plot = figure(title = "Main Plot")

plot.dot(x=[1, 2, 3, 4, 5], 
         y=[df["file_no"]])

hover = HoverTool()

hover.tooltips = """
    <div>
        Click to see this data
    </div>

"""
plot.add_tools(hover)

show(plot) 

Now, this will give me a plot with 5 dots. But I want that whenever I click on a certain dot, the following code for the corresponding dot will be run. For example, the code for datafile 1 will like like:

x1 = df1['x_values']
y1 = df1['y_values']

p1 = figure(title = "This is data1 plot")

p1.scatter(x1, y1)

show(plot) 

Likewise, there will be code for other data files too. My main question is how to add this onclick functionality? Also, is there any shorter way to do this because if the number of the data files increases, then this code will be very nasty and big.

  • 1
    Currently you are making standalone HTML output that only supports *JavaScript* callbacks. If you want selection events to trigger real Python code, you will have to make a Bokeh server application instead: https://docs.bokeh.org/en/latest/docs/user_guide/server.html Clicking on a glyph generates a *selection* event, there are several examples in the repo and the docs demonstrating JS and Python callbacks for selection events. – bigreddot Jun 25 '22 at 22:28
  • Could you provide us one such link where the callbacks were demonstrated? I cannot find it. The best I can find is the hover functionality but in none of them, I can click. – Abdul Muhaymin Jun 25 '22 at 22:59
  • 2
    Nearly everything here applies: https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-for-selections Just use `on_change` with a Python callback, instead of `js_on_change`. Or see examples: https://github.com/bokeh/bokeh/search?q=selected.on_change&type=code Any selection tool (including tap tool) will make an observable change to the selected indices any time a glyph is "hit" – bigreddot Jun 26 '22 at 04:36
  • 1
    Problem solved. I have used taptool and taptool.callback = OpenURL(url=any_link). – Abdul Muhaymin Jul 01 '22 at 15:42

0 Answers0