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.

Parameters:

Name Type Description Default
settings HotReloadSettings or None

Hot reload settings. Uses defaults if not provided.

None
asset_loader AssetLoader or None

Asset loader instance. Creates default if not provided.

None
file_watcher FileWatcher or None

File watcher instance. Creates default if not provided.

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.

Parameters:

Name Type Description Default
callback Callable[..., Any]

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

required

set_refresh_callback

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

Set the callback for page refresh.

Parameters:

Name Type Description Default
callback Callable[..., Any]

Function(label) to refresh a window.

required

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.

Parameters:

Name Type Description Default
label str

Window label.

required
content HtmlContent

HTML content with file references.

required

disable_for_window

disable_for_window(label: str) -> None

Disable hot reload for a window.

Parameters:

Name Type Description Default
label str

Window label.

required

reload_css

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

Reload CSS for a window.

Parameters:

Name Type Description Default
label str

Window label.

required
path Path or None

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

None

Returns:

Type Description
bool

True if CSS was injected, False otherwise.

refresh_window

refresh_window(label: str) -> bool

Trigger a full page refresh for a window.

Parameters:

Name Type Description Default
label str

Window label.

required

Returns:

Type Description
bool

True if refresh was triggered, False otherwise.

_on_file_change

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

Handle file change event.

Parameters:

Name Type Description Default
path Path

Path to changed file.

required
label str

Window label.

required

get_watched_files

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

Get list of watched files.

Parameters:

Name Type Description Default
label str or None

Specific window label. If None, returns all.

None

Returns:

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

Parameters:

Name Type Description Default
settings HotReloadSettings or None

Settings for new manager creation.

None

Returns:

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