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

data and view not updating when using nested rows and columns #514

Open
TermeHansen opened this issue May 13, 2024 · 0 comments
Open

data and view not updating when using nested rows and columns #514

TermeHansen opened this issue May 13, 2024 · 0 comments

Comments

@TermeHansen
Copy link

Describe the bug
on cell change can be used to grab curtain changes and set to something else programmatically. This however does not work with nested rows and columns for some reason...

To Reproduce
Based on nested hierarchies example (https://github.com/bloomberg/ipydatagrid/blob/main/examples/Nested%20Hierarchies.ipynb):


import ipydatagrid as ipg
import pandas as pd
import numpy as np

# Columns
col_top_level = [
    "VeryLongValueFactors",
    "VeryLongValueFactors",
    "Even Longer Momentum Factors",
    "Even Longer Momentum Factors",
]
col_bottom_level = ["Factor_A", "Factor_B", "Factor_C", "Factor_D"]

# Rows
row_top_level = ["Sector 1", "Sector 1", "Sector 2", "Sector 2"]
row_bottom_level = ["Security A", "Security B", "Security C", "Security D"]

header_renderer = ipg.TextRenderer(
    background_color="moccasin",
    text_color="navy",
    vertical_alignment="top",
    horizontal_alignment="center",
)

default_renderer = ipg.TextRenderer(
    text_color=ipg.VegaExpr("cell.value <= 0 ? 'purple' : 'green'")
)

simple_df = pd.DataFrame(
    np.array([range(4)]*4),
)

nested_df = pd.DataFrame(
    np.array([range(4)]*4),
    columns=pd.MultiIndex.from_arrays([col_top_level, col_bottom_level]),
    index=pd.MultiIndex.from_arrays(
        [row_top_level, row_bottom_level], names=("Sector", "Ticker")
    ),
)

Expected behavior
Simple grid works as expected:

def on_cell_changed(cell):
    if float(cell['value']) == 15.0:
        simple_grid.set_cell_value_by_index(0,0, -15)
    print(
        "Cell at primary key {row} and column '{column}' ({column_index}) changed to {value}".format(
            row=cell["row"],
            column=cell["column"],
            column_index=cell["column_index"],
            value=cell["value"],
        )
    )

simple_grid = ipg.DataGrid(
    simple_df,
    layout={"height": "150px"},
    editable=True,
)
simple_grid.on_cell_change(on_cell_changed)
simple_grid

Change the first cell to 15, and it will be changed to -15 as well as the dataframe behind
image

Problem
Doing the same with the nested_grid does not work
def on_cell_changed(cell):

def on_cell_changed(cell):
    if float(cell['value']) == 15.0:
        nested_grid.set_cell_value_by_index(cell["row"],cell["column_index"], -15)
        print('set value')
    print(
        "Cell at primary key {row} and column '{column}' ({column_index}) changed to {value}".format(
            row=cell["row"],
            column=cell["column"],
            column_index=cell["column_index"],
            value=cell["value"],
        )
    )

nested_grid = ipg.DataGrid(
    nested_df,
    layout={"height": "150px"},
    editable=True,
)
nested_grid.on_cell_change(on_cell_changed)
nested_grid

image

Environment (please complete the following information):
Ubuntu 22.40
jupyterlab 4.2.0
ipydatagrid 1.3.1

@TermeHansen TermeHansen changed the title data and and view not updating when using nested rows and columns data and view not updating when using nested rows and columns May 13, 2024
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

1 participant