altair icon indicating copy to clipboard operation
altair copied to clipboard

selection: getting selected data

Open eamonnmag opened this issue 5 years ago • 9 comments

Is there a way, given a selection to give back the selected data as a dataframe for example?

eamonnmag avatar Oct 04 '18 21:10 eamonnmag

I've never seen that in Altair but perhaps Jake knows a way. Something similar might be to have the selection displayed as a table (like a heat map but with text marks in the cells), but I know this is not exactly what you are asking.

sort of like this after a selection

Alcampopiano avatar Oct 04 '18 21:10 Alcampopiano

No, there's no easy way to do that at the moment.

If you write a custom javascript frontend, it is theoretically possible via the vega view api, but I don't know of any example of that.

Making this easier is on the Vega-Lite roadmap, and once it's developed there we can think about how to support it from the Python side.

jakevdp avatar Oct 04 '18 21:10 jakevdp

It would be nice to use the altair chart as a way to let user select input for some other part of code.

Currently I am using streamlit and such a usecase is coming up but I cannot really do anything about it.

yassersouri avatar Oct 13 '19 14:10 yassersouri

Yes, it would be nice, and no, there is still no way to do this, because the selection happens in the Javascript layer and there is no mechanism for the Javascript to communicate back to Python. Enabling this would require changes to Altair, vega-embed, the vega IPython extension, and the vega Jupyter extension. I don't know of anybody working on this – if it's important to you, you might consider exploring what it would take.

jakevdp avatar Oct 14 '19 03:10 jakevdp

@eamonnmag It is not currently possible to emit signals from Altair/VegaLite to Ipywidgets (or any other widget framework). There is some work on this for Jupyter in https://github.com/yifanwu/b2 and for Streamlit in https://github.com/streamlit/streamlit/issues/2263. There is also an open issue for supporting this in Panel https://github.com/holoviz/panel/issues/2365.

Since this is not an issue that Altair can solve, would you mind closing this issue and continue discussion in one of the links above or on the Vega-Lite issue tracker?

joelostblom avatar Feb 23 '21 22:02 joelostblom

This did the trick for me: https://stackoverflow.com/questions/64565901/how-to-retrieve-attributes-from-selected-datum-in-altair However, you need to serve the chart in Streamlit to be able to use this solution.

Z-Shakeri avatar May 27 '21 06:05 Z-Shakeri

One reason this would be helpful is that it enables using altair charts as highly tuned selection dialog for a more complicated tool. E.g. I have various timeseries and various histogram and scatterplot views of the data within - so I can use linked brushing to very quickly visually do highly complicated subselections that'd be 5-10 lines of super annoying code and a couple of minutes to write.

Here's how folks do it in bokeh right now (admittedly fairly gnarly): https://stackoverflow.com/questions/34164587/get-selected-data-contained-within-box-select-tool-in-bokeh

Any workaround with somehow getting vega to debug log the selection values in the console so I can copy paste easily would go a long of the way! Might try to figure that out, and will report results back.

makmanalp avatar Aug 27 '21 16:08 makmanalp

Agreed this would be very nice to have. Note that the Streamlit developers have created a solution that works on their frontend.

Nobody has yet stepped-up to create a more generally-usable solution.

jakevdp avatar Aug 27 '21 16:08 jakevdp

The capability to listen to Vega events and define custom callback for e.g. selected points was recently merged in Panel and will likely be included in their next major release, exciting! https://github.com/holoviz/panel/pull/2592 and now in the docs at https://pyviz-dev.github.io/panel/reference/panes/Vega.html

image

joelostblom avatar Dec 16 '21 20:12 joelostblom

This functionality will be available in Altair 5.1 using the JupyterChart jupyter widget added in https://github.com/altair-viz/altair/pull/3119. This will work in all environments that support Jupyter Widgets (classic jupyter notebook, jupyter lab, colab, visual studio code, and others).

Other dashboard toolkits that want to provide this functionality will need to implement it themselves, as Panel does.

jonmmease avatar Aug 02 '23 18:08 jonmmease