Skip to content

Commit

Permalink
put a frame around the preview, prepare new plugins, fix bug in regex…
Browse files Browse the repository at this point in the history
… w/ index
  • Loading branch information
nickzoic committed Aug 31, 2023
1 parent a97ca1c commit 8d54aec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
16 changes: 14 additions & 2 deletions countess/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down
9 changes: 6 additions & 3 deletions countess/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -125,6 +126,7 @@ def show_config_subframe(self):
self.config_canvas.bind("<Configure>", 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, *_):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 "):
Expand Down Expand Up @@ -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("<Configure>", self.on_frame_configure, add=True)
Expand Down
7 changes: 6 additions & 1 deletion countess/plugins/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 8d54aec

Please sign in to comment.