|
1 | | -# IPyWidgets |
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + formats: md:myst |
| 4 | + notebook_metadata_filter: -jupytext.text_representation.jupytext_version |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: myst |
| 8 | + format_version: 0.13 |
| 9 | +kernelspec: |
| 10 | + display_name: itables |
| 11 | + language: python |
| 12 | + name: itables |
| 13 | +--- |
2 | 14 |
|
3 | | -ITables does not come as a [Jupyter Widget](https://ipywidgets.readthedocs.io) at the moment. |
4 | | -You are welcome to subscribe or contribute to [#267](https://github.com/mwouts/itables/issues/267). |
| 15 | +# Jupyter Widget |
| 16 | + |
| 17 | +ITables is available as a [Jupyter Widget](https://ipywidgets.readthedocs.io) since v2.2. |
| 18 | + |
| 19 | +## The `ITable` widget |
| 20 | + |
| 21 | +The `ITable` widget has a few dependencies (essentially [AnyWidget](https://anywidget.dev), |
| 22 | +a great widget development framework!) that you can install with |
| 23 | +```bash |
| 24 | +pip install itables[widget] |
| 25 | +``` |
| 26 | + |
| 27 | +The `ITable` class accepts the same arguments as the `show` method, but |
| 28 | +the `df` argument is optional. |
| 29 | + |
| 30 | +```{code-cell} |
| 31 | +from itables.sample_dfs import get_dict_of_test_dfs |
| 32 | +from itables.widget import ITable |
| 33 | +
|
| 34 | +df = get_dict_of_test_dfs()["int_float_str"] |
| 35 | +
|
| 36 | +table = ITable(df, selected_rows=[0, 2, 5], select=True) |
| 37 | +table |
| 38 | +``` |
| 39 | + |
| 40 | +## The `selected_rows` traits |
| 41 | + |
| 42 | +The `selected_rows` attribute of the `ITable` object provides a view on the |
| 43 | +rows that have been selected in the table (remember to pass `select=True` |
| 44 | +to activate the row selection). You can use it to either retrieve |
| 45 | +or change the current row selection: |
| 46 | + |
| 47 | +```{code-cell} |
| 48 | +table.selected_rows |
| 49 | +``` |
| 50 | + |
| 51 | +```{code-cell} |
| 52 | +table.selected_rows = [3, 4] |
| 53 | +``` |
| 54 | + |
| 55 | +## The `df` property |
| 56 | + |
| 57 | +Use it to retrieve the table data: |
| 58 | + |
| 59 | +```{code-cell} |
| 60 | +table.df.iloc[table.selected_rows] |
| 61 | +``` |
| 62 | + |
| 63 | +or to update it |
| 64 | + |
| 65 | +```{code-cell} |
| 66 | +table.df = df.head(6) |
| 67 | +``` |
| 68 | + |
| 69 | +```{tip} |
| 70 | +`ITable` will raise an `IndexError` if the `selected_rows` are not consistent with the |
| 71 | +updated data. If you need to update the two simultaneously, use `table.update(df, selected_rows=...)`, see below. |
| 72 | +``` |
| 73 | + |
| 74 | +## The `caption`, `style` and `classes` traits |
| 75 | + |
| 76 | +You can update these traits from Python, e.g. |
| 77 | + |
| 78 | +```{code-cell} |
| 79 | +table.caption = "numbers and strings" |
| 80 | +``` |
| 81 | + |
| 82 | +## The `update` method |
| 83 | + |
| 84 | +Last but not least, you can update the `ITable` arguments simultaneously using the `update` method: |
| 85 | + |
| 86 | +```{code-cell} |
| 87 | +table.update(df.head(20), selected_rows=[7, 8]) |
| 88 | +``` |
| 89 | + |
| 90 | +## Limitations |
| 91 | + |
| 92 | +Compared to `show`, the `ITable` widget has the same limitations as the [Streamlit component](streamlit.md#limitations), |
| 93 | +e.g. structured headers are not available, you can't pass JavaScript callback, etc. |
| 94 | + |
| 95 | +The good news is that if you only want to _display_ the table, you do not need |
| 96 | +the `ITables` widget. Below is an example in which we use `show` to display a different |
| 97 | +table depending on the value of a drop-down component: |
| 98 | + |
| 99 | +```python |
| 100 | +import ipywidgets as widgets |
| 101 | +from itables import show |
| 102 | +from itables.sample_dfs import get_dict_of_test_dfs |
| 103 | + |
| 104 | +def use_show_in_interactive_output(table_name: str): |
| 105 | + show( |
| 106 | + sample_dfs[table_name], |
| 107 | + caption=table_name, |
| 108 | + ) |
| 109 | + |
| 110 | +sample_dfs = get_dict_of_test_dfs() |
| 111 | +table_selector = widgets.Dropdown(options=sample_dfs.keys(), value="int_float_str") |
| 112 | + |
| 113 | +out = widgets.interactive_output( |
| 114 | + use_show_in_interactive_output, {"table_name": table_selector} |
| 115 | +) |
| 116 | + |
| 117 | +widgets.VBox([table_selector, out]) |
| 118 | +``` |
0 commit comments