Tip
The table shown above does not reflect the initial row selection.
This is because the ITable widget was updated with
more row selection commands, see below.
selected_rows traits#The selected_rows attribute of the ITable object provides a view on the
rows that have been selected in the table (remember to pass select=True to activate the row selection). You can use it to either retrieve
or change the current row selection:
table.selected_rows
[0, 2, 5]
table.selected_rows = [3, 4]
df property#Use it to retrieve the table data:
assert table.df is not None
table.df.iloc[table.selected_rows]
| int | float | str | |
|---|---|---|---|
| 3 | 3 | 4.848485 | d |
| 4 | 4 | 4.797980 | e |
or to update it
table.df = df.head(6)
Tip
ITable raises an IndexError if the selected_rows are not consistent with the data. If you need to update both simultaneously, use table.update(df, selected_rows=...), see below.
caption, style and classes traits#You can update these traits from Python, e.g.
table.caption = "numbers and strings"
update method#Last but not least, you can update the ITable arguments simultaneously using the update method:
table.update(df.head(20), selected_rows=[7, 8])
An alternative to the widget, if you only want to display the table, is the show function. Below is an example in which we use show to display a different table depending on the value of a drop-down component:
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])