diff --git a/aiidalab_widgets_base/__init__.py b/aiidalab_widgets_base/__init__.py index a9f4dec55..269933c74 100644 --- a/aiidalab_widgets_base/__init__.py +++ b/aiidalab_widgets_base/__init__.py @@ -40,6 +40,8 @@ def is_running_in_jupyter(): if is_running_in_jupyter(): + from pathlib import Path + from aiida.manage import get_profile from IPython.display import HTML, display @@ -49,10 +51,9 @@ def is_running_in_jupyter(): if get_profile() is None: load_default_profile() - from .static import styles from .utils.loaders import load_css_stylesheet - load_css_stylesheet(package=styles) + load_css_stylesheet(css_dir=Path("static/styles")) from .computational_resources import ( diff --git a/aiidalab_widgets_base/static/__init__.py b/aiidalab_widgets_base/static/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/aiidalab_widgets_base/static/styles/__init__.py b/aiidalab_widgets_base/static/styles/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/aiidalab_widgets_base/utils/loaders.py b/aiidalab_widgets_base/utils/loaders.py index 7af25818f..30fd7d0c3 100644 --- a/aiidalab_widgets_base/utils/loaders.py +++ b/aiidalab_widgets_base/utils/loaders.py @@ -1,44 +1,22 @@ from __future__ import annotations -from importlib.resources import Package, files from pathlib import Path from IPython.display import Javascript, display -def load_css_stylesheet( - package: Package | None = None, - css_path: str | Path = "", - filename: str = "", -): +def load_css_stylesheet(css_dir: Path, filename: str = ""): """Load a CSS stylesheet from a package and inject it into the DOM. Parameters ---------- - `package` : `Package`, optional - The package where the CSS file is located. - `css_path` : `str` | `Path`, optional + `css_dir` : `Path` The path to the folder where the CSS file is located. `filename` : `str`, optional The name of the CSS file to load. If not provided, all CSS files in the package/folder will be loaded. """ - if package: - root = files(package) - filenames = ( - [root / filename] - if filename - else [ - root / path.name - for path in root.iterdir() - if path.is_file() and path.name.endswith(".css") - ] - ) - elif css_path: - path = Path(css_path) - filenames = [path / filename] if filename else [*path.glob("*.css")] - else: - raise ValueError("Either `package` or `path` must be provided.") + filenames = [css_dir / filename] if filename else [*css_dir.glob("*.css")] for fn in filenames: stylesheet = fn.read_text() diff --git a/notebooks/test_misc.ipynb b/notebooks/test_misc.ipynb index 85504a7c7..22ed7c171 100644 --- a/notebooks/test_misc.ipynb +++ b/notebooks/test_misc.ipynb @@ -2,16 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import ipywidgets as ipw" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -26,9 +17,11 @@ "metadata": {}, "outputs": [], "source": [ + "from pathlib import Path\n", + "\n", "from aiidalab_widgets_base.utils.loaders import load_css_stylesheet\n", "\n", - "load_css_stylesheet(css_path=\"../tests_notebooks/static/styles\")" + "load_css_stylesheet(css_dir=Path(\"../tests_notebooks/static/styles\"))" ] }, { @@ -37,6 +30,8 @@ "metadata": {}, "outputs": [], "source": [ + "import ipywidgets as ipw\n", + "\n", "label = ipw.Label(\"Testing\")\n", "label.add_class(\"red-text\")\n", "display(label)" diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 21caa9186..598d07576 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -1,8 +1,10 @@ +from pathlib import Path + from aiidalab_widgets_base.utils.loaders import load_css_stylesheet def test_load_css_stylesheet(): """Test `load_css_stylesheet` function.""" - package = "aiidalab_widgets_base.static.styles" - load_css_stylesheet(package=package, filename="global.css") - load_css_stylesheet(package=package) + css_dir = Path("aiidalab_widgets_base/static/styles") + load_css_stylesheet(css_dir=css_dir, filename="global.css") + load_css_stylesheet(css_dir=css_dir)