{ "cells": [ { "cell_type": "markdown", "id": "98f76270-7807-4371-bb3a-cfcd03eb08c4", "metadata": {}, "source": [ "### `ipydatagrid` supports auto-fitting of column widths!\n", "\n", "The DataGrid constructor can take the following arguments:\n", "1. `auto_fit_columns`: boolean, indicates whether to auot-fit column widths. Setting to False will reset all column sizes to `base_column_size`.\n", "2. `auto_fit_params` : dict, allows for more granular customisation of the auto-fitting algorithm:\n", " * `area`: select which section of the grid to apply auto-fitting on. Possible options are `all`, `row-header` (index column) and `body`.\n", " * `padding`: specifies how much padding should be added to the width of each column after resizing (default 30) \n", " * `numCols`: set a hard cap on the number of columns to resize for the selected area (optional). Not setting this value means there is no cap." ] }, { "cell_type": "code", "execution_count": 5, "id": "6eef27bd", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0a72e7e0e6844989856658cc60b57310", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DataGrid(auto_fit_params={'area': 'all', 'padding': 30, 'numCols': None}, corner_renderer=None, default_render…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from ipydatagrid import DataGrid\n", "\n", "df = pd.DataFrame(\n", " data={\n", " \"Col1HasAVeryLongName\": [1, 2, 4],\n", " \"Col2MediumName\": [4, 5, 6],\n", " \"Col3\": [7, 8, 9],\n", " }\n", ")\n", "grid = DataGrid(df, index_name=\"index_column\", layout={\"height\": \"90px\"})\n", "grid" ] }, { "cell_type": "code", "execution_count": 2, "id": "d5560dea", "metadata": {}, "outputs": [], "source": [ "grid.auto_fit_columns = True" ] }, { "cell_type": "code", "execution_count": 6, "id": "e99d654e", "metadata": {}, "outputs": [], "source": [ "grid.auto_fit_columns = False" ] }, { "cell_type": "code", "execution_count": 9, "id": "add87d0d", "metadata": {}, "outputs": [], "source": [ "grid.auto_fit_params = {\"area\": \"body\", \"padding\": 60, \"numCols\": 1}\n", "grid.auto_fit_columns = True" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.8.11" } }, "nbformat": 4, "nbformat_minor": 5 }