from nicegui import ui class aggridx(ui.aggrid): # #TODO: this should be done in the constructor via a keyword argument switch # #TODO: this would need the from_pandas/from_polars classmethods to pass through that switch # def setup_sync_edits(self): # """ # automatically sync data to the server after edits # this operation maintains the current view on the data # """ # self.on("cellValueChanged", self.set_cell_server) def set_cell_server(self, row_index, col_id, new_val): with self.props.suspend_updates(): self.options["rowData"][row_index][col_id] = new_val def set_cell_client(self, row_id, col_id, new_val): self.run_row_method(row_id, "setDataValue", col_id, new_val) # def set_row_server_from_event(self, evt): # index = evt.args["rowIndex"] # data = evt.args["data"] # self.set_row_server(index, data) # def set_row_server(self, index, data): # """ # overwrite row at index with data # this operation maintains the current view on the data # """ # with self.props.suspend_updates(): # self.options["rowData"][index] = data def append(self, row): """ append row to the bottom of the table and append any missing columns on the right side of the table this operation maintains the current view on the data """ self.ensure_column_defs(row) self.add_row_data([row]) # def insert(self, index, row): # """ # insert row at the provided index # append any missing columns on the right side of the table # this operation maintains the current view on the data # """ # self.ensure_column_defs(row) # self.add_row_data([row], index=index) # def extend(self, rows): # """ # append rows to the bottom of the table and # append any missing columns on the right side of the table # this operation maintains the current view on the data # """ # columns = dict.fromkeys(k for d in rows for k in d) # dict.fromkeys acts as ordered set # self.ensure_column_defs(columns) # self.add_row_data(rows) def ensure_column_defs(self, columns): """ append any missing columns on the right side of the table this operation maintains the current view on the data """ current_column_defs = self.options["columnDefs"] current_fields = [i["field"] for i in current_column_defs] added_fields = [i for i in columns if i not in current_fields] if not added_fields: return added_column_defs = [{"field": n} for n in added_fields] new_column_defs = current_column_defs + added_column_defs # update server without re-draw with self.props.suspend_updates(): self.options["columnDefs"] = new_column_defs # update client self.run_grid_method("setGridOption", "columnDefs", new_column_defs) def add_row_data(self, rows, index=-1): """ insert rows at the provided index, with the default being appending at the bottom of the table this operation maintains the current view on the data """ # update server without re-draw with self.props.suspend_updates(): self.options["rowData"].extend(rows) # update client self.run_grid_method("applyTransaction", {"add": rows, "addIndex": index}) # def scroll_to_row_index(self, index): # """ # scroll the view on the data such that the row with the given index is visible # negative indices count from the back # """ # nrows = len(self.options["rowData"]) # index %= nrows # move index into [0, nrows) # self.run_grid_method("ensureIndexVisible", index) # def scroll_to_row_key(self, key): # """ # scroll the view on the data such that the row with the given key is visible # consistent with tables generated from dataframes, the column called index is used to match the key # if the key is not found, the view is not scrolled # """ # indices = [i.get("index") for i in self.options["rowData"]] # try: # index = indices.index(key) # except ValueError: # return # self.scroll_to_row_index(index) # def scroll_to_column_index(self, index): # """ # scroll the view on the data such that the column with the given index is visible # negative indices count from the back # """ # key = self.options["columnDefs"][index]["field"] # self.scroll_to_column_key(key) # def scroll_to_column_key(self, key): # """ # scroll the view on the data such that the column with the given key is visible # """ # self.run_grid_method("ensureColumnVisible", key)