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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
Path or None
|
Base directory for resolving relative paths. Defaults to current working directory. |
None
|
Attributes¶
Functions¶
resolve_path
¶
Resolve a path relative to the base directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Absolute or relative path. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Resolved absolute path. |
load_css
¶
Load a CSS file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the CSS file. |
required |
use_cache
|
bool
|
Whether to use cached content. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
CSS file content, or empty string on error. |
load_script
¶
Load a JavaScript file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the JavaScript file. |
required |
use_cache
|
bool
|
Whether to use cached content. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JavaScript file content, or empty string on error. |
load_all_css
¶
Load and concatenate multiple CSS files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list of str or Path
|
List of paths to CSS files. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Concatenated CSS content. |
load_all_scripts
¶
Load multiple JavaScript files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list of str or Path
|
List of paths to JavaScript files. |
required |
Returns:
| Type | 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to the asset file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Asset ID string suitable for use as HTML element ID. |
has_changed
¶
Check if a file has changed since last load.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file content has changed, False otherwise. |
invalidate
¶
Invalidate cached content for a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to invalidate. |
required |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
Path or None
|
Base directory for resolving paths. |
None
|
settings
|
AssetSettings or None
|
Asset settings to apply. |
None
|
Returns:
| Type | Description |
|---|---|
AssetLoader
|
Configured asset loader instance. |
HTML Helpers¶
pywry.asset_loader.build_style_tag
cached
¶
Build a style tag with the given CSS content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
css
|
str
|
CSS content. |
required |
asset_id
|
str
|
ID for the style element. |
required |
Returns:
| Type | Description |
|---|---|
str
|
HTML style tag string. |
pywry.asset_loader.build_script_tag
cached
¶
Build a script tag with the given JavaScript content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script
|
str
|
JavaScript content. |
required |
asset_id
|
str or None
|
Optional ID for the script element. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML script tag string. |