|
| 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 | +--- |
| 14 | + |
| 15 | +## Row selection |
| 16 | + |
| 17 | +The [select](https://datatables.net/extensions/select) extension let you select rows (or cells). When you do so, |
| 18 | +only the selected rows are exported |
| 19 | + |
| 20 | +```{code-cell} |
| 21 | +from itables import init_notebook_mode, show |
| 22 | +
|
| 23 | +init_notebook_mode() |
| 24 | +``` |
| 25 | + |
| 26 | +```{code-cell} |
| 27 | +:tags: [hide-input] |
| 28 | +
|
| 29 | +import string |
| 30 | +
|
| 31 | +import numpy as np |
| 32 | +import pandas as pd |
| 33 | +
|
| 34 | +from itables.sample_dfs import get_countries |
| 35 | +
|
| 36 | +df = get_countries(html=False) |
| 37 | +# Add columns for the searchPanes demo |
| 38 | +df["climate_zone"] = np.where( |
| 39 | + df["latitude"].abs() < 23.43615, |
| 40 | + "Tropical", |
| 41 | + np.where( |
| 42 | + df["latitude"].abs() < 35, |
| 43 | + "Sub-tropical", |
| 44 | + # Artic circle is 66.563861 but there is no capital there => using 64 |
| 45 | + np.where(df["latitude"].abs() < 64, "Temperate", "Frigid"), |
| 46 | + ), |
| 47 | +) |
| 48 | +df["hemisphere"] = np.where(df["latitude"] > 0, "North", "South") |
| 49 | +wide_df = pd.DataFrame( |
| 50 | + { |
| 51 | + letter: np.random.normal(size=100) |
| 52 | + for letter in string.ascii_lowercase + string.ascii_uppercase |
| 53 | + } |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +```{code-cell} |
| 58 | +:tags: [full-width] |
| 59 | +
|
| 60 | +show( |
| 61 | + df, |
| 62 | + select=True, |
| 63 | + selected_rows=[2, 4, 5], |
| 64 | + buttons=["copyHtml5", "csvHtml5", "excelHtml5"], |
| 65 | +) |
| 66 | +``` |
| 67 | + |
| 68 | +```{tip} |
| 69 | +It is possible to get the updated `selected_rows` back in Python but for this you will have to use, |
| 70 | +instead of `show`, either |
| 71 | +- the `ITable` [Jupyter Widget](ipywidgets.md) |
| 72 | +- the `interactive_table` [Streamlit component](streamlit.md) |
| 73 | +- or `DT` in a [Shiny app](shiny.md). |
| 74 | +``` |
| 75 | + |
| 76 | +```{tip} |
| 77 | +The `select` option accept multiple values, as documented [here](https://datatables.net/extensions/select): |
| 78 | +- `select=True` or `select="os"` let you select using single click, shift-click and ctrl-click |
| 79 | +- `select="single"` let you select a single row |
| 80 | +- `select="multi"` for single click selection of multiple rows, `select="multi+shift"`, ... |
| 81 | +
|
| 82 | +With `select={"style": "os", "items": "cell"}` you can let the user select specific cells, |
| 83 | +however cell selection is not taken into account when exporting the data. |
| 84 | +``` |
0 commit comments