Skip to content

Commit

Permalink
Allow str paths for loading css stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Aug 22, 2024
1 parent f859695 commit e224fbd
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions aiidalab_widgets_base/utils/loaders.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
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, filename: str = ""):
def load_css_stylesheet(
package: Package | None = None,
css_path: str | Path = "",
filename: str = "",
):
"""Load a CSS stylesheet from a package and inject it into the DOM.
Parameters
----------
`package` : `Package`
`package` : `Package`, optional
The package where the CSS file is located.
`css_path` : `str` | `Path`, optional
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 will be loaded.
If not provided, all CSS files in the package/folder will be loaded.
"""
root = files(package)

if filename:
filenames = [filename]
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:
filenames = [
path.name
for path in root.iterdir()
if path.is_file() and path.name.endswith(".css")
]
raise ValueError("Either `package` or `path` must be provided.")

for fn in filenames:
stylesheet = (root / fn).read_text()
stylesheet = fn.read_text()
display(
Javascript(f"""
var style = document.createElement('style');
Expand Down

0 comments on commit e224fbd

Please sign in to comment.