pywry.window_manager¶
Window lifecycle management, controllers, and rendering mode strategies.
Lifecycle¶
pywry.window_manager.get_lifecycle
¶
get_lifecycle() -> WindowLifecycle
Get the global window lifecycle manager.
| RETURNS | DESCRIPTION |
|---|---|
WindowLifecycle
|
The window lifecycle singleton. |
pywry.window_manager.WindowLifecycle
¶
Manages window lifecycle with aggressive cleanup.
Uses subprocess IPC to communicate with pytauri process.
Initialize the lifecycle manager.
Functions¶
clear
¶
Clear all tracked windows.
This resets the lifecycle manager to its initial state. Useful for test cleanup when the runtime is stopped.
create
¶
create(label: str, title: str = 'PyWry', width: int = 800, height: int = 600, **builder_opts: Any) -> WindowResources
Create or register a window via subprocess IPC.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
title
|
Window title.
TYPE:
|
width
|
Window width.
TYPE:
|
height
|
Window height.
TYPE:
|
**builder_opts
|
Additional keyword arguments forwarded to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WindowResources
|
The window resources object. |
register_window
¶
register_window(label: str) -> WindowResources
Register a window in lifecycle tracking without creating via IPC.
Use this when the window already exists (e.g., from previous PyWry instance) and just needs to be tracked.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WindowResources
|
The window resources object. |
set_content
¶
set_content(label: str, html: str, theme: str = 'dark', config: WindowConfig | None = None) -> bool
Set the HTML content for a window via IPC.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
The HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
config
|
Store config for content-request handler to use.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if successful, False otherwise. |
is_open
¶
Check if window is truly open via IPC.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is verified open in backend. |
store_content_for_refresh
¶
store_content_for_refresh(label: str, content: HtmlContent, config: WindowConfig) -> bool
Store content and config for window refresh.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
content
|
The HtmlContent object.
TYPE:
|
config
|
The WindowConfig object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if stored successfully. |
get_content_for_refresh
¶
get_content_for_refresh(label: str) -> tuple[HtmlContent | None, WindowConfig | None]
Get stored content and config for window refresh.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[HtmlContent or None, WindowConfig or None]
|
Tuple of (content, config) or (None, None) if not found. |
add_watched_file
¶
Track a file for hot reload.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
path
|
Path to the watched file.
TYPE:
|
file_type
|
Either "css" or "script".
TYPE:
|
asset_id
|
Asset ID for CSS files.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if tracked successfully. |
clear_watched_files
¶
Clear all watched files for a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if cleared successfully. |
destroy
¶
Destroy a window via IPC.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if resources were destroyed, False if window not found. |
destroy_all
¶
Destroy all tracked windows.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of windows destroyed. |
get_labels
¶
Get all active window labels.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of window labels. |
get_stats
¶
Get statistics about tracked windows.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Statistics dict. |
pywry.window_manager.WindowResources
dataclass
¶
WindowResources(label: str, created_at: datetime = now(), html_content: str | None = None, scripts_injected: list[str] = list(), libraries_loaded: list[str] = list(), custom_data: dict[str, Any] = dict(), is_destroyed: bool = False, last_content: HtmlContent | None = None, last_config: WindowConfig | None = None, watched_css: list[Path] = list(), watched_scripts: list[Path] = list(), css_asset_ids: dict[Path, str] = dict(), content_set_at: datetime | None = None, on_close: list[Callable[[str, str], None]] = list(), close_reason: str | None = None)
Tracks resources associated with a window.
Controller¶
pywry.window_manager.WindowController
¶
WindowController(mode: WindowMode = NEW_WINDOW)
Controller for window management across different modes.
This class provides a unified interface for creating and managing windows regardless of the underlying window mode.
Initialize the controller.
| PARAMETER | DESCRIPTION |
|---|---|
mode
|
The window mode to use.
TYPE:
|
Attributes¶
Functions¶
set_mode
¶
set_mode(mode: WindowMode) -> None
Change the window mode.
Warning: This closes all existing windows.
| PARAMETER | DESCRIPTION |
|---|---|
mode
|
The new window mode.
TYPE:
|
show
¶
show(config: WindowConfig, html: str, callbacks: dict[str, CallbackFunc] | None = None) -> str
Show content in a window.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration.
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional event callbacks.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The window label. |
close
¶
Close a specific window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if closed successfully. |
is_open
¶
Check if a window is open.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is open. |
update_content
¶
Update window content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully. |
send_event
¶
Send an event to a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully. |
get_labels
¶
Get all window labels.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of window labels. |
get_stats
¶
Get window statistics.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Statistics dict. |
Mode Strategies¶
pywry.window_manager.WindowModeBase
¶
Bases: ABC
Abstract base class for window modes.
Functions¶
show
abstractmethod
¶
show(config: WindowConfig, html: str, callbacks: dict[str, Any] | None = None, label: str | None = None) -> str
Show content in a window.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration.
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional callback handlers.
TYPE:
|
label
|
Optional window label (for multi-window mode).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The window label. |
close
abstractmethod
¶
Close a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if closed successfully, False otherwise. |
is_open
abstractmethod
¶
Check if a window is open.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is open, False otherwise. |
update_content
abstractmethod
¶
Update window content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully, False otherwise. |
send_event
abstractmethod
¶
Send an event to a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully, False otherwise. |
get_labels
abstractmethod
¶
Get all window labels managed by this mode.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of window labels. |
close_all
abstractmethod
¶
Close all windows.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of windows closed. |
show_window
¶
Show a hidden window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if shown successfully, False otherwise. |
hide_window
¶
Hide a window (keeps it alive, just not visible).
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if hidden successfully, False otherwise. |
pywry.window_manager.NewWindowMode
¶
Bases: WindowModeBase
Creates a new window for each show() call.
Each window is independent and has its own lifecycle.
Initialize the mode.
Functions¶
show
¶
show(config: WindowConfig, html: str, callbacks: dict[str, Any] | None = None, label: str | None = None) -> str
Show content in a new window.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration.
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional callback handlers.
TYPE:
|
label
|
Ignored in new window mode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The window label. |
close
¶
Close a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if closed successfully, False otherwise. |
is_open
¶
Check if a window is open.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is open and visible, False otherwise. |
update_content
¶
Update window content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully, False otherwise. |
send_event
¶
Send an event to a window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully, False otherwise. |
get_labels
¶
Get all visible window labels managed by this mode.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of visible window labels. |
pywry.window_manager.SingleWindowMode
¶
Bases: WindowModeBase
Reuses a single window for all show() calls.
Content is replaced in-place without creating new windows.
Initialize the mode.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The fixed label for the single window, by default "main".
TYPE:
|
Attributes¶
Functions¶
show
¶
show(config: WindowConfig, html: str, callbacks: dict[str, Any] | None = None, label: str | None = None) -> str
Show content in the single window.
If window doesn't exist, creates it. Otherwise replaces content. Window is never destroyed - just hidden when user clicks X.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration.
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional callback handlers.
TYPE:
|
label
|
Ignored for single window mode.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The window label. |
close
¶
Close the window.
This closes the window but preserves lifecycle tracking and callbacks, allowing the window to be reopened via show(). For full cleanup, use the app's destroy() method instead.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label (must match our label).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if closed successfully, False otherwise. |
is_open
¶
Check if the window is open.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is open, False otherwise. |
update_content
¶
Update window content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully, False otherwise. |
send_event
¶
Send an event to the window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully, False otherwise. |
get_labels
¶
Get window labels.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List containing the single window label if visible. |
close_all
¶
Close all windows (just the one).
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of windows closed (0 or 1). |
pywry.window_manager.MultiWindowMode
¶
Bases: WindowModeBase
Manages multiple independent windows.
Each window has a unique label and can be controlled independently. Non-blocking - all windows can be interacted with simultaneously.
Initialize the mode.
Functions¶
show
¶
show(config: WindowConfig, html: str, callbacks: dict[str, Any] | None = None, label: str | None = None) -> str
Show content in a window.
If label is provided and window exists, updates content. Otherwise creates a new window.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration.
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional callback handlers.
TYPE:
|
label
|
Optional specific label (for update).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The window label. |
close
¶
Close a specific window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if closed successfully, False otherwise. |
is_open
¶
Check if a window is open.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if window is open, False otherwise. |
update_content
¶
Update window content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light') - MUST match window background.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully, False otherwise. |
send_event
¶
Send an event to a specific window.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully, False otherwise. |
send_event_all
¶
Send an event to all windows.
| PARAMETER | DESCRIPTION |
|---|---|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of windows that received the event. |
get_labels
¶
Get all visible window labels.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of visible window labels. |
get_window_count
¶
Get the number of open windows.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of open windows. |
show_window
¶
Show a hidden window and update visibility tracking.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if shown successfully, False otherwise. |
hide_window
¶
Hide a window and update visibility tracking.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if hidden successfully, False otherwise. |
pywry.window_manager.BrowserMode
¶
Bases: WindowModeBase
Opens content in the system's default browser.
Uses the inline FastAPI server and opens the widget URL in a browser tab. Each show() call creates a new widget instance with its own state.
This mode is ideal for headless environments (servers, SSH sessions) or when native window support is not available.
Initialize the browser mode.
Functions¶
show
¶
show(config: WindowConfig, html: str, callbacks: dict[str, Any] | None = None, label: str | None = None) -> str
Show content in the system browser.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Window configuration (width/height used for suggestion only).
TYPE:
|
html
|
HTML content to display.
TYPE:
|
callbacks
|
Optional callback handlers.
TYPE:
|
label
|
Optional widget label (used as prefix for widget_id).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The widget ID (used as window_label for GenericEvent). |
close
¶
Close a widget by removing it from tracking.
Note: This does not close the browser tab - that's user-controlled. It does trigger the disconnect callback and cleanup.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if widget was found and cleaned up. |
close_all
¶
Close all tracked widgets.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of widgets closed. |
is_open
¶
Check if a widget is being tracked.
Note: This only checks if we're tracking the widget, not if the browser tab is actually open (we can't know that).
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if widget is being tracked. |
update_content
¶
Update widget content.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The widget ID.
TYPE:
|
html
|
New HTML content.
TYPE:
|
theme
|
Theme mode ('dark' or 'light').
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated successfully. |
send_event
¶
Send an event to a widget's browser.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if sent successfully. |
get_labels
¶
Get all tracked widget IDs.
| RETURNS | DESCRIPTION |
|---|---|
list of str
|
List of widget IDs. |
get_widget
¶
Get an InlineWidget by ID for further interaction.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID returned from show().
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
InlineWidget or None
|
The widget instance, or None if not found. |