3 ms·
# webview_python A lightweight Python binding for creating native GUI applications using web technologies. Uses WebView2 on Windows and native webview implemen
by congzhangzh 2y ago
# webview_python
A lightweight Python binding for creating native GUI applications using web technologies. Uses WebView2 on Windows and native webview implementations on other platforms.
## Features
- Cross-platform support:
- Windows: Microsoft Edge WebView2 engine
- macOS: WebKit
- Linux: GTK-based WebKit
- Simple and intuitive Python API
- High performance with minimal resource usage
- Two-way JavaScript bridge
- Easy installation with minimal dependencies
## Why Another Binding?
Existing Python WebView solutions have limitations:
- [webview-python](https://github.com/zserge/webview-python https://github.com/zserge/webview-python) - Hasn't been updated in 6 years, compilation issues, doesn't use WebView2
- [pywebview](https://github.com/r0x0r/pywebview https://github.com/r0x0r/pywebview) - Complex setup and installation issues
## Installation
```bash
pip install webview_python
```
## Usage
### Display Inline HTML
```python
from webview.webview import Webview
from urllib.parse import quote
html = """
<html>
<body>
<h1>Hello from Python Webview!</h1>
</body>
</html>
"""
webview = Webview()
webview.navigate(f"data:text/html,{quote(html)}")
webview.run()
```
### Load Local HTML File
```python
from webview.webview import Webview
import os
webview = Webview()
current_dir = os.path.dirname(os.path.abspath(__file__))
html_path = os.path.join(current_dir, 'local.html')
webview.navigate(f"file://{html_path}")
webview.run()
```
### Load Remote URL
```python
from webview.webview import Webview
webview = Webview()
webview.navigate("https://www.python.org https://www.python.org")
webview.run()
```
### Python-JavaScript Bindings
```python
from webview.webview import Webview, Size, SizeHint
from urllib.parse import quote
webview = Webview(debug=True)
# Python functions that can be called from JavaScript
def hello():
webview.eval("updateFromPython('Hello from Python!')")
return "Hello from Python!"
def add(a, b):
return a + b
# Bind Python functions
webview.bind("hello", hello)
webview.bind("add", add)
# Configure window
webview.title = "Python-JavaScript Binding Demo"
webview.size = Size(640, 480, SizeHint.FIXED)
# Load HTML with JavaScript
html = """
<html>
<head>
<title>Python-JavaScript Binding Demo</title>
<script>
async function callPython() {
const result = await hello();
document.getElementById('result').innerHTML = result;
}
async function callPythonWithArgs() {
const result = await add(40, 2);
document.getElementById('result').innerHTML = `Result: ${result}`;
}
function updateFromPython(message) {
document.getElementById('result').innerHTML = `Python says: ${message}`;
}
</script>
</head>
<body>
<h1>Python-JavaScript Binding Demo</h1>
<button onclick="callPython()">Call Python</button>
<button onclick="callPythonWithArgs()">Call Python with Args</button>
<div id="result"></div>
</body>
</html>
"""
webview.navigate(f"data:text/html,{quote(html)}")
webview.run()
```
## API Overview
- `Webview()`: Create a webview instance
- `webview.navigate(url)`: Navigate to URL or load HTML content
- `webview.bind(name, func)`: Bind Python function to JavaScript
- `webview.eval(js_code)`: Execute JavaScript code
- `webview.run()`: Start the webview main loop
- `Size(width, height, hint)`: Configure window size
- `SizeHint`: Window size hint constants
## Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.