@@ -16,103 +16,102 @@ kernelspec:
1616
1717ITables is available as a [ Jupyter Widget] ( https://ipywidgets.readthedocs.io ) since v2.2.
1818
19- ## Using ` show `
19+ ## The ` ITable ` widget
2020
21- If you only want to _ display_ the table, you ** do not need**
22- our Jupyter widget. The ` show ` function is enough!
21+ The ` ITable ` widget has a few dependencies that you can install with
22+ ``` bash
23+ pip install itables[widget]
24+ ```
2325
24- ``` {code-cell}
25- import ipywidgets as widgets
26+ The ` ITable ` class accepts the same arguments as the ` show ` method, but
27+ the ` df ` argument is optional.
2628
27- from itables import show
29+ ``` {code-cell}
2830from itables.sample_dfs import get_dict_of_test_dfs
31+ from itables.widget import ITable
2932
30- sample_dfs = get_dict_of_test_dfs()
31-
33+ df = get_dict_of_test_dfs()["int_float_str"]
3234
33- def use_show_in_interactive_output(table_name: str):
34- show(
35- sample_dfs[table_name],
36- caption=table_name,
37- style="table-layout:auto;width:auto;float:left;caption-side:bottom",
38- )
35+ table = ITable(df, selected_rows=[0, 2, 5], select=True)
36+ table
37+ ```
3938
39+ ## The ` selected_rows ` traits
4040
41- table_selector = widgets.Dropdown(options=sample_dfs.keys(), value="int_float_str")
42- out = widgets.interactive_output(
43- use_show_in_interactive_output, {"table_name": table_selector}
44- )
41+ The ` selected_rows ` attribute of the ` ITable ` object provides a view on the
42+ rows that have been selected in the table (remember to pass ` select=True `
43+ to activate the row selection). You can use it to either retrieve
44+ or change the current row selection:
4545
46- widgets.VBox([table_selector, out])
46+ ``` {code-cell}
47+ table.selected_rows
4748```
4849
49- ``` {tip}
50- Jupyter widgets only work in a live notebook.
51- Click on the rocket icon at the top of the page to run this demo in Binder.
50+ ``` {code-cell}
51+ table.selected_rows = [3, 4]
5252```
5353
54- ## Using the ITable widget
55-
56- The ` ITable ` widget has a few dependencies that you can install with
57- ``` bash
58- pip install itables[widget]
59- ```
54+ ## The ` df ` property
6055
61- The ` ITable ` class accepts the same arguments as the ` show ` method, but
62- the ` df ` argument is optional.
56+ Use it to retrieve the table data:
6357
6458``` {code-cell}
65- from itables.widget import ITable
66-
67- table = ITable(selected_rows=[0, 2, 5, 99])
59+ table.df.iloc[table.selected_rows]
60+ ```
6861
62+ or to update it
6963
70- def update_selected_table(change):
71- table_name = table_selector.value
72- table.update(
73- sample_dfs[table_name],
74- caption=table_name,
75- select=True,
76- style="table-layout:auto;width:auto;float:left",
77- )
64+ ``` {code-cell}
65+ table.df = df.head(6)
66+ ```
7867
68+ ``` {tip}
69+ `ITable` will raise an `IndexError` if the `selected_rows` are not consistent with the
70+ updated data. If you need to update the two simultaneously, use `table.update(df, selected_rows=...)`, see below.
71+ ```
7972
80- # Update the table when the selector changes
81- table_selector.observe(update_selected_table, "value")
73+ ## The ` caption ` , ` style ` and ` classes ` traits
8274
83- # Set the table to the initial table selected
84- update_selected_table(None)
75+ You can update these traits from Python, e.g.
8576
86- widgets.VBox([table_selector, table])
77+ ``` {code-cell}
78+ table.caption = "numbers and strings"
8779```
8880
89- ## Get the selected rows
81+ ## The ` update ` method
9082
91- The ` ITable ` widget let you access the state of the table
92- and in particular, it has an ` .selected_rows ` attribute
93- that you can use to determine the rows that have been
94- selected by the user (allow selection by passing ` select=True `
95- to the ` ITable ` widget).
83+ Last but not least, you can update the ` ITable ` arguments simultaneously using the ` update ` method:
9684
9785``` {code-cell}
98- out = widgets.Output()
86+ table.update(df.head(20), selected_rows=[7, 8])
87+ ```
9988
89+ ## Limitations
10090
101- def show_selected_rows(change):
102- with out:
103- out.clear_output()
104- print("selected_rows: ", table.selected_rows)
91+ Compared to ` show ` , the ` ITable ` widget has the same limitations as the [ streamlit component] ( streamlit.md#limitations ) ,
92+ e.g. structured headers are not available, you can't pass JavaScript callback, etc.
10593
94+ The good news is that if you only want to _ display_ the table, you ** do not need**
95+ the ` ITables ` widget. Below is an example in which we use ` show ` to display a different
96+ table depending on the value of a drop-down component:
10697
107- table.observe(show_selected_rows, "selected_rows")
98+ ``` python
99+ import ipywidgets as widgets
100+ from itables import show
101+ from itables.sample_dfs import get_dict_of_test_dfs
108102
109- # Display the initial selection
110- show_selected_rows(None)
103+ def use_show_in_interactive_output (table_name : str ):
104+ show(
105+ sample_dfs[table_name],
106+ caption = table_name,
107+ )
111108
112- out
113- ```
109+ sample_dfs = get_dict_of_test_dfs()
110+ table_selector = widgets.Dropdown( options = sample_dfs.keys(), value = " int_float_str " )
114111
115- ## Limitations
112+ out = widgets.interactive_output(
113+ use_show_in_interactive_output, {" table_name" : table_selector}
114+ )
116115
117- Compared to ` show ` , the ` ITable ` widget has the same limitations as the [ streamlit component ] ( streamlit.md#limitations ) ,
118- e.g. structured headers are not available, you can't pass JavaScript callback, etc.
116+ widgets.VBox([table_selector, out])
117+ ```
0 commit comments