{ "cells": [ { "cell_type": "markdown", "id": "3180cb21", "metadata": {}, "source": [ "## Row selection\n", "\n", "The [select](https://datatables.net/extensions/select) extension let you select rows (or cells). When you do so,\n", "only the selected rows are exported" ] }, { "cell_type": "code", "execution_count": 1, "id": "d918561c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from itables import init_notebook_mode, show\n", "\n", "init_notebook_mode()" ] }, { "cell_type": "code", "execution_count": 2, "id": "4bee517a", "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "import string\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from itables.sample_dfs import get_countries\n", "\n", "df = get_countries(html=False)\n", "# Add columns for the searchPanes demo\n", "df[\"climate_zone\"] = np.where(\n", " df[\"latitude\"].abs() < 23.43615,\n", " \"Tropical\",\n", " np.where(\n", " df[\"latitude\"].abs() < 35,\n", " \"Sub-tropical\",\n", " # Artic circle is 66.563861 but there is no capital there => using 64\n", " np.where(df[\"latitude\"].abs() < 64, \"Temperate\", \"Frigid\"),\n", " ),\n", ")\n", "df[\"hemisphere\"] = np.where(df[\"latitude\"] > 0, \"North\", \"South\")\n", "wide_df = pd.DataFrame(\n", " {\n", " letter: np.random.normal(size=100)\n", " for letter in string.ascii_lowercase + string.ascii_uppercase\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "id": "7f13b133", "metadata": { "tags": [ "full-width" ] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "
regioncountrycapitallongitudelatitudeclimate_zonehemisphere
code
\n", "\n", "
\n", "Loading ITables v2.2.2 from the init_notebook_mode cell...\n", "(need help?)
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(\n", " df,\n", " select=True,\n", " selected_rows=[2, 4, 5],\n", " buttons=[\"copyHtml5\", \"csvHtml5\", \"excelHtml5\"],\n", ")" ] }, { "cell_type": "markdown", "id": "77b4fefd", "metadata": {}, "source": [ "```{tip}\n", "It is possible to get the updated `selected_rows` back in Python but for this you will have to use,\n", "instead of `show`, either\n", "- the `ITable` [Jupyter Widget](widget.md)\n", "- the `interactive_table` [Streamlit component](streamlit.md)\n", "- or `DT` in a [Shiny app](shiny.md).\n", "```\n", "\n", "```{tip}\n", "The `select` option accept multiple values, as documented [here](https://datatables.net/extensions/select):\n", "- `select=True` or `select=\"os\"` let you select using single click, shift-click and ctrl-click\n", "- `select=\"single\"` let you select a single row\n", "- `select=\"multi\"` for single click selection of multiple rows, `select=\"multi+shift\"`, ...\n", "\n", "With `select={\"style\": \"os\", \"items\": \"cell\"}` you can let the user select specific cells,\n", "however cell selection is not taken into account when exporting the data.\n", "```" ] } ], "metadata": { "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" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" }, "source_map": [ 13, 20, 26, 57, 66 ] }, "nbformat": 4, "nbformat_minor": 5 }