| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Cell Editing\n", |
| 8 |
"DataGrid cells can be edited using in-place editors built into DataGrid. Editing can be initiated by double clicking on a cell or by starting typing the new value for the cell.\n", |
| 9 |
"\n", |
| 10 |
"DataGrids are not editable by default. Editing can be enabled by setting `editable` property to `True`. Selection enablement is required for editing to work and it is set automatically to `cell` mode if it is `none` when editing is enabled.\n", |
| 11 |
"\n", |
| 12 |
"### Cursor Movement\n", |
| 13 |
"Editing is initiated for the `cursor` cell. Cursor cell is the same as the selected cell if there is a single cell selected. If there are multiple cells / rectangles selected then cursor cell is the cell where the last selection rectangle was started.\n", |
| 14 |
"\n", |
| 15 |
"Cursor can be moved in four directions by using the following keyboard keys.\n", |
| 16 |
"- **Down**: Enter\n", |
| 17 |
"- **Up**: Shift + Enter\n", |
| 18 |
"- **Right**: Tab\n", |
| 19 |
"- **Left**: Shift + Tab\n", |
| 20 |
"\n", |
| 21 |
"Once done with editing a cell, cursor can be moved to next cell based on the keyboard hit following the rules above." |
| 22 |
] |
| 23 |
}, |
| 24 |
{ |
| 25 |
"cell_type": "code", |
| 26 |
"execution_count": null, |
| 27 |
"metadata": {}, |
| 28 |
"outputs": [], |
| 29 |
"source": [ |
| 30 |
"from ipydatagrid import DataGrid\n", |
| 31 |
"from json import load\n", |
| 32 |
"import pandas as pd\n", |
| 33 |
"\n", |
| 34 |
"with open(\"./cars.json\") as fobj:\n", |
| 35 |
" data = load(fobj)\n", |
| 36 |
"df = pd.DataFrame(data[\"data\"]).drop(\"index\", axis=1)\n", |
| 37 |
"\n", |
| 38 |
"datagrid = DataGrid(df, editable=True, layout={\"height\": \"200px\"})\n", |
| 39 |
"datagrid" |
| 40 |
] |
| 41 |
}, |
| 42 |
{ |
| 43 |
"cell_type": "markdown", |
| 44 |
"metadata": {}, |
| 45 |
"source": [ |
| 46 |
"All grid views are updated simultaneously to reflect cell edit changes." |
| 47 |
] |
| 48 |
}, |
| 49 |
{ |
| 50 |
"cell_type": "code", |
| 51 |
"execution_count": null, |
| 52 |
"metadata": {}, |
| 53 |
"outputs": [], |
| 54 |
"source": [ |
| 55 |
"datagrid" |
| 56 |
] |
| 57 |
}, |
| 58 |
{ |
| 59 |
"cell_type": "code", |
| 60 |
"execution_count": null, |
| 61 |
"metadata": {}, |
| 62 |
"outputs": [], |
| 63 |
"source": [ |
| 64 |
"# keep track of changed cells\n", |
| 65 |
"changed_cells = {}\n", |
| 66 |
"\n", |
| 67 |
"\n", |
| 68 |
"def create_cell_key(cell):\n", |
| 69 |
" return \"{row}:{column}\".format(row=cell[\"row\"], column=cell[\"column_index\"])\n", |
| 70 |
"\n", |
| 71 |
"\n", |
| 72 |
"def track_changed_cell(cell):\n", |
| 73 |
" key = create_cell_key(cell)\n", |
| 74 |
" changed_cells[key] = cell" |
| 75 |
] |
| 76 |
}, |
| 77 |
{ |
| 78 |
"cell_type": "markdown", |
| 79 |
"metadata": {}, |
| 80 |
"source": [ |
| 81 |
"Changes to cell values can be tracked by subscribing to `on_cell_change` event as below." |
| 82 |
] |
| 83 |
}, |
| 84 |
{ |
| 85 |
"cell_type": "code", |
| 86 |
"execution_count": null, |
| 87 |
"metadata": {}, |
| 88 |
"outputs": [], |
| 89 |
"source": [ |
| 90 |
"def on_cell_changed(cell):\n", |
| 91 |
" track_changed_cell(cell)\n", |
| 92 |
" print(\n", |
| 93 |
" \"Cell at primary key {row} and column '{column}'({column_index}) changed to {value}\".format(\n", |
| 94 |
" row=cell[\"row\"],\n", |
| 95 |
" column=cell[\"column\"],\n", |
| 96 |
" column_index=cell[\"column_index\"],\n", |
| 97 |
" value=cell[\"value\"],\n", |
| 98 |
" )\n", |
| 99 |
" )" |
| 100 |
] |
| 101 |
}, |
| 102 |
{ |
| 103 |
"cell_type": "code", |
| 104 |
"execution_count": null, |
| 105 |
"metadata": {}, |
| 106 |
"outputs": [], |
| 107 |
"source": [ |
| 108 |
"datagrid.on_cell_change(on_cell_changed)" |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cell_type": "markdown", |
| 113 |
"metadata": {}, |
| 114 |
"source": [ |
| 115 |
"A cell's value can also be changed programmatically by using the DataGrid methods `set_cell_value` and `set_cell_value_by_index`" |
| 116 |
] |
| 117 |
}, |
| 118 |
{ |
| 119 |
"cell_type": "code", |
| 120 |
"execution_count": null, |
| 121 |
"metadata": {}, |
| 122 |
"outputs": [], |
| 123 |
"source": [ |
| 124 |
"datagrid.set_cell_value(\"Cylinders\", 2, 12)" |
| 125 |
] |
| 126 |
}, |
| 127 |
{ |
| 128 |
"cell_type": "markdown", |
| 129 |
"metadata": {}, |
| 130 |
"source": [ |
| 131 |
"Whether new cell values are entered using UI or programmatically, both the DataGrid cell rendering and the underlying python data are updated." |
| 132 |
] |
| 133 |
}, |
| 134 |
{ |
| 135 |
"cell_type": "code", |
| 136 |
"execution_count": null, |
| 137 |
"metadata": {}, |
| 138 |
"outputs": [], |
| 139 |
"source": [ |
| 140 |
"datagrid.data.iloc[2][\"Cylinders\"]" |
| 141 |
] |
| 142 |
}, |
| 143 |
{ |
| 144 |
"cell_type": "code", |
| 145 |
"execution_count": null, |
| 146 |
"metadata": {}, |
| 147 |
"outputs": [], |
| 148 |
"source": [ |
| 149 |
"datagrid.set_cell_value_by_index(\"Horsepower\", 3, 169)" |
| 150 |
] |
| 151 |
}, |
| 152 |
{ |
| 153 |
"cell_type": "code", |
| 154 |
"execution_count": null, |
| 155 |
"metadata": {}, |
| 156 |
"outputs": [], |
| 157 |
"source": [ |
| 158 |
"datagrid.data.iloc[3][\"Origin\"]" |
| 159 |
] |
| 160 |
}, |
| 161 |
{ |
| 162 |
"cell_type": "code", |
| 163 |
"execution_count": null, |
| 164 |
"metadata": {}, |
| 165 |
"outputs": [], |
| 166 |
"source": [ |
| 167 |
"def select_all_changed_cells():\n", |
| 168 |
" datagrid.clear_selection()\n", |
| 169 |
" for cell in changed_cells.values():\n", |
| 170 |
" datagrid.select(cell[\"row\"], cell[\"column_index\"])\n", |
| 171 |
"\n", |
| 172 |
" return datagrid.selected_cells" |
| 173 |
] |
| 174 |
}, |
| 175 |
{ |
| 176 |
"cell_type": "markdown", |
| 177 |
"metadata": {}, |
| 178 |
"source": [ |
| 179 |
"Show all cells changed using UI or programmatically by selecting them." |
| 180 |
] |
| 181 |
}, |
| 182 |
{ |
| 183 |
"cell_type": "code", |
| 184 |
"execution_count": null, |
| 185 |
"metadata": {}, |
| 186 |
"outputs": [], |
| 187 |
"source": [ |
| 188 |
"select_all_changed_cells()" |
| 189 |
] |
| 190 |
}, |
| 191 |
{ |
| 192 |
"cell_type": "markdown", |
| 193 |
"metadata": {}, |
| 194 |
"source": [ |
| 195 |
"Setting an entire row at once is also possible." |
| 196 |
] |
| 197 |
}, |
| 198 |
{ |
| 199 |
"cell_type": "code", |
| 200 |
"execution_count": null, |
| 201 |
"metadata": {}, |
| 202 |
"outputs": [], |
| 203 |
"source": [ |
| 204 |
"datagrid.set_row_value(0, [260, \"USA\", 10, \"Very fast car\", \"\", 0, \"1999-01-01\", 0, 0, 0])" |
| 205 |
] |
| 206 |
} |
| 207 |
], |
| 208 |
"metadata": { |
| 209 |
"kernelspec": { |
| 210 |
"display_name": "Python 3 (ipykernel)", |
| 211 |
"language": "python", |
| 212 |
"name": "python3" |
| 213 |
}, |
| 214 |
"language_info": { |
| 215 |
"codemirror_mode": { |
| 216 |
"name": "ipython", |
| 217 |
"version": 3 |
| 218 |
}, |
| 219 |
"file_extension": ".py", |
| 220 |
"mimetype": "text/x-python", |
| 221 |
"name": "python", |
| 222 |
"nbconvert_exporter": "python", |
| 223 |
"pygments_lexer": "ipython3", |
| 224 |
"version": "3.12.1" |
| 225 |
} |
| 226 |
}, |
| 227 |
"nbformat": 4, |
| 228 |
"nbformat_minor": 4 |
| 229 |
} |