☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Extract list of images selected by lassotool

Community Supportbokeh
Sidddhartha_Kosti
Sidddhartha_Kosti

Hey,

I have a dataframe df, like this [x,y,image_list], and I am creating a 2D scatter plot of x, y points. Now I want to extract a list of only those images selected by lassoselect tool. I have tried many things but nothing is working.

This is my code where I am using a JS callback function to print these images on console. Any help would be appreciated

from bokeh.plotting import figure, show
from bokeh.models import LassoSelectTool, ColumnDataSource, CustomJS
from bokeh.events import SelectionGeometry
import pandas as pd
import glob

folder_path = "C:\\Users\\User_name\\img_folder_path"
image_list = glob.glob(f"{folder_path}/*.jpg", recursive=True) + glob.glob(f"{folder_path}/*.png", recursive=True)

data = {
    'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'y': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    'image_list': image_list[:10]
}
df = pd.DataFrame(data)

source = ColumnDataSource(df)

# Create a scatter plot
p = figure(width=400, height=400, tools='lasso_select', title='Image')
scatter = p.scatter('x', 'y', source=source)

#JavaScript callback
callback = CustomJS(args=dict(source=source), code="""
    const selected_indices = source.selected.indices;
    const selected_image_list = [];
    for (let i = 0; i < selected_indices.length; i++) {
        selected_image_list.push(source.data['image_list'][selected_indices[i]]);
    }
    // Send the selected image URLs to the Python code
    console.log("Selected Images:", selected_image_list);
""")

#JavaScript callback to the scatter plot's data source
scatter.data_source.selected.js_on_change('indices', callback)

show(p)
⌁   Link to post
Bryan
Bryan

Your earlier question seemed like you were asking for the points of the lasso itself (the geometry) rather than the points selected by the lassso. There are several examples of adding callbacks to selection indices in Bokeh server apps, here is one:

https://github.com/bokeh/bokeh/blob/HEAD/examples/server/app/selection_histogram.py

In case it is not clear, to access the selected.indices in Python code you must use a Bokeh server application one way or another. [1]


  1. modulo something like pyscript I guess, but I don’t know anything about that other than it exists. ↩︎

⌁   Link to post

End of archived discussion · Replies are disabled