Skip to content

Commit

Permalink
Upgrade to widget_code_input version 4 and tests for lab 4 (PR #45)
Browse files Browse the repository at this point in the history
With widget_code_input 4 the WidgetCodeInput can now be used in lab 4.  The
tests have been updated to test support lab 4.
  • Loading branch information
agoscinski committed Jul 4, 2024
2 parents 989f7c1 + 79680a2 commit e025b01
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 70 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/tests-lab-4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests - lab 4

on:
push:
branches: [main]
pull_request:
# Check all PR

jobs:
tests:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-22.04
python-version: "3.11"

steps:
- uses: actions/checkout@v3
- name: Install Firefox
uses: browser-actions/setup-firefox@latest

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: pip install tox

- name: run Python tests
run: tox -e tests-lab-4

- name: run Python tests for coverage
run: tox -e coverage
- uses: codecov/codecov-action@v3
with:
files: coverage.xml
verbose: true
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dependencies = [
"ipywidgets>=8.0.0",
"numpy<2.0.0",
"widget_code_input<4.0.0",
"widget_code_input>=4.0.13",
"matplotlib",
"termcolor"
]
Expand Down
21 changes: 18 additions & 3 deletions src/scwidgets/cue/_widget_cue_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ def __init__(
# we close the figure so the figure is only contained in this widget
# and not shown using plt.show()
plt.close(self.figure)
elif matplotlib.backends.backend == "module://ipympl.backend_nbagg":
elif (
matplotlib.backends.backend == "module://ipympl.backend_nbagg"
or matplotlib.backends.backend == "widget"
):
# jupyter lab 3 uses "module://ipympl.backend_nbagg"
# jupyter lab 4 uses "widget"
with self:
self.figure.canvas.show()
else:
Expand All @@ -83,7 +88,12 @@ def clear_display(self, wait=False):
if matplotlib.backends.backend == "module://matplotlib_inline.backend_inline":
self.clear_figure()
self.clear_output(wait=wait)
elif matplotlib.backends.backend == "module://ipympl.backend_nbagg":
elif (
matplotlib.backends.backend == "module://ipympl.backend_nbagg"
or matplotlib.backends.backend == "widget"
):
# jupyter lab 3 uses "module://ipympl.backend_nbagg"
# jupyter lab 4 uses "widget"
self.clear_figure()
if not (wait):
self.figure.canvas.draw_idle()
Expand All @@ -100,7 +110,12 @@ def draw_display(self):
if matplotlib.backends.backend == "module://matplotlib_inline.backend_inline":
with self:
display(self.figure)
elif matplotlib.backends.backend == "module://ipympl.backend_nbagg":
elif (
matplotlib.backends.backend == "module://ipympl.backend_nbagg"
or matplotlib.backends.backend == "widget"
):
# jupyter lab 3 uses "module://ipympl.backend_nbagg"
# jupyter lab 4 uses "widget"
self.figure.canvas.draw_idle()
self.figure.canvas.flush_events()
else:
Expand Down
23 changes: 15 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urljoin

import pytest
from packaging.version import Version
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
Expand All @@ -17,7 +18,7 @@
JUPYTER_VERSION = None


def get_jupyter_version() -> str:
def get_jupyter_version() -> Version:
"""
Function so we can update the jupyter version during initialization
and use it in other files
Expand Down Expand Up @@ -45,7 +46,7 @@ def notebook_service():
["jupyter", f"{JUPYTER_TYPE}", "--version"]
)
# convert to string
JUPYTER_VERSION = jupyter_version.decode().replace("\n", "")
JUPYTER_VERSION = Version(jupyter_version.decode().replace("\n", ""))

jupyter_process = subprocess.Popen(
[
Expand Down Expand Up @@ -106,17 +107,20 @@ def _selenium_driver(nb_path):

# jupyter lab < 4
if JUPYTER_TYPE == "lab":
if get_jupyter_version() < "4.0.0":
if get_jupyter_version() < Version("4.0.0"):
restart_kernel_button_class_name = (
"bp3-button.bp3-minimal.jp-ToolbarButtonComponent.minimal.jp-Button"
)
restart_kernel_button_title_attribute = (
"Restart Kernel and Run All Cells…"
)
else:
raise ValueError("jupyter lab > 4.0.0 is not supported.")
restart_kernel_button_class_name = "jp-ToolbarButtonComponent"
restart_kernel_button_title_attribute = (
"Restart the kernel and run all cells"
)
elif JUPYTER_TYPE == "notebook":
if get_jupyter_version() < "7.0.0":
if get_jupyter_version() < Version("7.0.0"):
restart_kernel_button_class_name = "btn.btn-default"
restart_kernel_button_title_attribute = (
"restart the kernel, then re-run the whole notebook (with dialog)"
Expand Down Expand Up @@ -169,15 +173,18 @@ def _selenium_driver(nb_path):
# -------------------------------

if JUPYTER_TYPE == "lab":
if get_jupyter_version() < "4.0.0":
if get_jupyter_version() < Version("4.0.0"):
restart_button_class_name = (
"jp-Dialog-button.jp-mod-accept.jp-mod-warn.jp-mod-styled"
)
restart_button_text = "Restart"
else:
raise ValueError("jupyter lab > 4.0.0 is not supported.")
restart_button_class_name = (
"jp-Dialog-button.jp-mod-accept.jp-mod-warn.jp-mod-styled"
)
restart_button_text = "Restart"
elif JUPYTER_TYPE == "notebook":
if get_jupyter_version() < "7.0.0":
if get_jupyter_version() < Version("7.0.0"):
restart_button_class_name = "btn.btn-default.btn-sm.btn-danger"
restart_button_text = "Restart and Run All Cells"
else:
Expand Down
Loading

0 comments on commit e025b01

Please sign in to comment.