{ "cells": [ { "cell_type": "markdown", "id": "17e67564", "metadata": {}, "source": [ "# Custom Extensions\n", "\n", "## Internationalisation" ] }, { "cell_type": "code", "execution_count": 1, "id": "7296f47b", "metadata": {}, "outputs": [], "source": [ "from itables import show\n", "from itables.sample_dfs import get_countries\n", "\n", "df = get_countries(html=False)" ] }, { "cell_type": "markdown", "id": "64de901f", "metadata": {}, "source": [ "DataTables controls can use a different language than English. To\n", "display the table controls in another language, go to the [internationalisation](https://datatables.net/plug-ins/i18n/)\n", "plug-ins page and find the language URL, like e.g." ] }, { "cell_type": "code", "execution_count": 2, "id": "f1ba2d0f", "metadata": {}, "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", "
regioncountrycapitallongitudelatitude
code
\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", " \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", " \n", " \n", " \n", "\n", "\n", "
\n", "
\n", "Loading ITables v2.0.1 from the internet...\n", "(need help?)
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(\n", " df,\n", " language={\"url\": \"https://cdn.datatables.net/plug-ins/2.0.2/i18n/fr-FR.json\"},\n", ")" ] }, { "cell_type": "markdown", "id": "1f1c3db8", "metadata": {}, "source": [ "```{tip}\n", "You can also use the internationalization in the offline mode. Download the translation file,\n", "then set `opt.language` accordingly:\n", "\n", "~~~python\n", "import json\n", "import itables.options as opt\n", "\n", "with open(\"fr-FR.json\") as fp:\n", " opt.language = json.load(fp)\n", "~~~\n", "```\n", "\n", "## Creating a custom DataTables bundle\n", "\n", "To use custom extensions in the offline mode, you will need\n", "to create a bundle of jQuery, DataTables, and the desired extensions.\n", "\n", "To do so, make a copy of\n", "[`itables/dt_for_itables`](https://github.com/mwouts/itables/tree/main/itables/dt_for_itables):\n", "```bash\n", "$ ls itables/dt_for_itables/\n", "package.json package-lock.json README.md src.js\n", "```\n", "\n", "Add or remove the desired extensions in `package.json` and `src.js`. To do this,\n", "you can use the [DataTables download](https://datatables.net/download/) page and\n", "follow the instructions for the _NPM_ download method.\n", "\n", "For instance, say you want to bundle the PDF export button. Change\n", "`src.js` to this code:\n", "```javascript\n", "import JSZip from 'jszip';\n", "import jQuery from 'jquery';\n", "import pdfMake from 'pdfmake';\n", "import DataTable from 'datatables.net-dt';\n", "import 'datatables.net-dt/css/dataTables.dataTables.min.css';\n", "\n", "import 'datatables.net-buttons-dt';\n", "import 'datatables.net-buttons/js/buttons.html5.mjs';\n", "import 'datatables.net-buttons/js/buttons.print.mjs';\n", "import 'datatables.net-buttons-dt/css/buttons.dataTables.min.css';\n", "\n", "DataTable.Buttons.jszip(JSZip);\n", "DataTable.Buttons.pdfMake(pdfMake);\n", "\n", "pdfMake.vfs = pdfFonts.pdfMake.vfs;\n", "\n", "export { DataTable, jQuery };\n", "```\n", "\n", "and run these commands:\n", "```bash\n", "# Install the dependencies in package.json\n", "npm install\n", "\n", "# Install the additional dependencies\n", "npm install pdfmake --save\n", "\n", "# Create dt_bundle.js and dt_bundle.css\n", "npm run build\n", "```\n", "\n", "Finally, you can either deploy `dt_bundle.js` and `dt_bundle.css` on an\n", "http server and pass the URL of `dt_bundle.js` as the `dt_url` option to `show`,\n", "or, in the offline mode, pass the path to `dt_bundle.js`\n", "as the `dt_bundle` argument of the `init_notebook_mode` method (in either\n", "case you can set the values permanently on `itables.options`)." ] } ], "metadata": { "jupytext": { "formats": "md:myst", "text_representation": { "extension": ".md", "format_name": "myst", "format_version": 0.13, "jupytext_version": "1.14.5" } }, "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.9" }, "source_map": [ 13, 19, 24, 30, 35 ] }, "nbformat": 4, "nbformat_minor": 5 }