Avaiga / taipy-gui

Graphical User Interface generator for Taipy
Apache License 2.0
60 stars 18 forks source link

BUG- When a table is filtered, `on_action()` does not pass the correct row number of the original DataFrame #835

Closed Dr-Irv closed 1 year ago

Dr-Irv commented 1 year ago

Description If a user filters a table, and an on_action() callback is set on the table, then the payload["index"] contains the row number of the filtered table, but there is no way to know what real row that is in the original DataFrame.

We are pretty sure that the same error occurs with on_edit() as well.

How to reproduce

from typing import Any

import pandas as pd

from taipy.gui import Gui, Markdown, State

table1 = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})

indexval = -1
xval = "-1"
yval = "-1"

page = Markdown(
    """

<|{table1}|table|filter=True|rebuild=True|width=fit-content|on_action=on_action|>

<|Indexval|text|> <|{indexval}|text|>

<|xval|text|> <|{xval}|text|>

<|yval|text|> <|{yval}|text|>

"""
)

def on_action(state: State, var_name: str, action: str, payload: dict[str, Any]):
    indexval = payload["index"]
    xval = state.table1.iloc[indexval]["x"]
    state.xval = str(xval)
    yval = state.table1.iloc[indexval]["y"]
    state.yval = str(yval)
    state.indexval = indexval

if __name__ == "__main__":
    gui = Gui(page=page)
    gui.run(title="Filter Action")

Sequence of events:

Expected behavior The row number should be of the filtered DataFrame, or there should be some way to know what rows remain after filtering.

Runtime environment Please specify relevant indications.

FlorianJacta commented 1 year ago

Good find! Using the index in the payload seems impossible as it corresponds to the filtered data.