ipydatagrid icon indicating copy to clipboard operation
ipydatagrid copied to clipboard

Not obvious how to get the data back out

Open jonschull opened this issue 2 years ago • 2 comments

Describe the bug I'm probably missing something.

If I modify the DataGrid, how do I get back the modified data (e.g, as a new or updated dataframe?)

To Reproduce DataGrid(df) --modify a few cells-- --examine df-- (not changed. That was my original reading of "two way databinding") --DataGrid.to_df()-- (is the kind if thing I'd expect.)

jonschull avatar Dec 25 '22 22:12 jonschull

I have this issue also:

#Import libraries 
import pandas as pd
pd.options.plotting.backend = "matplotlib"
import ipydatagrid as dg
import ipywidgets as ipw
import datetime as dt
import numpy as np

## Callbacks ##
def add_row(e):
    current_grid_df = grid.get_visible_data()
    additional_row = pd.DataFrame(
        columns = current_grid_df.columns, 
        index = [current_grid_df.index[-1] + 1],
        data = "" * len(current_grid_df.columns)
    )
    grid.data = current_grid_df.append(additional_row, ignore_index=True)
    
def remove_row(e):
    current_grid_df = grid.get_visible_data()
    grid.data = current_grid_df.drop(current_grid_df.tail(1).index)

#########################
##### DATA ##############
#########################
%load_ext SQL

%sql sqlite:///Durations.sqlite3
result = %sql SELECT * from DURATIONS;
df = result.DataFrame()
df["item_date"] = pd.to_datetime(df["item_date"])
#########################
#########################
#########################

## Widgets ##
grid = dg.DataGrid(df, selection_mode='cell', editable=True, layout={"height":"400px"})
button_add_row = ipw.Button(description="Add Row", 
                            style=ipw.ButtonStyle(button_color='darkgreen'))
button_remove_row = ipw.Button(description="Remove Row", 
                               style=ipw.ButtonStyle(button_color='darkred'))

## Handlers ##
button_add_row.on_click(add_row)
button_remove_row.on_click(remove_row)

## Layout ##
ipw.VBox([
    ipw.HBox([button_add_row, button_remove_row]),
    grid
])

The sql extension is already loaded. To reload it, use: %reload_ext sql

  • sqlite:///Durations.sqlite3 Done.

Modify values in last row of grid

df

Last row of dataframe is unmodified.

It would be really nice to have a little documentation in addition to the examples, as well.

GaryScottMartin avatar Dec 30 '22 07:12 GaryScottMartin

@jonschull and @GaryScottMartin dataframe used to create the datagrid and the data in the datagrid are not coupled or linked. The dataframe gets copied into the datagrid object. Changes in the datagrid will not affect the dataframe used to initiate the datagrid.

You can access the changed data by using .data instance variable of the datagrid object.

Example

In a jupyter notebook cell

import pandas as pd
from ipydatagrid import DataGrid
df = pd.DataFrame(
    {
        "column0":  ["", "", ""]
    }
)

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

In another cell

grid.set_cell_value("column0",0,"e-NABLE the future")
print(grid.data) #To access the updated data
print('\n',grid.data['column0'][0]) 

williamnavaraj avatar Jan 12 '23 12:01 williamnavaraj