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.

PARAMETER DESCRIPTION
base_dir

Base directory for resolving relative paths. Defaults to current working directory.

TYPE: Path or None DEFAULT: 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.

PARAMETER DESCRIPTION
path

Absolute or relative path.

TYPE: str or Path

RETURNS DESCRIPTION
Path

Resolved absolute path.

load_css

load_css(path: str | Path, use_cache: bool = True) -> str

Load a CSS file.

PARAMETER DESCRIPTION
path

Path to the CSS file.

TYPE: str or Path

use_cache

Whether to use cached content.

TYPE: bool DEFAULT: True

RETURNS 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.

PARAMETER DESCRIPTION
path

Path to the JavaScript file.

TYPE: str or Path

use_cache

Whether to use cached content.

TYPE: bool DEFAULT: True

RETURNS 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.

PARAMETER DESCRIPTION
paths

List of paths to CSS files.

TYPE: list of str or Path

RETURNS DESCRIPTION
str

Concatenated CSS content.

load_all_scripts

load_all_scripts(paths: list[str | Path]) -> list[str]

Load multiple JavaScript files.

PARAMETER DESCRIPTION
paths

List of paths to JavaScript files.

TYPE: list of str or Path

RETURNS 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.

PARAMETER DESCRIPTION
path

Path to the asset file.

TYPE: str or Path

RETURNS 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.

PARAMETER DESCRIPTION
path

Path to check.

TYPE: str or Path

RETURNS DESCRIPTION
bool

True if file content has changed, False otherwise.

invalidate

invalidate(path: str | Path) -> None

Invalidate cached content for a file.

PARAMETER DESCRIPTION
path

Path to invalidate.

TYPE: str or Path

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.

PARAMETER DESCRIPTION
base_dir

Base directory for resolving paths.

TYPE: Path or None DEFAULT: None

settings

Asset settings to apply.

TYPE: AssetSettings or None DEFAULT: None

RETURNS 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.

PARAMETER DESCRIPTION
css

CSS content.

TYPE: str

asset_id

ID for the style element.

TYPE: str

RETURNS 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.

PARAMETER DESCRIPTION
script

JavaScript content.

TYPE: str

asset_id

Optional ID for the script element.

TYPE: str or None DEFAULT: None

RETURNS DESCRIPTION
str

HTML script tag string.