pythonjupyter-notebookipywidgets

How to work with two ipysheets at the same time. accesing cell atributes


I want to render two ipysheets in a particular notebook as an example assume I have:

mysheet1 = ipysheet.sheet(rows=3, columns=4)
mysheet2 = ipysheet.sheet(rows=3, columns=4)

If I now do:

cell1 = ipysheet.cell(0, 0, 'Hello')
cell2 = ipysheet.cell(2, 0, 'World')
cell_value = ipysheet.cell(2,2, 42.)

the values are changed in sheet 2. This is really weird. why is that that the last sheet created is modified?

why is following not working?

cell1 = mysheet1.cell(0, 0, 'Hello')

nor

mysheet1.cell(0, 0, 'Hello')

I would assume that since all is an object in pyhton ipysheet is also an object but it "cell" is not an atribute of mysheet1.

any plausible explanation?

This is actually extendable to column atributes.


Solution

  • This is because, cell adds a new Cell widget to the current Sheet according to the docs. One way that worked for me was to assign Cell to the attributes cells of the first sheet as a tuple. That is,

    from ipysheet import sheet, current, cell, Cell
    
    s1 = sheet(rows=3, columns=4)
    s2 = sheet(rows=3, columns=4)
    
    cell1 = cell(0, 0, 'Hello', )
    cell2 = cell(2, 0, 'World', )
    
    s1.cells = (Cell(column_end=0, column_start=0, row_end=1, row_start=1, type='text', value='Hello'), Cell(column_end=0, column_start=0, row_end=0, row_start=0, type='text', value='Hello'))
    

    and one has enter image description here

    but I think it is not a practical way to assign the cells like this, it would be easier if you first work with s1 and then with s2.