mwouts / itables

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

Is there interaction possible between itables and the altair package (graphs) ? #334

Open tomnobelsAM opened 4 days ago

tomnobelsAM commented 4 days ago

It would be nice if there could be an interaction between these 2 packages If you click on a graph , you get the details in itables or if you make a selection in itables , you get a graph visualisation in altair.

Do you think this is possible ?

Thanks in advance

mwouts commented 2 days ago

Hello @tomnobelsAM , great question, thanks! That is possible indeed.

You will need to choose an "application" framework that is supported by both ITables and your graph library (e.g. Jupyter Widgets, Streamlit, Shiny for Python as far as ITables is concerned).

Then you will use either the graph state to change the content of your table, or the selected row(s) in the table to change the content of the graph.

In the documentation we have an example that updates the content of a table depending on a dropdown object:

import ipywidgets as widgets
from itables import show
from itables.sample_dfs import get_dict_of_test_dfs

def use_show_in_interactive_output(table_name: str):
    show(
        sample_dfs[table_name],
        caption=table_name,
    )

sample_dfs = get_dict_of_test_dfs()
table_selector = widgets.Dropdown(options=sample_dfs.keys(), value="int_float_str")

out = widgets.interactive_output(
    use_show_in_interactive_output, {"table_name": table_selector}
)

widgets.VBox([table_selector, out])

The other way around, getting the selected rows in an ITable widget is easily done with the .selected_rows trait (use select="single" rather than select=True if you want to allow selecting only one row).

The other supported frameworks include Streamlit (which seems to support Altair plots) and Shiny for Python.