-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import sys | ||
import asyncio | ||
from pathlib import Path | ||
|
||
sys.path.append(str(Path(__file__).parent.parent)) | ||
from main import WebView, WebViewOptions, WebViewContentHtml, WebViewNotification | ||
|
||
|
||
async def main(): | ||
print("Creating webview") | ||
config = WebViewOptions( | ||
title="Load Html Example", | ||
load=WebViewContentHtml( | ||
html="<h1>Initial html</h1>", | ||
# Note: This origin is used with a custom protocol so it doesn't match | ||
# https://example.com. This doesn't need to be set, but can be useful if | ||
# you want to control resources that are scoped to a specific origin like | ||
# local storage or indexeddb. | ||
origin="example.com", | ||
), | ||
devtools=True, | ||
) | ||
|
||
async with WebView(config) as webview: | ||
|
||
async def handle_start(event: WebViewNotification): | ||
await webview.open_devtools() | ||
await webview.load_html("<h1>Updated html!</h1>") | ||
|
||
webview.on("started", handle_start) | ||
|
||
print("Webview closed") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
import asyncio | ||
import time | ||
from pathlib import Path | ||
|
||
sys.path.append(str(Path(__file__).parent.parent)) | ||
from main import WebView, WebViewOptions, WebViewContentUrl, WebViewNotification | ||
|
||
|
||
async def main(): | ||
print("Creating webview") | ||
config = WebViewOptions( | ||
title="Load Url Example", | ||
load=WebViewContentUrl( | ||
url="https://example.com", | ||
headers={ | ||
"Content-Type": "text/html", | ||
}, | ||
), | ||
userAgent="curl/7.81.0", | ||
devtools=True, | ||
) | ||
|
||
async with WebView(config) as webview: | ||
|
||
async def handle_start(event: WebViewNotification): | ||
await webview.open_devtools() | ||
await asyncio.sleep(2) # Sleep for 2 seconds | ||
await webview.load_url( | ||
"https://val.town/", | ||
headers={ | ||
"Content-Type": "text/html", | ||
}, | ||
) | ||
|
||
webview.on("started", handle_start) | ||
|
||
print("Webview closed") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sys | ||
import asyncio | ||
from pathlib import Path | ||
|
||
sys.path.append(str(Path(__file__).parent.parent)) | ||
from main import WebView, WebViewOptions, WebViewContentHtml, IpcNotification | ||
|
||
|
||
async def main(): | ||
print("Creating webview") | ||
config = WebViewOptions( | ||
title="Window Size", | ||
load=WebViewContentHtml( | ||
html=""" | ||
<h1>Window Sizes</h1> | ||
<div style="display: flex; gap: 10px;"> | ||
<button onclick="window.ipc.postMessage('maximize')">Maximize</button> | ||
<button onclick="window.ipc.postMessage('minimize')">Minimize</button> | ||
<button onclick="window.ipc.postMessage('fullscreen')">Fullscreen</button> | ||
</div> | ||
""" | ||
), | ||
size={ | ||
"width": 800, | ||
"height": 200 | ||
}, | ||
ipc=True | ||
) | ||
|
||
async with WebView(config) as webview: | ||
async def handle_ipc(event: IpcNotification): | ||
message = event.message | ||
if message == "maximize": | ||
await webview.maximize() | ||
elif message == "minimize": | ||
await webview.minimize() | ||
elif message == "fullscreen": | ||
await webview.fullscreen() | ||
else: | ||
print(f"Unknown message: {message}") | ||
|
||
webview.on("ipc", handle_ipc) | ||
|
||
print("Webview closed") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |