jupyter-widgets / ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
BSD 3-Clause "New" or "Revised" License
579 stars 51 forks source link

Callback issues #386

Open williamnavaraj opened 1 year ago

williamnavaraj commented 1 year ago

Describe the bug Removing ipydatagrid on_cell_change callback (with remove==True flag) and adding it back seems to not work correctly. To Reproduce Steps to reproduce the behavior: Run the below code. (I used VSCode notebook)

import pandas as pd
from ipydatagrid import DataGrid
def on_cell_changed(cell):

    message="Cell at primary key {row} and column '{column}'({column_index}) changed to {value}".format(
            row=cell["row"],
            column=cell["column"],
            column_index=cell["column_index"],
            value=cell["value"]

        )

    print(message)#,flush=True)
    #Tkinter box for better visualization of what is going on
    import tkinter.messagebox
    tkinter.messagebox.Message(title='On cell changed', message= message,).show()

df = pd.DataFrame(
    {
        "column0":  ["", "", ""]
    }
)

datagrid=DataGrid(
    df,editable=True
, layout={"height": "100px"})
display(datagrid)

datagrid.on_cell_change(on_cell_changed)
datagrid.set_cell_value("column0",0,"a")
datagrid.set_cell_value("column0",1,"b")
datagrid.set_cell_value("column0",2,"c") #Upto this works fine
datagrid.on_cell_change(on_cell_changed,True) #This is supposed to remove the callback
datagrid.set_cell_value("column0",0,"e")
datagrid.set_cell_value("column0",1,"f")
datagrid.set_cell_value("column0",2,"g")
#If I uncomment the below line the output for callbacks for the value changes of all 6 cases above displays
#datagrid.on_cell_change(on_cell_changed) 

The code above provides the Minimum example to replicate the code. I am running this in a jupyter notebook cell in VSCode 1.74.1.

Expected behavior

Cell at primary key 0 and column 'column0'(0) changed to a
Cell at primary key 1 and column 'column0'(0) changed to b
Cell at primary key 2 and column 'column0'(0) changed to c

Actual output

Cell at primary key 0 and column 'column0'(0) changed to a
Cell at primary key 1 and column 'column0'(0) changed to b
Cell at primary key 2 and column 'column0'(0) changed to c
Cell at primary key 0 and column 'column0'(0) changed to e Cell at primary key 0 and column 'column0'(0) changed to e Cell at primary key 1 and column 'column0'(0) changed to f Cell at primary key 0 and column 'column0'(0) changed to e Cell at primary key 1 and column 'column0'(0) changed to f Cell at primary key 2 and column 'column0'(0) changed to g

The tkinter message box shows all 6 cases are occurring again.