3
3
import numpy as np
4
4
import json
5
5
6
+ from types import FunctionType
6
7
from IPython .display import display
7
8
from numbers import Integral
8
9
from traitlets import Unicode , Instance , Bool , Integer , Dict , List , Tuple , Any
@@ -179,7 +180,7 @@ def disable():
179
180
def show_grid (data_frame , show_toolbar = None ,
180
181
precision = None , grid_options = None ,
181
182
column_options = None , column_definitions = None ,
182
- row_edit_conditions = None ):
183
+ row_edit_callback = None ):
183
184
"""
184
185
Renders a DataFrame or Series as an interactive qgrid, represented by
185
186
an instance of the ``QgridWidget`` class. The ``QgridWidget`` instance
@@ -234,15 +235,14 @@ def show_grid(data_frame, show_toolbar=None,
234
235
"data_frame must be DataFrame or Series, not %s" % type (data_frame )
235
236
)
236
237
237
- row_edit_conditions = (row_edit_conditions or {})
238
238
column_definitions = (column_definitions or {})
239
239
240
240
# create a visualization for the dataframe
241
241
return QgridWidget (df = data_frame , precision = precision ,
242
242
grid_options = grid_options ,
243
243
column_options = column_options ,
244
244
column_definitions = column_definitions ,
245
- row_edit_conditions = row_edit_conditions ,
245
+ row_edit_callback = row_edit_callback ,
246
246
show_toolbar = show_toolbar )
247
247
248
248
@@ -366,6 +366,7 @@ class QgridWidget(widgets.DOMWidget):
366
366
_df_json = Unicode ('' , sync = True )
367
367
_primary_key = List ()
368
368
_columns = Dict ({}, sync = True )
369
+ _editable_rows = Dict ({}, sync = True )
369
370
_filter_tables = Dict ({})
370
371
_sorted_column_cache = Dict ({})
371
372
_interval_columns = List ([], sync = True )
@@ -391,7 +392,7 @@ class QgridWidget(widgets.DOMWidget):
391
392
grid_options = Dict (sync = True )
392
393
column_options = Dict (sync = True )
393
394
column_definitions = Dict ({})
394
- row_edit_conditions = Dict ( sync = True )
395
+ row_edit_callback = Instance ( FunctionType , sync = False , allow_none = True )
395
396
show_toolbar = Bool (False , sync = True )
396
397
397
398
def __init__ (self , * args , ** kwargs ):
@@ -573,6 +574,13 @@ def _update_table(self,
573
574
double_precision = self .precision )
574
575
575
576
self ._df_json = df_json
577
+
578
+ if self .row_edit_callback is not None :
579
+ editable_rows = {}
580
+ for index , row in df .iterrows ():
581
+ editable_rows [int (row [self ._index_col_name ])] = self .row_edit_callback (row )
582
+ self ._editable_rows = editable_rows
583
+
576
584
if fire_data_change_event :
577
585
data_to_send = {
578
586
'type' : 'update_data_view' ,
0 commit comments