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 0b1392d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 41 deletions.
5 changes: 3 additions & 2 deletions 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 @@ -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 (
Expand Down
Empty file.
Empty file.
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
17 changes: 6 additions & 11 deletions notebooks/test_misc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
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
8 changes: 5 additions & 3 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0b1392d

Please sign in to comment.