class DataFrameManagerWidget(widgets.Splitter):
def __init__(self, object_name: str = "pandas_df_manager", **kwargs):
super().__init__(object_name=object_name, **kwargs)
self.dataframe_list = pandaswidgets.DataFrameListWidget()
self.dataframe_editor = pandaswidgets.DataFrameWidget()
self.dataframe_list.selectionModel().currentRowChanged.connect(
self._on_current_change
)
self._current_row: int | None = None
self.dataframe_editor.holder.changed.connect(self._on_editor_df_changed)
self.add(self.dataframe_list)
widget = widgets.Widget()
widget.set_layout("horizontal")
widget.box.add(self.dataframe_editor)
self.add(widget)
def add_df(self, df: pd.DataFrame):
self.dataframe_list.add_df(df)
def _on_current_change(self, new, old):
if not new.isValid():
return
# The list model stores the frames; its columns only provide display data.
df = self.dataframe_list.model().items[new.row()]
self._current_row = None # loading a frame is not an edit
self.dataframe_editor.set_df(df)
self._current_row = new.row()
def _on_editor_df_changed(self, origin=None):
"""Store the edited frame in the list, replacing the one it came from."""
if self._current_row is None:
return
model = self.dataframe_list.model()
index = model.index(self._current_row, 0)
model.setData(index, self.dataframe_editor.df, constants.USER_ROLE)