Skip to content

pywry.asset_loader

Utilities for loading and caching CSS/JavaScript assets.


Asset Loader

pywry.asset_loader.AssetLoader

AssetLoader(base_dir: Path | None = None)

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

_base_dir instance-attribute

_base_dir = base_dir or cwd()

_cache instance-attribute

_cache: dict[Path, str] = {}

_hash_cache instance-attribute

_hash_cache: dict[Path, str] = {}

base_dir property writable

base_dir: Path

Get the base directory for resolving paths.

Functions

resolve_path

resolve_path(path: str | Path) -> 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_css(path: str | Path, use_cache: bool = True) -> str

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_script(path: str | Path, use_cache: bool = True) -> str

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_all_css(paths: list[str | Path]) -> str

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_all_scripts(paths: list[str | Path]) -> list[str]

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_asset_id(path: str | Path) -> str

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.

_update_hash

_update_hash(path: Path, content: str) -> None

Update the content hash for a file.

has_changed

has_changed(path: str | Path) -> bool

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(path: str | Path) -> None

Invalidate cached content for a file.

Parameters:

Name Type Description Default
path str or Path

Path to invalidate.

required

clear_cache

clear_cache() -> None

Clear all cached content.


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_style_tag(css: str, asset_id: str) -> str

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_script_tag(script: str, asset_id: str | None = None) -> str

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.