pywry.asset_loader¶
Utilities for loading and caching CSS/JavaScript assets.
Asset Loader¶
pywry.asset_loader.AssetLoader
¶
Loads and caches CSS and JavaScript files.
Provides hash-based asset IDs for efficient hot reload detection.
Initialize the asset loader.
| PARAMETER | DESCRIPTION |
|---|---|
base_dir
|
Base directory for resolving relative paths. Defaults to current working directory.
TYPE:
|
Attributes¶
Functions¶
resolve_path
¶
Resolve a path relative to the base directory.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Absolute or relative path.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Resolved absolute path. |
load_css
¶
Load a CSS file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the CSS file.
TYPE:
|
use_cache
|
Whether to use cached content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
CSS file content, or empty string on error. |
load_script
¶
Load a JavaScript file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the JavaScript file.
TYPE:
|
use_cache
|
Whether to use cached content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
JavaScript file content, or empty string on error. |
load_all_css
¶
Load and concatenate multiple CSS files.
| PARAMETER | DESCRIPTION |
|---|---|
paths
|
List of paths to CSS files.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Concatenated CSS content. |
load_all_scripts
¶
Load multiple JavaScript files.
| PARAMETER | DESCRIPTION |
|---|---|
paths
|
List of paths to JavaScript files.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of script contents in order. |
get_asset_id
¶
Get a unique asset ID based on path and content hash.
The ID is stable across reloads if content hasn't changed.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the asset file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Asset ID string suitable for use as HTML element ID. |
has_changed
¶
Check if a file has changed since last load.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if file content has changed, False otherwise. |
invalidate
¶
Invalidate cached content for a file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to invalidate.
TYPE:
|
Factory Function¶
pywry.asset_loader.get_asset_loader
¶
get_asset_loader() -> AssetLoader
Get the global asset loader instance.
pywry.asset_loader.configure_asset_loader
¶
configure_asset_loader(base_dir: Path | None = None, settings: AssetSettings | None = None) -> AssetLoader
Configure and return the global asset loader.
| PARAMETER | DESCRIPTION |
|---|---|
base_dir
|
Base directory for resolving paths.
TYPE:
|
settings
|
Asset settings to apply.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AssetLoader
|
Configured asset loader instance. |
HTML Helpers¶
pywry.asset_loader.build_style_tag
cached
¶
Build a style tag with the given CSS content.
| PARAMETER | DESCRIPTION |
|---|---|
css
|
CSS content.
TYPE:
|
asset_id
|
ID for the style element.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
HTML style tag string. |
pywry.asset_loader.build_script_tag
cached
¶
Build a script tag with the given JavaScript content.
| PARAMETER | DESCRIPTION |
|---|---|
script
|
JavaScript content.
TYPE:
|
asset_id
|
Optional ID for the script element.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
HTML script tag string. |