Skip to content

Commit

Permalink
reworks SelectionContext class, also to make naming less confusing
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Biasuzzi <[email protected]>
  • Loading branch information
CBiasuzzi committed Jul 23, 2024
1 parent f415452 commit 52c35e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/pypowsybl_jupyter/networkexplorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def on_nadslider_changed(d):

def on_text_changed(d):
nonlocal found
sel_ctx.apply_filter_by_name(d['new'])
sel_ctx.apply_filter(d['new'])
found.value=None
found.options = sel_ctx.get_filtered_vls_as_list()
if sel_ctx.is_selected_in_filtered_vls():
Expand Down
34 changes: 14 additions & 20 deletions src/pypowsybl_jupyter/selectcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ class SelectContext:
def __init__(self, network:Network = None, vl_id : str = None, use_name:bool = True):
self.network = network
self.use_name = use_name
self.display_attribute = 'name' if use_name else 'id'

self.vls = network.get_voltage_levels(attributes=['name'])
self.vls['name'] = self.vls['name'].replace('', pd.NA).fillna(self.vls.index.to_series().astype(str))
self.vls['id'] = self.vls.index
if use_name:
self.vls['name'] = self.vls['name'].replace('', pd.NA).fillna(self.vls['id'])
else:
self.vls['name'] = self.vls.index

self.vls = self.vls.sort_values(by='name')

self.apply_filter_by_name(None)
self.vls = self.vls.sort_values(by=self.display_attribute) if use_name else self.vls.sort_index()

self.apply_filter(None)

self.set_selected(self.vls.iloc[0].id if vl_id is None else vl_id)
self.set_selected(self.vls.index[0] if vl_id is None else vl_id)

def get_vls(self):
return self.vls
Expand All @@ -40,23 +38,19 @@ def set_selected(self, id):
def get_selected(self):
return self.selected_vl

def apply_filter_by_name(self, sfilter):
def apply_filter(self, sfilter, search_attribute = None):
if sfilter is not None and sfilter != '':
vls_filtered = self.vls[self.vls['name'].str.contains(sfilter, case=False, na=False, regex=False)]
search_by = self.display_attribute if search_attribute is None else search_attribute
self.vls_filtered = self.vls[self.vls[search_by].str.contains(sfilter, case=False, na=False, regex=False)]
else:
vls_filtered = self.vls

self.vls_filtered_dict = OrderedDict(zip(vls_filtered.index, vls_filtered['name']))
self.vls_filtered = self.vls

def is_selected_in_filtered_vls(self):
return self.selected_vl in self.vls_filtered_dict
return self.selected_vl in self.vls_filtered.index

def get_filtered_vls_as_list(self):
names=list(self.vls_filtered_dict.values())
ids=list(self.vls_filtered_dict.keys())
name_id = list(zip(names,ids))
return name_id
return list(zip(self.vls_filtered[self.display_attribute].values.tolist(), self.vls_filtered.index))

def extend_filtered_vls(self, id):
if (id in self.vls.index) and (id not in self.vls_filtered_dict):
self.vls_filtered_dict[id]=self.vls.loc[id, 'name']
if (id in self.vls.index) and (id not in self.vls_filtered.index):
self.vls_filtered = pd.concat([self.vls_filtered, self.vls.loc[[id]]])

0 comments on commit 52c35e5

Please sign in to comment.