# Selection fails when shared datasource

**URL:** <https://discourse.holoviz.org/t/selection-fails-when-shared-datasource/1061>\
**Category:** HoloViews\
**Tags:** holoviews\
**Created:** [August 4, 2020, 1:31pm UTC](https://discourse.holoviz.org/t/selection-fails-when-shared-datasource/1061 "2020-08-04T13:31:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedroarbs](https://discourse.holoviz.org/letter_avatar_proxy/v4/letter/p/b19c9b/32.png) [@pedroarbs](https://discourse.holoviz.org/u/pedroarbs)\
**Post date:** [August 4, 2020, 1:31pm UTC](https://discourse.holoviz.org/t/selection-fails-when-shared-datasource/1061/1 "2020-08-04T13:31:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![marckassay](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/marckassay/32/1378_2.png) [@marckassay](https://discourse.holoviz.org/u/marckassay)\
**Post date:** [November 30, 2020, 4:07am UTC](https://discourse.holoviz.org/t/selection-fails-when-shared-datasource/1061/2 "2020-11-30T04:07:32Z")

</div>

I believe what I have mashed up between your code and Marc’s [‘How do I link Plots and Tables?’](https://discourse.holoviz.org/t/how-do-i-link-plots-and-tables/925), 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!

```python
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.

 ![image](https://discourse.holoviz.org/uploads/default/original/2X/8/8014a71a6e4ba3ebab7ad029fa4ae173cf49938f.png)

---

<div class="post-metadata">

**Author:** ![pedroarbs](https://discourse.holoviz.org/letter_avatar_proxy/v4/letter/p/b19c9b/32.png) [@pedroarbs](https://discourse.holoviz.org/u/pedroarbs)\
**Post date:** [November 30, 2020, 12:41pm UTC](https://discourse.holoviz.org/t/selection-fails-when-shared-datasource/1061/3 "2020-11-30T12:41:07Z")

</div>

Thanks @marckassay, I’ll try that solution.
