Selection fails when shared datasource

Hello,
I am building a scientific dashboard, which is composed by two scatter plots that share the same dataframe. Panel widgets are used as well to change color, etc. I am trying to add a new functionality: an option to download a dataframe whose rows correspond to the selected points (box or lasso) in any of the scatter plots. The problem arises when I set shared_datasource=True. The code below illustrates the problem, although it is a much simpler version.

df = pd.DataFrame(np.random.rand(100,3), columns=['a','b','c'])
scatter1 = hv.Scatter(data=df, kdims=['a', 'b']).opts(tools=['lasso_select', 'help'])
scatter2 = hv.Scatter(data=df, kdims=['a', 'c']).opts(tools=['lasso_select', 'help'])
layout = (scatter1 + scatter2).cols(2)
layout.opts(opts.Layout(shared_axes=True, shared_datasource=True))
layout

Then I set sel = hv.streams.Selection1D(source=layout) (I’ve tried scatter1 and scatter2 as sources also). However, sel.contents always returns an empty index {'index': []}. If I set shared_datasource=False, it works, but then the cross selection does’t work any more. Could someone help me with this issue?

Additional info: I don’t need to watch when the selection is made, just to get it when a button, for example, is pressed. That’s why I’m not using streams. Thanks!

I believe what I have mashed up between your code and Marc’s ‘How do I link Plots and Tables?’, will show you a way to your objective.

Also I’m new to Python, and running this in Jupyter notebook, I’m unaware on how to access table in print_indexes without using global. So, I would think there would be a more elegant way to access table. I hope this helps!

import pandas as pd
import numpy as np
import holoviews as hv
from holoviews import opts
import panel as pn
import param
from holoviews.selection import link_selections

hv.extension('bokeh')

df = pd.DataFrame(np.random.rand(100,3), columns=['a','b','c'])
dataset = hv.Dataset(df)
link = link_selections.instance()
table = None

def print_sel_indices():
    for i in table.dframe().index:
        print("Dataset index '{i}' is currently selected:".format(i=i))
        print(dataset.iloc[i].data)
        print("-----------------------------------------")

@param.depends(link.param.selection_expr)
def sel_indices_to_table(_):
    global table
    table = hv.Table(dataset.select(link.selection_expr)).opts(width=900, height=200)
    return table

scatter1 = hv.Scatter(data=dataset, kdims=['a', 'b'], vdims=['b'])
scatter2 = hv.Scatter(data=dataset, kdims=['a', 'c'], vdims=['c'])

scat_plots = link(scatter1 + scatter2).cols(2)
scat_plots.opts(shared_axes=True, shared_datasource=False)

layout = pn.Column(scat_plots, sel_indices_to_table)
layout

And by calling print_sel_indices, as seen in image, prints what is currently selected.

1 Like

Thanks @marckassay, I’ll try that solution.