Skip to content

Commit

Permalink
Simplify CSS loading utility
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Aug 22, 2024
1 parent e106e01 commit 8613744
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
4 changes: 3 additions & 1 deletion aiidalab_widgets_base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -52,7 +54,7 @@ def is_running_in_jupyter():
from .static import styles
from .utils.loaders import load_css_stylesheet

load_css_stylesheet(package=styles)
load_css_stylesheet(css_dir=Path(styles.__file__))


from .computational_resources import (
Expand Down
28 changes: 3 additions & 25 deletions aiidalab_widgets_base/utils/loaders.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
15 changes: 5 additions & 10 deletions notebooks/test_misc.ipynb
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as ipw"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -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\"))"
]
},
{
Expand All @@ -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)"
Expand Down
9 changes: 6 additions & 3 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from pathlib import Path

from aiidalab_widgets_base.static import styles
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(styles.__file__)
load_css_stylesheet(css_dir=css_dir, filename="global.css")
load_css_stylesheet(css_dir=css_dir)

0 comments on commit 8613744

Please sign in to comment.