ipysheet icon indicating copy to clipboard operation
ipysheet copied to clipboard

Observe triggers too many times

Open krey opened this issue 6 years ago • 1 comments

Version: https://github.com/QuantStack/ipysheet/commit/5065a5975c48ae56ef1d86c61e5174435f64cb0d

import ipywidgets as widgets
import ipysheet

sheet = ipysheet.Sheet(rows=1, columns=1)
sheet.cells = ipysheet.Cell(value=[""], row_start=0, row_end=0, column_start=0, column_end=0, squeeze_column=False),

output = widgets.Output()

def observer(change):
    with output:
        print("{0} -> {1}".format(change.old, change.new))
        
sheet.cells[0].observe(observer, names='value')
sheet.cells[0].value = ['a']
sheet.cells[0].value = ['b']
sheet.cells[0].value = ['c']

output

output contains

[''] -> ['a']
['a'] -> ['b']
['b'] -> ['c']
['c'] -> ['a']
['a'] -> ['c']

Related: https://github.com/QuantStack/ipysheet/issues/85

krey avatar Apr 05 '19 21:04 krey

Another example:

In[0]:

import ipywidgets as widgets
import ipysheet

sheet = ipysheet.Sheet(rows=1, columns=2)

sheet.cells = (
    ipysheet.Cell(value=[""], row_start=0, row_end=0, column_start=0, column_end=0, squeeze_column=False),
    ipysheet.Cell(value=[""], row_start=0, row_end=0, column_start=1, column_end=1, squeeze_column=False)
)

output = widgets.Output()

def observer(change):
    with output:
        print("{0} -> {1}".format(change.old, change.new))

sheet.cells[0].value = ["A"]
sheet.cells[1].value = ["B"]

sheet.cells[0].observe(observer, names='value')
sheet.cells[1].observe(observer, names='value')
    
output

In[1]:

sheet.cells[0].value = ["C"]
sheet.cells[1].value = ["D"]

Run In[0] and then In[1]. output will then contain

['A'] -> ['C']
['B'] -> ['D']
['D'] -> ['B']
['B'] -> ['D']

Interestingly, if you replace In[1] with

sheet.cells[1].value = ["D"]
sheet.cells[0].value = ["C"]

then it works fine. output contains

['B'] -> ['D']
['A'] -> ['C']

Doesn't seem to happen if the cells are in two different sheets.

krey avatar Apr 08 '19 17:04 krey