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

Uses @powsybl/diagram-viewer to build jupyter notebook widgets (updated) #4

Merged
merged 19 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
13 changes: 6 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ jobs:
- name: Install node
uses: actions/setup-node@v1
with:
node-version: "16.x"
node-version: "18.x"
- name: Install Python
uses: actions/setup-python@v2
with:
python-version: "3.7"
python-version: "3.11"
architecture: "x64"

- name: Setup pip cache
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: pip-3.7-${{ hashFiles('package.json') }}
key: pip-3.11-${{ hashFiles('package.json') }}
restore-keys: |
pip-3.7-
pip-3.11-
pip-

- name: Get npm cache directory
Expand All @@ -44,12 +44,11 @@ jobs:

- name: Install dependencies
run: |
python -m pip install -U pip setuptools codecov
python -m pip install -U codecov
npm install -g codecov
- name: Test the extension
run: |
python -m pip install --upgrade -v -e ".[test]"
yarn run lint:check

pytest
python -m pytest
yarn run test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
.yarn/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -158,3 +159,4 @@ pypowsybl_jupyter/labextension

# Intellij files
.idea/
*.iml
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,55 @@

Widgets for [pypowsybl](https://github.com/powsybl/pypowsybl) in the Jupyter notebook.


## Installation

You can install using `pip`:

```bash
pip install pypowsybl-jupyter
pip install pypowsybl_jupyter
```

If you are using Jupyter Notebook 5.2 or earlier, you may also need to enable
the nbextension:
```bash
jupyter nbextension enable --py [--sys-prefix|--user|--system] pypowsybl-jupyter
jupyter nbextension enable --py [--sys-prefix|--user|--system] pypowsybl_jupyter
```

## Examples

In the examples directory there are some notebooks demonstrating the widgets.


## Development Installation

Create a dev environment:
```bash
conda create -n pypowsybl_jupyter-dev -c conda-forge nodejs python jupyterlab
conda activate pypowsybl_jupyter-dev
```

Install the python. This will also build the TS package.
```bash
pip install -e ".[test]"
```


### Note

When developing your extensions, you need to manually enable your extensions with the
notebook / lab frontend. For lab, this is done by the command:

```
jupyter labextension develop --overwrite .
yarn run build
jlpm run build
```

For classic notebook, you need to run:

```
jupyter nbextension install --sys-prefix --symlink --overwrite --py ipysvg
jupyter nbextension enable --sys-prefix --py ipysvg
jupyter nbextension install --sys-prefix --symlink --overwrite --py pypowsybl_jupyter
jupyter nbextension enable --sys-prefix --py pypowsybl_jupyter
```

Note that the `--symlink` flag doesn't work on Windows, so you will here have to run
Expand All @@ -45,20 +60,17 @@ of those flags here.

### How to see your changes
#### Typescript:
If you use JupyterLab to develop then you can watch the source directory and run JupyterLab
at the same time in different terminals to watch for changes in the extension's source and
automatically rebuild the widget.
If you use JupyterLab to develop then you can watch the source directory and run JupyterLab at the same time in different
terminals to watch for changes in the extension's source and automatically rebuild the widget.

```bash
# Watch the source directory in one terminal, automatically rebuilding when needed
yarn run watch
jlpm run watch
# Run JupyterLab in another terminal
jupyter lab
```

After a change wait for the build to finish and then refresh your browser and the changes
should take effect.
After a change wait for the build to finish and then refresh your browser and the changes should take effect.

#### Python:
If you make a change to the python code then you will need to restart the notebook kernel
to have it take effect.
If you make a change to the python code then you will need to restart the notebook kernel to have it take effect.
61 changes: 61 additions & 0 deletions examples/demo_simple.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b6d17e9b-2a42-481b-903c-a8761f1fe829",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pypowsybl_jupyter import display_svg\n",
"import pypowsybl.network as pn"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a41c8d0d-1340-400b-ab05-5e33657c4bf0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"network = pn.create_ieee9()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4aeb5de0-94a1-464e-b561-fe0a90fc27f4",
"metadata": {},
"outputs": [],
"source": [
"#display an SVG (e.g., a single-line-diagram svg), with pan and zoom\n",
"display_svg(network.get_single_line_diagram(network.get_voltage_levels().index[1]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
64 changes: 64 additions & 0 deletions examples/demo_simple_explorer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b6d17e9b-2a42-481b-903c-a8761f1fe829",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pypowsybl_jupyter import network_explorer\n",
"import pypowsybl.network as pn"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a41c8d0d-1340-400b-ab05-5e33657c4bf0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"network = pn.create_ieee9()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4aeb5de0-94a1-464e-b561-fe0a90fc27f4",
"metadata": {},
"outputs": [],
"source": [
"#Display a simple explorer widget for the network\n",
"#built by assembling the SvgView SVG viewer and other basic ipywidgets.\n",
"#Start typing some prefix characters for the ID in the text box (e.g., VL)\n",
"#then select one from the list of filtered VLs and click the button to update the diagram.\n",
"network_explorer(network)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
96 changes: 96 additions & 0 deletions examples/demo_sld.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b6d17e9b-2a42-481b-903c-a8761f1fe829",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pypowsybl_jupyter import display_sld_svg\n",
"import ipywidgets as widgets\n",
"import pypowsybl as pp"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a41c8d0d-1340-400b-ab05-5e33657c4bf0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"network = pp.network.create_four_substations_node_breaker_network()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4aeb5de0-94a1-464e-b561-fe0a90fc27f4",
"metadata": {},
"outputs": [],
"source": [
"#declare some example callbacks\n",
"def test_nextvl_callback(event):\n",
" print('Clicked a VL arrow, next VL is: ' + event.clicked_nextvl)\n",
"def test_switch_callback(event):\n",
" print('Clicked a switch: ' + str(event.clicked_switch))\n",
"def test_feeder_callback(event):\n",
" print('Clicked a feeder: ' + str(event.clicked_feeder)) \n",
"def test_bus_callback(event):\n",
" print('Clicked a bus: ' + str(event.clicked_bus)) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ed51a20-f1d5-4792-9625-c3fccf5e2f0f",
"metadata": {},
"outputs": [],
"source": [
"#define an SLD for a VL, using the display_sld_widget from pypowsybl_jupyter\n",
"#and activate the callbacks on VL arrows, switches and feeders\n",
"sld_widget=display_sld_svg(network.get_single_line_diagram(network.get_voltage_levels().index[1]), enable_callbacks=True)\n",
"sld_widget.on_nextvl(test_nextvl_callback)\n",
"sld_widget.on_switch(test_switch_callback)\n",
"sld_widget.on_feeder(test_feeder_callback)\n",
"sld_widget.on_bus(test_bus_callback)\n",
"\n",
"\n",
"#define a text box, using one of the standard ipywidgets\n",
"sld_nextvl_text = widgets.Text()\n",
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"#link the text box content to the widget's clicked_nextvl value, so that its content is automatically updated\n",
"#when a VL arrow is clicked\n",
"mylink = widgets.jslink((sld_widget, 'clicked_nextvl'), (sld_nextvl_text, 'value'))\n",
"\n",
"#finally, display the two widgets\n",
"widgets.VBox([widgets.HBox([widgets.Label(value='Voltage Level:'), sld_nextvl_text]), sld_widget])"
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading