Skip to content

pywry.hot_reload

Hot reload manager for live CSS/JavaScript updates during development.


Hot Reload Manager

pywry.hot_reload.HotReloadManager

HotReloadManager(settings: HotReloadSettings | None = None, asset_loader: AssetLoader | None = None, file_watcher: FileWatcher | None = None)

Manages hot reload for CSS and JavaScript files.

  • CSS changes: Inject updated styles without page reload
  • JS changes: Trigger full page refresh (with scroll position preservation)

Initialize the hot reload manager.

PARAMETER DESCRIPTION
settings

Hot reload settings. Uses defaults if not provided.

TYPE: HotReloadSettings or None DEFAULT: None

asset_loader

Asset loader instance. Creates default if not provided.

TYPE: AssetLoader or None DEFAULT: None

file_watcher

File watcher instance. Creates default if not provided.

TYPE: FileWatcher or None DEFAULT: None

Attributes

_settings instance-attribute

_settings = settings

_asset_loader instance-attribute

_asset_loader = asset_loader or get_asset_loader()

_file_watcher instance-attribute

_file_watcher = file_watcher or get_file_watcher(debounce_ms)

_window_files instance-attribute

_window_files: dict[str, dict[Path, str]] = {}

_asset_ids instance-attribute

_asset_ids: dict[str, dict[Path, str]] = {}

_inject_css_callback instance-attribute

_inject_css_callback: Callable[..., Any] | None = None

_refresh_callback instance-attribute

_refresh_callback: Callable[..., Any] | None = None

_running instance-attribute

_running = False

is_running property

is_running: bool

Check if hot reload is active.

settings property

Get current settings.

Functions

set_inject_css_callback

set_inject_css_callback(callback: Callable[..., Any]) -> None

Set the callback for CSS injection.

PARAMETER DESCRIPTION
callback

Function(label, css, asset_id) to inject CSS.

TYPE: Callable[..., Any]

set_refresh_callback

set_refresh_callback(callback: Callable[..., Any]) -> None

Set the callback for page refresh.

PARAMETER DESCRIPTION
callback

Function(label) to refresh a window.

TYPE: Callable[..., Any]

start

start() -> None

Start the hot reload manager.

stop

stop() -> None

Stop the hot reload manager.

enable_for_window

enable_for_window(label: str, content: HtmlContent) -> None

Enable hot reload for a window's assets.

PARAMETER DESCRIPTION
label

Window label.

TYPE: str

content

HTML content with file references.

TYPE: HtmlContent

disable_for_window

disable_for_window(label: str) -> None

Disable hot reload for a window.

PARAMETER DESCRIPTION
label

Window label.

TYPE: str

reload_css

reload_css(label: str, path: Path | None = None) -> bool

Reload CSS for a window.

PARAMETER DESCRIPTION
label

Window label.

TYPE: str

path

Specific CSS file to reload. If None, reloads all.

TYPE: Path or None DEFAULT: None

RETURNS DESCRIPTION
bool

True if CSS was injected, False otherwise.

refresh_window

refresh_window(label: str) -> bool

Trigger a full page refresh for a window.

PARAMETER DESCRIPTION
label

Window label.

TYPE: str

RETURNS DESCRIPTION
bool

True if refresh was triggered, False otherwise.

_on_file_change

_on_file_change(path: Path, label: str) -> None

Handle file change event.

PARAMETER DESCRIPTION
path

Path to changed file.

TYPE: Path

label

Window label.

TYPE: str

get_watched_files

get_watched_files(label: str | None = None) -> dict[str, list[Path]]

Get list of watched files.

PARAMETER DESCRIPTION
label

Specific window label. If None, returns all.

TYPE: str or None DEFAULT: None

RETURNS DESCRIPTION
dict[str, list[Path]]

Dict mapping labels to lists of watched paths.


Factory Functions

pywry.hot_reload.get_hot_reload_manager

get_hot_reload_manager(settings: HotReloadSettings | None = None) -> HotReloadManager

Get the global hot reload manager instance.

PARAMETER DESCRIPTION
settings

Settings for new manager creation.

TYPE: HotReloadSettings or None DEFAULT: None

RETURNS DESCRIPTION
HotReloadManager

Global HotReloadManager instance.

pywry.hot_reload.stop_hot_reload_manager

stop_hot_reload_manager() -> None

Stop the global hot reload manager if running.