HTMLWindow Class
The HTMLWindow class provides JavaScript window object functionality within PyPositron. It's available as ui.htmlwindow
in your main function and represents the browser window that contains your HTML document. This class provides access to browser APIs, dialog methods, timing functions, and window management capabilities.
Overview
The HTMLWindow class is your gateway to browser-specific functionality in PyPositron. It allows you to:
- Display dialog boxes (alert, confirm, prompt)
- Manage timers and animations
- Control window properties and behavior
- Access browser objects (navigator, location, history)
- Handle window events and navigation
Properties
Window State
closed -> bool
: Returns true if a window is closed. W3Schools Reference | MDN Referencename -> str
: Sets or returns the name of a window. W3Schools Reference | MDN Reference
Window Dimensions
innerHeight -> int
: Returns the height of the window's content area (viewport) including scrollbars. W3Schools Reference | MDN ReferenceinnerWidth -> int
: Returns the width of a window's content area (viewport) including scrollbars. W3Schools Reference | MDN ReferenceouterHeight -> int
: Returns the height of the browser window, including toolbars/scrollbars. W3Schools Reference | MDN ReferenceouterWidth -> int
: Returns the width of the browser window, including toolbars/scrollbars. W3Schools Reference | MDN Reference
Window Position
screenLeft -> int
: Returns the horizontal coordinate of the window relative to the screen. W3Schools Reference | MDN ReferencescreenTop -> int
: Returns the vertical coordinate of the window relative to the screen. W3Schools Reference | MDN ReferencescreenX -> int
: Returns the horizontal coordinate of the window relative to the screen. W3Schools Reference | MDN ReferencescreenY -> int
: Returns the vertical coordinate of the window relative to the screen. W3Schools Reference | MDN Reference
Scroll Position
pageXOffset -> int
: Returns the pixels the current document has been scrolled (horizontally) from the upper left corner of the window. W3Schools Reference | MDN ReferencepageYOffset -> int
: Returns the pixels the current document has been scrolled (vertically) from the upper left corner of the window. W3Schools Reference | MDN ReferencescrollX -> int
: An alias of pageXOffset. W3Schools Reference | MDN ReferencescrollY -> int
: An alias of pageYOffset. W3Schools Reference | MDN Reference
Browser Objects
console -> Console
: Returns the Console object for the window. W3Schools Reference | MDN Referencedocument -> Document
: Returns the Document object for the window. W3Schools Reference | MDN Referencehistory -> History
: Returns the History object for the window. W3Schools Reference | MDN Referencelocation -> Location
: Returns the Location object for the window. W3Schools Reference | MDN Referencenavigator -> Navigator
: Returns the Navigator object for the window. W3Schools Reference | MDN Referencescreen -> Screen
: Returns the Screen object for the window. W3Schools Reference | MDN Reference
Window Hierarchy
frameElement
: Returns the frame in which the window runs. W3Schools Reference | MDN Referenceframes
: Returns all window objects running in the window. W3Schools Reference | MDN Referencelength -> int
: Returns the number of<iframe>
elements in the current window. W3Schools Reference | MDN Referenceopener
: Returns a reference to the window that created the window. W3Schools Reference | MDN Referenceparent
: Returns the parent window of the current window. W3Schools Reference | MDN Referenceself
: Returns the current window. W3Schools Reference | MDN Referencetop
: Returns the topmost browser window. W3Schools Reference | MDN Reference
Storage
localStorage
: Allows to save key/value pairs in a web browser. Stores the data with no expiration date. W3Schools Reference | MDN ReferencesessionStorage
: Allows to save key/value pairs in a web browser. Stores the data for one session. W3Schools Reference | MDN Reference
Methods
Dialog Methods
alert(message: str)
: Displays an alert box with a message and an OK button. W3Schools Reference | MDN Referenceconfirm(message: str) -> bool
: Displays a dialog box with a message and an OK and a Cancel button. Returns True if OK is clicked, False if Cancel is clicked. W3Schools Reference | MDN Referenceprompt(message: str, default_value: str = '') -> str
: Displays a dialog box that prompts the visitor for input. Returns the input string or None if cancelled. W3Schools Reference | MDN Reference
Timing Methods
setTimeout(callback, milliseconds: int) -> int
: Calls a function or evaluates an expression after a specified number of milliseconds. Returns a timer ID. W3Schools Reference | MDN ReferenceclearTimeout(timeout_id: int)
: Clears a timer set with setTimeout(). W3Schools Reference | MDN ReferencesetInterval(callback, milliseconds: int) -> int
: Calls a function or evaluates an expression at specified intervals (in milliseconds). Returns an interval ID. W3Schools Reference | MDN ReferenceclearInterval(interval_id: int)
: Clears a timer set with setInterval(). W3Schools Reference | MDN Reference
Animation
requestAnimationFrame(callback) -> int
: Requests the browser to call a function to update an animation before the next repaint. W3Schools Reference | MDN Reference
Window Management
open(url: str = '', name: str = '_blank', specs: str = '', replace: bool = False)
: Opens a new browser window. W3Schools Reference | MDN Referenceclose()
: Closes the current window. W3Schools Reference | MDN Referencefocus()
: Sets focus to the current window. W3Schools Reference | MDN Referenceblur()
: Removes focus from the current window. W3Schools Reference | MDN Reference
Window Positioning and Sizing
moveTo(x: int, y: int)
: Moves a window to the specified position. W3Schools Reference | MDN ReferencemoveBy(x: int, y: int)
: Moves a window relative to its current position. W3Schools Reference | MDN ReferenceresizeTo(width: int, height: int)
: Resizes the window to the specified width and height. W3Schools Reference | MDN ReferenceresizeBy(width: int, height: int)
: Resizes the window by the specified pixels. W3Schools Reference | MDN Reference
Scrolling
scrollTo(x: int, y: int)
: Scrolls the document to the specified coordinates. W3Schools Reference | MDN ReferencescrollBy(x: int, y: int)
: Scrolls the document by the specified number of pixels. W3Schools Reference | MDN Reference
Event Handling
addEventListener(event_type: str, callback: Callable) -> bool
: Attaches an event handler to the window. W3Schools Reference | MDN ReferenceremoveEventListener(event_type: str, callback=None)
: Removes an event handler from the window. W3Schools Reference | MDN Reference
Utility Methods
print()
: Prints the content of the current window. W3Schools Reference | MDN Referencestop()
: Stops the window from loading. W3Schools Reference | MDN Reference
CSS and Styling
getComputedStyle(element: Element, pseudo_element: str = None)
: Gets the current computed CSS styles applied to an element. W3Schools Reference | MDN ReferencematchMedia(media_query: str)
: Returns a MediaQueryList object representing the specified CSS media query string. W3Schools Reference | MDN Reference
Selection and Encoding
getSelection()
: Returns a Selection object representing the range of text selected by the user. W3Schools Reference | MDN Referenceatob(encoded_string: str) -> str
: Decodes a base-64 encoded string. W3Schools Reference | MDN Referencebtoa(string: str) -> str
: Encodes a string in base-64. W3Schools Reference | MDN Reference
Usage Examples
Dialog Interactions
def main(ui):
def show_welcome():
ui.htmlwindow.alert("Welcome to PyPositron!")
if ui.htmlwindow.confirm("Would you like to continue?"):
name = ui.htmlwindow.prompt("What's your name?", "User")
if name:
ui.htmlwindow.alert(f"Hello, {name}!")
return name
return None
# Trigger welcome dialog
welcome_btn = ui.document.getElementById("welcomeBtn")
if welcome_btn:
welcome_btn.addEventListener("click", show_welcome)
Timer and Delays
def main(ui):
status_div = ui.document.getElementById("status")
def show_message(text, duration=3000):
status_div.innerText = text
status_div.style.opacity = "1"
# Auto-hide after duration
def hide_message():
status_div.style.opacity = "0"
ui.htmlwindow.setTimeout(hide_message, duration)
def start_countdown():
count = 5
countdown_display = ui.document.getElementById("countdown")
def update_countdown():
nonlocal count
if count > 0:
countdown_display.innerText = f"Starting in {count}..."
count -= 1
ui.htmlwindow.setTimeout(update_countdown, 1000)
else:
countdown_display.innerText = "Started!"
show_message("Process completed!", 2000)
update_countdown()
start_btn = ui.document.getElementById("startBtn")
if start_btn:
start_btn.addEventListener("click", start_countdown)
Auto-save with Intervals
def main(ui):
editor = ui.document.getElementById("editor")
save_status = ui.document.getElementById("saveStatus")
auto_save_interval = None
def save_content():
content = editor.value
# Simulate saving to localStorage
ui.htmlwindow.localStorage.setItem("draft", content)
save_status.innerText = "Saved"
save_status.style.color = "green"
# Clear status after 2 seconds
ui.htmlwindow.setTimeout(lambda: setattr(save_status, 'innerText', ''), 2000)
def start_auto_save():
nonlocal auto_save_interval
# Save every 30 seconds
auto_save_interval = ui.htmlwindow.setInterval(save_content, 30000)
save_status.innerText = "Auto-save enabled"
def stop_auto_save():
nonlocal auto_save_interval
if auto_save_interval:
ui.htmlwindow.clearInterval(auto_save_interval)
auto_save_interval = None
save_status.innerText = "Auto-save disabled"
# Load saved content on startup
if editor:
saved_content = ui.htmlwindow.localStorage.getItem("draft")
if saved_content:
editor.value = saved_content
# Start auto-save when user types
def on_input():
if not auto_save_interval:
start_auto_save()
editor.addEventListener("input", on_input)
# Manual save button
save_btn = ui.document.getElementById("saveBtn")
if save_btn:
save_btn.addEventListener("click", save_content)
Window Information Display
def main(ui):
def update_window_info():
info_div = ui.document.getElementById("windowInfo")
info_html = f"""
<h3>Window Information</h3>
<p><strong>Inner Size:</strong> {ui.htmlwindow.innerWidth} x {ui.htmlwindow.innerHeight}</p>
<p><strong>Outer Size:</strong> {ui.htmlwindow.outerWidth} x {ui.htmlwindow.outerHeight}</p>
<p><strong>Screen Position:</strong> ({ui.htmlwindow.screenX}, {ui.htmlwindow.screenY})</p>
<p><strong>Scroll Position:</strong> ({ui.htmlwindow.scrollX}, {ui.htmlwindow.scrollY})</p>
<p><strong>User Agent:</strong> {ui.htmlwindow.navigator.userAgent}</p>
<p><strong>Online:</strong> {'Yes' if ui.htmlwindow.navigator.onLine else 'No'}</p>
"""
info_div.innerHTML = info_html
# Update on window resize
ui.htmlwindow.addEventListener("resize", update_window_info)
ui.htmlwindow.addEventListener("scroll", update_window_info)
# Initial update
update_window_info()
# Refresh button
refresh_btn = ui.document.getElementById("refreshInfo")
if refresh_btn:
refresh_btn.addEventListener("click", update_window_info)
Smooth Scrolling
def main(ui):
def smooth_scroll_to_top():
def scroll_step():
current_scroll = ui.htmlwindow.scrollY
if current_scroll > 0:
ui.htmlwindow.scrollTo(0, current_scroll - 50)
ui.htmlwindow.requestAnimationFrame(scroll_step)
scroll_step()
def scroll_to_element(element_id):
element = ui.document.getElementById(element_id)
if element:
element.scrollIntoView(True)
# Scroll to top button
top_btn = ui.document.getElementById("scrollTopBtn")
if top_btn:
top_btn.addEventListener("click", smooth_scroll_to_top)
# Navigation links
nav_links = ui.document.getElementsByClassName("nav-link")
for link in nav_links:
target_id = link.getAttribute("data-target")
if target_id:
link.addEventListener("click", lambda: scroll_to_element(target_id))
Responsive Design Helpers
def main(ui):
def check_responsive_breakpoint():
width = ui.htmlwindow.innerWidth
if width >= 1200:
return "xl"
elif width >= 992:
return "lg"
elif width >= 768:
return "md"
elif width >= 576:
return "sm"
else:
return "xs"
def apply_responsive_layout():
breakpoint = check_responsive_breakpoint()
body = ui.document.body
# Remove existing breakpoint classes
class_list = body.className.split()
class_list = [cls for cls in class_list if not cls.startswith("bp-")]
# Add current breakpoint class
class_list.append(f"bp-{breakpoint}")
body.className = " ".join(class_list)
# Show current breakpoint
breakpoint_display = ui.document.getElementById("currentBreakpoint")
if breakpoint_display:
breakpoint_display.innerText = f"Current breakpoint: {breakpoint}"
# Apply layout on resize
ui.htmlwindow.addEventListener("resize", apply_responsive_layout)
# Initial layout
apply_responsive_layout()
# Media query matching example
def setup_media_queries():
mobile_query = ui.htmlwindow.matchMedia("(max-width: 767px)")
def handle_mobile_change(mql):
mobile_nav = ui.document.getElementById("mobileNav")
desktop_nav = ui.document.getElementById("desktopNav")
if mql.matches:
# Mobile view
if mobile_nav:
mobile_nav.style.display = "block"
if desktop_nav:
desktop_nav.style.display = "none"
else:
# Desktop view
if mobile_nav:
mobile_nav.style.display = "none"
if desktop_nav:
desktop_nav.style.display = "block"
mobile_query.addEventListener("change", handle_mobile_change)
handle_mobile_change(mobile_query) # Initial check
setup_media_queries()
Print Functionality
def main(ui):
def prepare_for_print():
# Hide non-printable elements
no_print_elements = ui.document.getElementsByClassName("no-print")
for element in no_print_elements:
element.style.display = "none"
# Add print-specific styling
print_style = ui.document.createElement("style")
print_style.innerHTML = """
@media print {
body { font-size: 12pt; }
.print-only { display: block !important; }
.no-print { display: none !important; }
}
"""
ui.document.head.appendChild(print_style)
def print_page():
prepare_for_print()
ui.htmlwindow.print()
print_btn = ui.document.getElementById("printBtn")
if print_btn:
print_btn.addEventListener("click", print_page)
Best Practices
-
Handle dialog return values: Always check return values from
confirm()
andprompt()
methods. -
Clean up timers: Always clear intervals and timeouts when they're no longer needed to prevent memory leaks.
-
Use requestAnimationFrame for animations: For smooth animations, prefer
requestAnimationFrame()
oversetTimeout()
. -
Check window properties safely: Some window properties may not be available in all contexts.
-
Responsive design: Use window dimension properties and media queries to create responsive layouts.
-
Save timer IDs: Store timer IDs returned by
setTimeout()
andsetInterval()
so you can clear them later.
# Good practice example
def main(ui):
timer_id = None
def start_timer():
nonlocal timer_id
if timer_id:
ui.htmlwindow.clearInterval(timer_id)
timer_id = ui.htmlwindow.setInterval(update_function, 1000)
def stop_timer():
nonlocal timer_id
if timer_id:
ui.htmlwindow.clearInterval(timer_id)
timer_id = None
Common Window Events
You can listen for these events on the window object:
"resize"
: Window is resized"scroll"
: Window is scrolled"beforeunload"
: Before the window unloads"load"
: Window has finished loading"focus"
: Window gains focus"blur"
: Window loses focus