jupyter-widgets-contrib / ipysheet

Jupyter handsontable integration
MIT License
543 stars 68 forks source link

to_dataframe() and to_array() do not return values manually entered into the cells #220

Closed giswqs closed 2 years ago

giswqs commented 2 years ago

The to_dataframe() and to_array() functions can only return values added the the cells programmatically. Any values entered manually on the sheet are not passed to the dataframe/array. This makes ipysheet much less usable if it can't capture values entered manually.

Related issue: #194

import ipysheet
sheet = ipysheet.sheet(rows=2, columns=2)
cell1 = ipysheet.cell(0, 0, 'Hello')
cell2 = ipysheet.cell(1, 0, 'World')
sheet

Peek 2022-01-27 09-01

giswqs commented 2 years ago

It seems that if the sheet is constructed using from_dataframe(), the to_dataframe() works properly.

Peek 2022-01-27 09-18

martinRenou commented 2 years ago

IIRC you need to specifically create the cells if you need them to be synced between front-end/back-end.

from_dataframe might create the cells objects for you, but if you don't use from_dataframe you'll probably have to create the cells objects by yourself.

giswqs commented 2 years ago

My question is: how to capture the values entered into the cells if I have to create the cells objects manually?

martinRenou commented 2 years ago

I don't understand the question?

I you want to be able to capture the values entered into the cells, you need the cells connection to actually exist. We could agree on the fact that it's not a user-friendly behavior, but I guess that was for optimization purpose (not creating useless cells objects, each cell being a widget).

giswqs commented 2 years ago

My goal is to create a tool that can capture values entered by user and pass those values to properties of user draw features. That's why I need to use to_dataframe() to get the cell values.

Peek 2022-01-27 09-38

martinRenou commented 2 years ago

Sure :) You can do this by creating the cell-range objects manually when creating the sheet, then if the user enters something, you will know about it and be able to use to_dataframe

giswqs commented 2 years ago

I just tested the cell-range example. It does not work for to_dataframe().

https://ipysheet.readthedocs.io/en/stable/#Cell-ranges image

martinRenou commented 2 years ago

Not sure what's happening with this code example, what happens if you make a simpler table?

sheet = ipysheet.sheet(rows=1, columns=2)
row = ipysheet.row(0, [3.4, 5.6])
giswqs commented 2 years ago

The cell-range trick works. I need to add the the cell-range with empty strings. Otherwise, it won't work.

Peek 2022-01-27 09-58

giswqs commented 2 years ago

@martinRenou Thanks for you help. Closing it for now.

giswqs commented 2 years ago

Just a reference for future readers: the follow code snippet will create a sheet that works for to_dataframe() and to_array().

import ipysheet
rows = 3
columns = 2
sheet = ipysheet.sheet(rows=rows, columns=columns)
for i in range(columns):
    ipysheet.column(i, [""] * sheet.rows)
sheet

# ipysheet.to_dataframe(sheet)
# ipysheet.to_array(sheet)