Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

displayed VLs history #17

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/_static/img/getting_started_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/img/network_explorer_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/img/network_explorer_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/img/network_explorer_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/user_guide/network_explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Network explorer is interactive network explorer widget, built on pypowsybl-jupyter's widgets (SLD, NAD, Network map) and some standard [ipywidgets](https://ipywidgets.readthedocs.io/en/stable/index.html): select lists, tabs, etc.

Through the widget, you can select a voltage level from the list (or search of a specific one using the Filter) and the NAD and SLD diagrams for that voltage level will be displayed in the two "Network Area" and "Single Line" tabs, respectively. Both diagrams can be panned and zoomed. A third tab, 'Network map' displays the network's substations and lines on a map, if substations and lines geo data is available in the network.
Through the widget, you can select a voltage level from the list (or search of a specific one using the Filter) and the NAD and SLD diagrams for that voltage level will be displayed in the two "Network Area" and "Single Line" tabs, respectively. Both diagrams can be panned and zoomed. A third tab, 'Network map' displays the network's substations and lines on a map, if substations and lines geo data is available in the network. The last displayed voltage levels are listed in a history box, on the explorer's bottom left corner.

The following code, to be run in a notebook, first creates a network, then displays the network explorer on it.

Expand Down
48 changes: 27 additions & 21 deletions src/pypowsybl_jupyter/networkexplorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,26 @@ def network_explorer(network: Network, vl_id : str = None, use_name:bool = True,

def go_to_vl(event: any):
arrow_vl= str(event.clicked_nextvl)
sel_ctx.set_selected(arrow_vl)
sel_ctx.set_selected(arrow_vl, add_to_history=True)
update_select_widget(history, sel_ctx.get_selected(), sel_ctx.get_history_as_list(), on_selected_history)
update_select_widget(found, None, None, on_selected)
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
update_explorer()


def toggle_switch(event: any):
idswitch = event.clicked_switch.get('id')
statusswitch = event.clicked_switch.get('switch_status')
network.update_switches(id=idswitch, open=statusswitch)
update_sld_diagram(sel_ctx.get_selected(), True)
update_nad_diagram(sel_ctx.get_selected())

def go_to_vl_from_map(event: any):
vl_from_map= str(event.selected_vl)
sel_ctx.set_selected(vl_from_map, add_to_history=True)
update_select_widget(history, sel_ctx.get_selected(), sel_ctx.get_history_as_list(), on_selected_history)
update_select_widget(found, None, None, on_selected)
#switch to the SLD tab
tabs_diagrams.selected_index=1
flo-dup marked this conversation as resolved.
Show resolved Hide resolved

def update_nad_diagram(el):
nonlocal nad_widget
if el is not None:
Expand All @@ -97,16 +106,15 @@ def update_sld_diagram(el, kv: bool = False):
else:
update_sld(sld_widget, sld_diagram_data, keep_viewbox=kv, enable_callbacks=True)

def update_map():
def update_map(el):
nonlocal map_widget
if sel_ctx.get_selected() is not None:
if el is not None:
if map_widget==None:
map_widget=NetworkMapWidget(network, use_name=use_name, nominal_voltages_top_tiers_filter = nominal_voltages_top_tiers_filter, use_line_geodata = use_line_geodata)
map_widget=NetworkMapWidget(network, use_name=use_name, nominal_voltages_top_tiers_filter = nominal_voltages_top_tiers_filter)
map_widget.on_selectvl(lambda event : go_to_vl_from_map(event))

else:
map_widget.center_on_voltage_level(sel_ctx.get_selected())

map_widget.center_on_voltage_level(el)
nadslider = widgets.IntSlider(value=selected_depth, min=0, max=20, step=1, description='depth:', disabled=False,
continuous_update=False, orientation='horizontal', readout=True, readout_format='d')

Expand Down Expand Up @@ -152,15 +160,16 @@ def on_text_changed(d):

found = widgets.Select(
options=sel_ctx.get_filtered_vls_as_list(),
value=sel_ctx.get_selected(),
value=None,
description='Found',
disabled=False,
layout=widgets.Layout(flex='80%', height='100%', width='350px', margin='0 0 0 0')
)

def on_selected(d):
if d['new'] != None:
sel_ctx.set_selected(d['new'])
sel_ctx.set_selected(d['new'], add_to_history=True)
update_select_widget(history, None, sel_ctx.get_history_as_list(), on_selected_history)
update_explorer()

found.observe(on_selected, names='value')
Expand All @@ -175,23 +184,19 @@ def on_selected(d):

def on_selected_history(d):
if d['new'] != None:
sel_ctx.set_selected(d['new'])
sel_ctx.set_selected(d['new'], add_to_history=False)
update_select_widget(found, None, None, on_selected)
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
update_explorer()

history.observe(on_selected_history, names='value')

def update_explorer():
sel=sel_ctx.get_selected()

update_select_widget(found, sel if sel_ctx.is_selected_in_filtered_vls() else None, None, on_selected)
update_select_widget(history, sel, sel_ctx.get_history_as_list(), on_selected_history)

update_nad_diagram(sel)
update_sld_diagram(sel)
update_map(sel)


update_nad_diagram(sel_ctx.get_selected())
update_sld_diagram(sel_ctx.get_selected())
update_explorer()

voltage_levels_label=widgets.Label("Voltage levels")
spacer_label=widgets.Label("")
Expand All @@ -200,15 +205,16 @@ def update_explorer():

right_panel_nad = widgets.VBox([nadslider, nad_widget])
right_panel_sld = widgets.VBox([spacer_label,sld_widget])
right_panel_map = widgets.VBox([spacer_label, map_widget])

tabs_diagrams = widgets.Tab()
tabs_diagrams.children = [right_panel_nad, right_panel_sld]
tabs_diagrams.titles = ['Network Area', 'Single Line']
tabs_diagrams.children = [right_panel_nad, right_panel_sld, right_panel_map]
tabs_diagrams.titles = ['Network Area', 'Single Line', 'Network map']
tabs_diagrams.layout=widgets.Layout(width='850px', height='700px', margin='0 0 0 4px')

left_vbox = widgets.VBox([voltage_levels_label, left_panel])
right_vbox = widgets.VBox([spacer_label, tabs_diagrams])

hbox = widgets.HBox([left_vbox, right_vbox])

return hbox
return hbox
9 changes: 3 additions & 6 deletions src/pypowsybl_jupyter/selectcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def __init__(self, network:Network = None, vl_id : str = None, use_name:bool = T
def get_vls(self):
return self.vls

def set_selected(self, id):
def set_selected(self, id, add_to_history=True):
if id in self.vls.index:
self.selected_vl = id
self.add_to_history(id)
if add_to_history:
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
self.add_to_history(id)
else:
raise ValueError(f'a voltage level with id={id} does not exist in the network.')

Expand All @@ -61,10 +62,6 @@ def extend_filtered_vls(self, id):
def add_to_history(self, id):
if (id in self.vls.index):
row_to_add = self.vls.loc[id].to_dict()
for item in self.history:
if item['id'] == row_to_add['id']:
self.history.remove(item)
break
self.history.appendleft(row_to_add)

def get_history_as_list(self):
Expand Down
Loading