From 8d54aec8cf7bd897aeb0608218147f4c473ceff9 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 31 Aug 2023 16:26:06 +1000 Subject: [PATCH] put a frame around the preview, prepare new plugins, fix bug in regex w/ index --- countess/core/plugins.py | 16 ++++++++++++++-- countess/gui/main.py | 9 ++++++--- countess/plugins/regex.py | 7 ++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/countess/core/plugins.py b/countess/core/plugins.py index 6873b36..7aee1ed 100644 --- a/countess/core/plugins.py +++ b/countess/core/plugins.py @@ -240,6 +240,12 @@ def process_dataframe(self, dataframe: pd.DataFrame, logger: Logger) -> Optional """Override this to process a single dataframe""" raise NotImplementedError(f"{self.__class__}.process_dataframe()") + def finalize(self, logger: Logger) -> Iterable[pd.DataFrame]: + for p in self.parameters.values(): + p.set_column_choices(self.input_columns.keys()) + + return super().finalize(logger) + class PandasProductPlugin(PandasProcessPlugin): """Some plugins need to have all the data from two sources presented to them, @@ -428,8 +434,14 @@ def __init__(self, *a, **k): assert all(isinstance(pp["name"], StringParam) for pp in self.parameters["output"]) def series_to_dataframe(self, series: pd.Series) -> pd.DataFrame: - column_names = [pp.name.value for pp in self.parameters["output"]] # type: ignore [attr-defined] - df = pd.DataFrame(series.tolist(), columns=column_names, index=series.index) + column_names = [ + pp.name.value or "Column %d" % n + for n, pp in enumerate(self.parameters["output"], 1) + ] # type: ignore [attr-defined] + + data = series.tolist() + column_names = column_names[:len(data[0])] + df = pd.DataFrame(data, columns=column_names, index=series.index) return df diff --git a/countess/gui/main.py b/countess/gui/main.py index ad6b883..b32845c 100644 --- a/countess/gui/main.py +++ b/countess/gui/main.py @@ -115,6 +115,7 @@ def show_config_subframe(self): # self.node.plugin.update() self.configurator = PluginConfigurator(self.config_canvas, self.node.plugin, self.config_change_callback) self.config_subframe = self.configurator.frame + else: self.config_subframe = PluginChooserFrame(self.config_canvas, "Choose Plugin", self.choose_plugin) self.config_subframe_id = self.config_canvas.create_window((0, 0), window=self.config_subframe, anchor=tk.NW) @@ -125,6 +126,7 @@ def show_config_subframe(self): self.config_canvas.bind("", self.on_config_canvas_configure) def on_config_canvas_configure(self, *_): + self.config_canvas.itemconfigure(self.config_subframe_id, width=self.config_canvas.winfo_width()) def on_label_configure(self, *_): @@ -160,7 +162,7 @@ def show_preview_subframe(self): else: try: df = concat_dataframes(self.node.result) - self.preview_subframe = TabularDataFrame(self.frame) + self.preview_subframe = TabularDataFrame(self.frame, highlightbackground="black", highlightthickness=3) self.preview_subframe.set_dataframe(df) except (TypeError, ValueError): self.preview_subframe = tk.Frame(self.frame) @@ -208,6 +210,7 @@ def config_change_task_callback_2(self): def choose_plugin(self, plugin_class): self.node.plugin = plugin_class() + self.node.prerun(self.logger) self.node.is_dirty = True self.show_config_subframe() if self.node.name.startswith("NEW "): @@ -272,8 +275,8 @@ def __init__(self, tk_parent: tk.Widget, config_filename: Optional[str] = None): self.subframe.rowconfigure(0, weight=0) self.subframe.rowconfigure(1, weight=0) self.subframe.rowconfigure(2, weight=0) - self.subframe.rowconfigure(3, weight=1) - self.subframe.rowconfigure(4, weight=2) + self.subframe.rowconfigure(3, weight=2) + self.subframe.rowconfigure(4, weight=1) self.subframe.rowconfigure(5, weight=0) self.frame.bind("", self.on_frame_configure, add=True) diff --git a/countess/plugins/regex.py b/countess/plugins/regex.py index d46df9a..252b20a 100644 --- a/countess/plugins/regex.py +++ b/countess/plugins/regex.py @@ -47,6 +47,7 @@ class RegexToolPlugin(PandasTransformSingleToTuplePlugin): compiled_re = None def prepare(self, sources: list[str], row_limit: Optional[int] = None): + super().prepare(sources, row_limit) self.compiled_re = re.compile(self.parameters["regex"].value) def process_dataframe(self, dataframe: pd.DataFrame, logger: Logger) -> pd.DataFrame: @@ -62,7 +63,11 @@ def process_dataframe(self, dataframe: pd.DataFrame, logger: Logger) -> pd.DataF if column_name in df.columns: df = df.drop(columns=column_name) else: - df = df.reset_index(column_name, drop=True) + # XXX maybe set up a helper function for this + try: + df = df.reset_index(column_name, drop=True) + except KeyError: + pass index_names = [pp.name.value for pp in self.parameters["output"] if pp.index.value] if index_names: