-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a Jupyter Widget for ITables (#319)
* npm create anywidget@latest * npm install dt_for_itables --save * Implement the widget * Add init function * Move the widget to itables.widget * Document the ITable widget * Include itables_anywidget in build * Add traitlets as a dependency of itables[widget] * Add selected rows * Update the row selection in the table using model * Recreate the table when the dt_args change * fixup pyproject.toml * The value of the streamlit component is the list of selected rows * Add anywidget to the environment * fix typo * WIP get_selected_rows_after_downsampling * fix tests * Sync data and table def with destroy_and_recreate * Passing the data through dt_args seems to be faster * Selected rows conversion in JS * Add back [tool.hatch.build.targets.sdist] and [tool.hatch.build.targets.wheel] * Selected rows in Streamlit * Fix selected rows * New functions set/get_selected_rows * Pass filtered_row_count explicitly * table_id rather than tableId * selected rows and offline mode in Shiny apps * Set the initial row selection in shiny inputs * Document init_itables and selected_rows * New df property and setter, make some traits private * Version 2.2.0 * Print shiny version if test fails * Require shiny>=1.0 in tests * Install shiny>=1 when Python>3.7 * Skip test if recent shiny is not available * Update the documentation
- Loading branch information
Showing
38 changed files
with
1,275 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ dt_bundle.css | |
|
||
# Streamlit package | ||
src/itables/itables_for_streamlit | ||
|
||
# Jupyter Widget | ||
src/itables/widget/static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,118 @@ | ||
# IPyWidgets | ||
--- | ||
jupytext: | ||
formats: md:myst | ||
notebook_metadata_filter: -jupytext.text_representation.jupytext_version | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
kernelspec: | ||
display_name: itables | ||
language: python | ||
name: itables | ||
--- | ||
|
||
ITables does not come as a [Jupyter Widget](https://ipywidgets.readthedocs.io) at the moment. | ||
You are welcome to subscribe or contribute to [#267](https://github.com/mwouts/itables/issues/267). | ||
# Jupyter Widget | ||
|
||
ITables is available as a [Jupyter Widget](https://ipywidgets.readthedocs.io) since v2.2. | ||
|
||
## The `ITable` widget | ||
|
||
The `ITable` widget has a few dependencies (essentially [AnyWidget](https://anywidget.dev), | ||
a great widget development framework!) that you can install with | ||
```bash | ||
pip install itables[widget] | ||
``` | ||
|
||
The `ITable` class accepts the same arguments as the `show` method, but | ||
the `df` argument is optional. | ||
|
||
```{code-cell} | ||
from itables.sample_dfs import get_dict_of_test_dfs | ||
from itables.widget import ITable | ||
df = get_dict_of_test_dfs()["int_float_str"] | ||
table = ITable(df, selected_rows=[0, 2, 5], select=True) | ||
table | ||
``` | ||
|
||
## The `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: | ||
|
||
```{code-cell} | ||
table.selected_rows | ||
``` | ||
|
||
```{code-cell} | ||
table.selected_rows = [3, 4] | ||
``` | ||
|
||
## The `df` property | ||
|
||
Use it to retrieve the table data: | ||
|
||
```{code-cell} | ||
table.df.iloc[table.selected_rows] | ||
``` | ||
|
||
or to update it | ||
|
||
```{code-cell} | ||
table.df = df.head(6) | ||
``` | ||
|
||
```{tip} | ||
`ITable` will raise an `IndexError` if the `selected_rows` are not consistent with the | ||
updated data. If you need to update the two simultaneously, use `table.update(df, selected_rows=...)`, see below. | ||
``` | ||
|
||
## The `caption`, `style` and `classes` traits | ||
|
||
You can update these traits from Python, e.g. | ||
|
||
```{code-cell} | ||
table.caption = "numbers and strings" | ||
``` | ||
|
||
## The `update` method | ||
|
||
Last but not least, you can update the `ITable` arguments simultaneously using the `update` method: | ||
|
||
```{code-cell} | ||
table.update(df.head(20), selected_rows=[7, 8]) | ||
``` | ||
|
||
## Limitations | ||
|
||
Compared to `show`, the `ITable` widget has the same limitations as the [Streamlit component](streamlit.md#limitations), | ||
e.g. structured headers are not available, you can't pass JavaScript callback, etc. | ||
|
||
The good news is that if you only want to _display_ the table, you do not need | ||
the `ITables` widget. Below is an example in which we use `show` to display a different | ||
table depending on the value of a drop-down component: | ||
|
||
```python | ||
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]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ dependencies: | |
- ghp-import | ||
- shiny | ||
- streamlit | ||
- anywidget | ||
- pip: | ||
- world_bank_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# itables_anywidget | ||
|
||
## Installation | ||
|
||
```sh | ||
pip install itables_anywidget | ||
``` | ||
|
||
## Development installation | ||
|
||
Create a virtual environment and and install itables_anywidget in *editable* mode with the | ||
optional development dependencies: | ||
|
||
```sh | ||
python -m venv .venv | ||
source .venv/bin/activate | ||
pip install -e ".[dev]" | ||
``` | ||
|
||
You then need to install the JavaScript dependencies and run the development server. | ||
|
||
```sh | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
Open `example.ipynb` in JupyterLab, VS Code, or your favorite editor | ||
to start developing. Changes made in `js/` will be reflected | ||
in the notebook. |
Empty file.
Oops, something went wrong.