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

WIP: Demonstrate the use of widgets with a basic explorer #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions pypowsybl_jupyter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from .svg import SvgWidget, display_svg
from .explorer import network_explorer
from ._version import __version__, version_info


def _jupyter_labextension_paths():
"""Called by Jupyter Lab Server to detect if it is a valid labextension and
to install the widget
Expand Down
51 changes: 51 additions & 0 deletions pypowsybl_jupyter/explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) 2022, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from IPython.display import display
from pypowsybl.network import Network
from pypowsybl_jupyter.svg import display_svg

import ipywidgets as widgets


def network_explorer(network: Network):
"""
Creates a basic explorer widget for this network.
"""

vl_input = widgets.Text(
value='',
placeholder='Voltage level ID',
description='Voltage level:',
disabled=False,
continuous_update=True
)

found = widgets.SelectMultiple(
options=[],
value=[],
rows=10,
description='Found',
disabled=False
)

def on_text_changed(d):
vls = network.get_voltage_levels(attributes=[])
vls = vls[vls.index.str.startswith(d['new'])]
found.options = vls.index

vl_input.observe(on_text_changed, names='value')

button = widgets.Button(description="Display voltage level")
diagram_panel = widgets.Output()

def on_click(_):
with diagram_panel:
diagram_panel.clear_output()
display(display_svg(network.get_single_line_diagram(found.value[0])))

button.on_click(on_click)
left_panel = widgets.VBox([vl_input, found, button])
return widgets.HBox([left_panel, diagram_panel])