Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not obvious how to get the data back out #385

Open
jonschull opened this issue Dec 25, 2022 · 2 comments
Open

Not obvious how to get the data back out #385

jonschull opened this issue Dec 25, 2022 · 2 comments

Comments

@jonschull
Copy link

jonschull commented Dec 25, 2022

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.)

@GaryScottMartin
Copy link

GaryScottMartin commented Dec 30, 2022

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.

@williamnavaraj
Copy link

williamnavaraj commented Jan 12, 2023

@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]) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants