| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"id": "98f76270-7807-4371-bb3a-cfcd03eb08c4", |
| 6 |
"metadata": {}, |
| 7 |
"source": [ |
| 8 |
"### `ipydatagrid` supports auto-fitting of column widths!\n", |
| 9 |
"\n", |
| 10 |
"The DataGrid constructor can take the following arguments:\n", |
| 11 |
"1. `auto_fit_columns`: boolean, indicates whether to auto-fit column widths. Setting to False will reset all column sizes to `base_column_size`.\n", |
| 12 |
"2. `auto_fit_params` : dict, allows for more granular customisation of the auto-fitting algorithm:\n", |
| 13 |
" * `area`: select which section of the grid to apply auto-fitting on. Possible options are `all`, `row-header` (index column) and `body`.\n", |
| 14 |
" * `padding`: specifies how much padding should be added to the width of each column after resizing (default 30) \n", |
| 15 |
" * `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." |
| 16 |
] |
| 17 |
}, |
| 18 |
{ |
| 19 |
"cell_type": "code", |
| 20 |
"execution_count": 5, |
| 21 |
"id": "6eef27bd", |
| 22 |
"metadata": {}, |
| 23 |
"outputs": [ |
| 24 |
{ |
| 25 |
"data": { |
| 26 |
"application/vnd.jupyter.widget-view+json": { |
| 27 |
"model_id": "0a72e7e0e6844989856658cc60b57310", |
| 28 |
"version_major": 2, |
| 29 |
"version_minor": 0 |
| 30 |
}, |
| 31 |
"text/plain": [ |
| 32 |
"DataGrid(auto_fit_params={'area': 'all', 'padding': 30, 'numCols': None}, corner_renderer=None, default_render…" |
| 33 |
] |
| 34 |
}, |
| 35 |
"metadata": {}, |
| 36 |
"output_type": "display_data" |
| 37 |
} |
| 38 |
], |
| 39 |
"source": [ |
| 40 |
"import numpy as np\n", |
| 41 |
"import pandas as pd\n", |
| 42 |
"from ipydatagrid import DataGrid\n", |
| 43 |
"\n", |
| 44 |
"df = pd.DataFrame(\n", |
| 45 |
" data={\n", |
| 46 |
" \"Col1HasAVeryLongName\": [1, 2, 4],\n", |
| 47 |
" \"Col2MediumName\": [4, 5, 6],\n", |
| 48 |
" \"Col3\": [7, 8, 9],\n", |
| 49 |
" }\n", |
| 50 |
")\n", |
| 51 |
"grid = DataGrid(df, index_name=\"index_column\", layout={\"height\": \"90px\"})\n", |
| 52 |
"grid" |
| 53 |
] |
| 54 |
}, |
| 55 |
{ |
| 56 |
"cell_type": "code", |
| 57 |
"execution_count": 2, |
| 58 |
"id": "d5560dea", |
| 59 |
"metadata": {}, |
| 60 |
"outputs": [], |
| 61 |
"source": [ |
| 62 |
"grid.auto_fit_columns = True" |
| 63 |
] |
| 64 |
}, |
| 65 |
{ |
| 66 |
"cell_type": "code", |
| 67 |
"execution_count": 6, |
| 68 |
"id": "e99d654e", |
| 69 |
"metadata": {}, |
| 70 |
"outputs": [], |
| 71 |
"source": [ |
| 72 |
"grid.auto_fit_columns = False" |
| 73 |
] |
| 74 |
}, |
| 75 |
{ |
| 76 |
"cell_type": "code", |
| 77 |
"execution_count": 9, |
| 78 |
"id": "add87d0d", |
| 79 |
"metadata": {}, |
| 80 |
"outputs": [], |
| 81 |
"source": [ |
| 82 |
"grid.auto_fit_params = {\"area\": \"body\", \"padding\": 60, \"numCols\": 1}\n", |
| 83 |
"grid.auto_fit_columns = True" |
| 84 |
] |
| 85 |
} |
| 86 |
], |
| 87 |
"metadata": { |
| 88 |
"kernelspec": { |
| 89 |
"display_name": "Python 3 (ipykernel)", |
| 90 |
"language": "python", |
| 91 |
"name": "python3" |
| 92 |
}, |
| 93 |
"language_info": { |
| 94 |
"codemirror_mode": { |
| 95 |
"name": "ipython", |
| 96 |
"version": 3 |
| 97 |
}, |
| 98 |
"file_extension": ".py", |
| 99 |
"mimetype": "text/x-python", |
| 100 |
"name": "python", |
| 101 |
"nbconvert_exporter": "python", |
| 102 |
"pygments_lexer": "ipython3", |
| 103 |
"version": "3.8.11" |
| 104 |
} |
| 105 |
}, |
| 106 |
"nbformat": 4, |
| 107 |
"nbformat_minor": 5 |
| 108 |
} |