Debian Codegraph @

examples/Selections.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Selections\n",
8 "DataGrid cells can be selected using mouse by simply clicking and dragging over the cells. Pressing Cmd / Ctrl key during selection will add to existing selections. Pressing Shift key allows selection of regions between two clicks.\n",
9 "\n",
10 "DataGrid supports three modes of selection 'cell', 'row', 'column'. In order to disable selections, selection mode can be set to 'none' which is the default setting.\n",
11 "#### Selection Modes\n",
12 "- **'cell'**: Clicking on grid will select only the cell under mouse cursor\n",
13 "- **'row'**: Clicking on grid will select all the cells on the row under mouse cursor\n",
14 "- **'column'**: Clicking on grid will select all the cells on the column under mouse cursor\n"
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": null,
20 "metadata": {
21 "scrolled": false
22 },
23 "outputs": [],
24 "source": [
25 "from ipydatagrid import DataGrid\n",
26 "from json import load\n",
27 "import pandas as pd\n",
28 "\n",
29 "with open(\"./cars.json\") as fobj:\n",
30 " data = load(fobj)\n",
31 "\n",
32 "df = pd.DataFrame(data[\"data\"]).drop(\"index\", axis=1)\n",
33 "\n",
34 "datagrid = DataGrid(df, selection_mode=\"cell\")\n",
35 "datagrid"
36 ]
37 },
38 {
39 "cell_type": "markdown",
40 "metadata": {},
41 "source": [
42 "Select a rectangular region programmatically. 'row1' & 'column1' correspond to top-left corner of selected region, and 'row2'& 'column2' correspond to bottom-right corner of selected region. Row and column values start from 0 and from top-left corner of **grid body**. clear_mode specifies how new selection should be applied in the presence of other selections. possible values are `'none'`: to keep previous selections (default), `'all'`: to clear previous selections, `'current'`: to remove only the last selection pre-existing"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": null,
48 "metadata": {},
49 "outputs": [],
50 "source": [
51 "datagrid.select(row1=1, column1=1, row2=2, column2=2, clear_mode=\"all\")"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {},
57 "source": [
58 "Clear existing selections"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": null,
64 "metadata": {},
65 "outputs": [],
66 "source": [
67 "datagrid.clear_selection()"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": null,
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "datagrid.select(1, 1, 2, 2)"
77 ]
78 },
79 {
80 "cell_type": "code",
81 "execution_count": null,
82 "metadata": {},
83 "outputs": [],
84 "source": [
85 "datagrid.select(2, 2, 3, 3)"
86 ]
87 },
88 {
89 "cell_type": "markdown",
90 "metadata": {},
91 "source": [
92 "Select individual cells by using only row1 and column1 parameters"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "metadata": {},
99 "outputs": [],
100 "source": [
101 "datagrid.select(4, 1)"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": [
110 "datagrid.select(6, 1, clear_mode=\"current\")"
111 ]
112 },
113 {
114 "cell_type": "markdown",
115 "metadata": {},
116 "source": [
117 "Selections are stored as rectangular regions internally and can be accessed using:"
118 ]
119 },
120 {
121 "cell_type": "code",
122 "execution_count": null,
123 "metadata": {},
124 "outputs": [],
125 "source": [
126 "datagrid.selections"
127 ]
128 },
129 {
130 "cell_type": "markdown",
131 "metadata": {},
132 "source": [
133 "Access cells selected. Cells in overlapped regions are listed once."
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": null,
139 "metadata": {},
140 "outputs": [],
141 "source": [
142 "datagrid.selected_cells"
143 ]
144 },
145 {
146 "cell_type": "markdown",
147 "metadata": {},
148 "source": [
149 "Cell values for the selected cells can also be accessed with single method call"
150 ]
151 },
152 {
153 "cell_type": "code",
154 "execution_count": null,
155 "metadata": {},
156 "outputs": [],
157 "source": [
158 "datagrid.selected_cell_values"
159 ]
160 },
161 {
162 "cell_type": "markdown",
163 "metadata": {},
164 "source": [
165 "For large DataGrids and large selection regions, it is more optimal to use iterator and access selected cells one by one. **selected_cell_iterator** can be used to loop through selected cells without need to store them into a list in advance internally."
166 ]
167 },
168 {
169 "cell_type": "code",
170 "execution_count": null,
171 "metadata": {},
172 "outputs": [],
173 "source": [
174 "for cell in datagrid.selected_cell_iterator:\n",
175 " print(cell)"
176 ]
177 },
178 {
179 "cell_type": "markdown",
180 "metadata": {},
181 "source": [
182 "Iterator also provides the total selection count"
183 ]
184 },
185 {
186 "cell_type": "code",
187 "execution_count": null,
188 "metadata": {},
189 "outputs": [],
190 "source": [
191 "len(datagrid.selected_cell_iterator)"
192 ]
193 },
194 {
195 "cell_type": "markdown",
196 "metadata": {},
197 "source": [
198 "DataGrid selections can also be modified with a single assignment statement by passing in selection regions as a list of rectangles."
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": null,
204 "metadata": {},
205 "outputs": [],
206 "source": [
207 "datagrid.selections = [\n",
208 " {\"r1\": 22, \"r2\": 20, \"c1\": 0, \"c2\": 2},\n",
209 " {\"r1\": 6, \"r2\": 6, \"c1\": 2, \"c2\": 2},\n",
210 " {\"r1\": 10, \"r2\": 10, \"c1\": 3, \"c2\": 3},\n",
211 " {\"r1\": 13, \"r2\": 13, \"c1\": 2, \"c2\": 2},\n",
212 "]"
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": null,
218 "metadata": {},
219 "outputs": [],
220 "source": []
221 }
222 ],
223 "metadata": {
224 "kernelspec": {
225 "display_name": "Python 3",
226 "language": "python",
227 "name": "python3"
228 },
229 "language_info": {
230 "codemirror_mode": {
231 "name": "ipython",
232 "version": 3
233 },
234 "file_extension": ".py",
235 "mimetype": "text/x-python",
236 "name": "python",
237 "nbconvert_exporter": "python",
238 "pygments_lexer": "ipython3",
239 "version": "3.6.6"
240 }
241 },
242 "nbformat": 4,
243 "nbformat_minor": 2
244 }