Pywebview
Pywebview is a lightweight cross-platform wrapper for webview components, allowing you to build desktop GUI applications with HTML, CSS, and JavaScript. It abstracts away platform-specific webview implementations (Edge Chromium, Cocoa WebKit, GTK, QT, WinForms, etc.) providing a consistent Python API. The current version is 6.2 and it follows an active release cadence with frequent updates and bug fixes.
Warnings
- breaking Pywebview versions 6.x and higher require Python 3.8 or newer. Applications targeting older Python versions must use pywebview 5.x or earlier.
- gotcha Pywebview relies on native web rendering engines (e.g., WebView2 on Windows, WebKit on macOS, WebKitGTK/QtWebEngine on Linux). Missing system dependencies or an outdated browser component can lead to `webview.start()` failures, blank windows, or unexpected rendering issues.
- gotcha The `webview.start()` function is blocking by default. Integrating pywebview with existing asynchronous code or other GUI toolkits often requires running `webview.start()` in a separate thread or using advanced non-blocking patterns.
- gotcha When bundling applications with PyInstaller, specific configurations and hooks might be necessary to ensure pywebview's bundled resources and dynamic libraries are correctly included, especially for different webview backends. Failures can manifest as blank windows, crashes, or missing assets.
Install
-
pip install pywebview
Imports
- webview
import webview
Quickstart
import webview
def hello_world(window):
"""Function to be executed after the window is ready."""
# Expose a Python function to JavaScript
def get_message():
return "Hello from Python!"
window.expose(get_message)
# Execute JavaScript after the window is fully loaded
# Access the exposed Python function via window.pywebview.api
window.evaluate_js(
"document.getElementById('message').innerText = 'Loading...';"
)
window.evaluate_js(
"window.pywebview.api.get_message().then(result => {"
"document.getElementById('message').innerText = result;"
"});"
)
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Pywebview App</title>
</head>
<body>
<h1>Pywebview Example</h1>
<p id="message">Waiting for Python to load...</p>
</body>
</html>
"""
if __name__ == '__main__':
# Create a webview window
window = webview.create_window(
'My First Pywebview App',
html=html_content,
width=800,
height=600
)
# Start the webview application.
# The 'hello_world' function will be called when the window is ready.
webview.start(hello_world, window)