Skip to content

Commit 3e778e7

Browse files
authored
Python side API changes (#130)
* change get_cell_value * fix comment typo * change signature of get_cell_value and get_cell_value_by_index * fix typo in get_cell_value_by_index * add internal _get_cell_value_by_numerical_index function * change set_cell_value signature * name changes * update example notebooks * removed memoization, improved docstring for get_cell_calue
1 parent 483e255 commit 3e778e7

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

examples/CellEditing.ipynb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@
8888
"def on_cell_changed(cell):\n",
8989
" track_changed_cell(cell)\n",
9090
" print(\"Cell at primary key {row} and column '{column}'({column_index}) changed to {value}\".format(\n",
91-
" row=cell['row'], column=cell['column'], column_index=cell['column_index'], value=cell['value']))\n",
92-
" "
91+
" row=cell['row'], column=cell['column'], column_index=cell['column_index'], value=cell['value']))"
9392
]
9493
},
9594
{
@@ -139,7 +138,7 @@
139138
"metadata": {},
140139
"outputs": [],
141140
"source": [
142-
"datagrid.set_cell_value_by_index(1, 3, 16)"
141+
"datagrid.set_cell_value_by_index(\"Horsepower\", 3, 169)"
143142
]
144143
},
145144
{
@@ -198,7 +197,7 @@
198197
"name": "python",
199198
"nbconvert_exporter": "python",
200199
"pygments_lexer": "ipython3",
201-
"version": "3.6.6"
200+
"version": "3.7.6"
202201
}
203202
},
204203
"nbformat": 4,

ipydatagrid/datagrid.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def all_values(self):
8686
it = self.__iter__()
8787

8888
for cell in it:
89-
value = self._grid.get_cell_value_by_index(cell['c'], cell['r'])
89+
value = self._grid._get_cell_value_by_numerical_index(cell['c'], cell['r'])
9090
values.append(value)
9191

9292
return values
@@ -331,24 +331,23 @@ def data(self, dataframe):
331331
'schema': schema,
332332
'fields': [{field['name']:None} for field in schema['fields']]}
333333

334-
def get_cell_value(self, column, row_index):
335-
"""Gets the value for a single cell by column name and row index.
334+
def get_cell_value(self, column_name, primary_key_value):
335+
"""Gets the value for a single or multiple cells by column name and index name.
336336
337-
Tuples should be used to index into multi-index columns.
337+
Tuples should be used to index into multi-index columns."""
338338

339-
Note: The provided row_index should correspond to the row index in the
340-
untransformed dataset."""
341-
342-
return self._data['data'][row_index][column]
339+
row_indices = self._get_row_index_of_primary_key(primary_key_value)
340+
341+
return [self._data['data'][row][column_name] for row in row_indices]
343342

344-
def set_cell_value(self, column, primary_key, value):
343+
def set_cell_value(self, column_name, primary_key_value, new_value):
345344
"""Sets the value for a single cell by column name and primary key.
346345
347346
Note: This method returns a boolean to indicate if the operation
348347
was successful.
349348
"""
350349

351-
row_indices = self._get_row_index_of_primary_key(primary_key)
350+
row_indices = self._get_row_index_of_primary_key(primary_key_value)
352351

353352
# Bail early if key could not be found
354353
if not row_indices:
@@ -358,9 +357,9 @@ def set_cell_value(self, column, primary_key, value):
358357
else:
359358
op_success = []
360359
for row_index in row_indices:
361-
if column in self._data['data'][row_index] and row_index is not None:
362-
self._data['data'][row_index][column] = value
363-
self._notify_cell_change(row_index, column, value)
360+
if column_name in self._data['data'][row_index] and row_index is not None:
361+
self._data['data'][row_index][column_name] = new_value
362+
self._notify_cell_change(row_index, column_name, new_value)
364363
op_success.append(True)
365364
else:
366365
op_success.append(False)
@@ -369,26 +368,21 @@ def set_cell_value(self, column, primary_key, value):
369368

370369
return False
371370

372-
def get_cell_value_by_index(self, column_index, row_index):
373-
"""Gets the value for a single cell by column index and row index."""
371+
def get_cell_value_by_index(self, column_name, row_index):
372+
"""Gets the value for a single cell by column name and row index."""
374373

375-
column = self._column_index_to_name(column_index)
376-
if column is not None:
377-
return self._data['data'][row_index][column]
378-
379-
return None
374+
return self._data['data'][row_index][column_name]
380375

381-
def set_cell_value_by_index(self, column_index, row_index, value):
382-
"""Sets the value for a single cell by column index and row index.
376+
def set_cell_value_by_index(self, column_name, row_index, new_value):
377+
"""Sets the value for a single cell by column name and row index.
383378
384379
Note: This method returns a boolean to indicate if the operation
385380
was successful.
386381
"""
387382

388-
column = self._column_index_to_name(column_index)
389-
if column is not None and row_index >= 0 and row_index < len(self._data['data']):
390-
self._data['data'][row_index][column] = value
391-
self._notify_cell_change(row_index, column, value)
383+
if column_name in self._data['data'][row_index] and row_index >= 0 and row_index < len(self._data['data']):
384+
self._data['data'][row_index][column_name] = new_value
385+
self._notify_cell_change(row_index, column_name, new_value)
392386
return True
393387

394388
return False
@@ -603,3 +597,12 @@ def _get_row_index_of_primary_key(self, value):
603597
row_indices.append(i)
604598

605599
return row_indices
600+
601+
def _get_cell_value_by_numerical_index(self, column_index, row_index):
602+
"""Gets the value for a single cell by column index and row index."""
603+
604+
column = self._column_index_to_name(column_index)
605+
if column is not None:
606+
return self._data['data'][row_index][column]
607+
608+
return None

0 commit comments

Comments
 (0)