Skip to content

Commit

Permalink
Add remaining examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Feb 7, 2025
1 parent 6db668b commit 66ebef0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/clients/python/examples/load_html.py
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())
42 changes: 42 additions & 0 deletions src/clients/python/examples/load_url.py
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())
48 changes: 48 additions & 0 deletions src/clients/python/examples/window_size.py
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())

0 comments on commit 66ebef0

Please sign in to comment.