-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement static CSS loader utility (#624)
`ipywidgets` provides its own mechanism for styling widgets. However, it is often insufficient and clunky, only exposing a limited set of style properties. This PR implements a CSS stylesheet loader utility and a dedicated static folder for CSS stylesheets (loaded on import by the utility) to extend widget styling to the full breadth of standard features allowed by CSS. The utility can be imported by apps wishing to leverage this approach to widget styling.
- Loading branch information
1 parent
0c9f6fb
commit 73df986
Showing
12 changed files
with
172 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Stylesheets for AiiDAlab Widgets Base | ||
|
||
This folder contains `.css` stylesheets, which are loaded on any import from the AiiDAlab widgets base package. |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
from IPython.display import Javascript, display | ||
|
||
|
||
def load_css(css_path: Path | str) -> None: | ||
"""Load and inject CSS stylesheets into the DOM. | ||
Parameters | ||
---------- | ||
`css_path` : `Path` | `str` | ||
The path to the CSS stylesheet. If the path is a directory, | ||
all CSS files in the directory will be loaded. | ||
""" | ||
path = Path(css_path) | ||
|
||
if not path.exists(): | ||
raise FileNotFoundError(f"CSS file or directory not found: {path}") | ||
|
||
filenames = [*path.glob("*.css")] if path.is_dir() else [path] | ||
|
||
for fn in filenames: | ||
stylesheet = fn.read_text() | ||
display( | ||
Javascript(f""" | ||
var style = document.createElement('style'); | ||
style.type = 'text/css'; | ||
style.innerHTML = `{stylesheet}`; | ||
document.head.appendChild(style); | ||
""") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pathlib import Path | ||
|
||
from aiidalab_widgets_base.utils.loaders import load_css | ||
|
||
|
||
def test_load_css(): | ||
"""Test `load_css` utility.""" | ||
css_dir = Path("aiidalab_widgets_base/static/styles") | ||
load_css(css_path=css_dir) | ||
load_css(css_path=css_dir / "global.css") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.red-text { | ||
color: rgb(255, 0, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ipywidgets as ipw" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from aiida import load_profile\n", | ||
"\n", | ||
"load_profile();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from aiidalab_widgets_base.utils.loaders import load_css\n", | ||
"\n", | ||
"load_css(css_path=\"static/styles/test.css\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"label = ipw.Label(\"Testing\")\n", | ||
"label.add_class(\"red-text\")\n", | ||
"display(label)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters