mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
805 stars 58 forks source link

Support for Selecting rows? #250

Closed jnaylor92 closed 2 months ago

jnaylor92 commented 8 months ago

I see that DataTables supports row select: https://datatables.net/examples/api/select_row.html

Is there any plan to include this in itables?

mwouts commented 8 months ago

Hi @jnaylor92 , well actually the selection extension is included in ITables! It's a requirement for the SearchPanes and SearchBuilder extensions.

I did not add an example for that extension since I was not sure how to use that information back in Python - do you have a plan for that?

See also #208 for an example on how to retrieve the selected row in the context of a Shiny app (with an older version of ITables).

jnaylor92 commented 8 months ago

A very simple example would be something like:

from itables.sample_dfs import get_countries
from itables import init_notebook_mode, show

init_notebook_mode(connected=False)
countries_table = show(get_countries(), connected=False)

selected_countries - countries_table.get_selected()

I really like ITables for what it gives and the way it looks in comparison to something like qgridnext which gives selection but not whole table search and pagination.

I am trying to build up an interactive form that includes displaying a table of data for the user to select samples to then do something further with. Using the above example it might be to plot the selected countries on a map using the lat / long

mwouts commented 8 months ago

I see. Well at the moment there is no direct way to retrieve the selected rows in ITables. That might come at a later stage when we improve the support for "apps" (i.e. Dash, Shiny, or Python widgets), but there is no ETA for that at the moment, for now ITables is "display only". Please leave the issue open though, so that I can give an update when the situation changes.

jnaylor92 commented 8 months ago

That all makes sense and it is good to hear there is a plan to head in that direction in the future. Appreciate the quick responses

ronnystandtke commented 5 months ago

This feature would be awesome! I use itables in a simple JupyterLite notebook to complement a map of bridges: https://ronnystandtke.github.io/KUBA/lab?path=KUBA.ipynb I would absolutely love to be able to select an entry in the table and jump to the bridge in the map. Any way to detect the selection in the table would be very helpful.

mwouts commented 5 months ago

Sure! That will come at some point.

Already we have made progress with the apps - now we do have a streamlit component. I think I'd like to get at least a second "app" component, ideally a jupyter widget (https://github.com/mwouts/itables/issues/267), before I expose the selected cells (without the widget I'm not sure how you'd get a callback on selection changes).

NB:

  1. If you're at ease with JavaScript you might also try to port this example that worked with an earlier version of ITables in the context of a Shiny app: https://github.com/mwouts/itables/issues/208#issuecomment-1832823686
  2. The DataTable css seems to be missing from your app (the buttons have a odd look), I'm not sure what can cause this?
ronnystandtke commented 5 months ago
2. The DataTable css seems to be missing from your app (the buttons have a odd look), I'm not sure what can cause this?

I was also wondering about this. I use a plain simple JupyterLite notebook with a basic requirements list: https://github.com/ronnystandtke/KUBA/blob/main/requirements.txt In the notebook itself I install itables via the built-in magic command %pip: %pip install -q itables Am I missing something?

mwouts commented 2 months ago

Hi @allanJard, now that I have a streamlit component and almost a Jupyter Widget (#267), I would like to give access to the selected rows.

The problem is that I am not sure how to convert table.rows('.selected') to an array of numbers (the selected row indices). Would you have an example around? Thanks!

AllanJard commented 2 months ago

You want the [rows().indexes() chained method](https://datatables.net/reference/api/rows().indexes()) most likely. That will give you the "data index" (i.e. the order in which it was loaded into DataTables. You might be interested in the [rows().ids() method](https://datatables.net/reference/api/rows().ids()) to get row ids if they are defined. They are often more useful externally.

Finally, use .rows({selected: true}) to get selected rows rather than the .selected class selector. The reason for that is that the class selector will only work with rows which have already been rendered. That might not be the case if Ajax / JS loaded data is used and the user does an action (e.g. "Select all") that selects rows which have not yet been made visible.

mwouts commented 2 months ago

The new ITable widget in ITables v2.2 has a selected_rows trait, see the documentation