Skip to content

Instantly share code, notes, and snippets.

@eli-kha
Created March 11, 2023 20:36
Show Gist options
  • Save eli-kha/06a47bfdf1e50f4cdfc3f43a199a6d2d to your computer and use it in GitHub Desktop.
Save eli-kha/06a47bfdf1e50f4cdfc3f43a199a6d2d to your computer and use it in GitHub Desktop.
A small demo of using pywebview with NiceGUI based on wrapping both Unicorn.Server and webview in processes.
#!/usr/bin/env python3
import multiprocessing
import tempfile
from fastapi import FastAPI
from uvicorn import Config, Server
import webview
from nicegui import ui
class UvicornServer(multiprocessing.Process):
def __init__(self, config: Config):
super().__init__()
self.server = Server(config=config)
self.config = config
def stop(self):
self.terminate()
def run(self, *args, **kwargs):
self.server.run()
def start_window(pipe_send, url_to_load):
def on_closed():
pipe_send.send('closed')
win = webview.create_window('Demo', url=url_to_load)
win.events.closed += on_closed
webview.start(storage_path=tempfile.mkdtemp())
app = FastAPI()
@app.get('/')
def read_root():
return {'Hello': 'World'}
@ui.page('/show')
def show():
ui.image('https://picsum.photos/id/377/640/360')
ui.run_with(app)
if __name__ == '__main__':
server_ip = "127.0.0.1"
server_port = 8080
conn_recv, conn_send = multiprocessing.Pipe()
windowsp = multiprocessing.Process(target=start_window, args=(conn_send, f'http://{server_ip}:{server_port}/show'))
windowsp.start()
config = Config("main:app", host=server_ip, port=server_port, log_level="debug")
instance = UvicornServer(config=config)
instance.start()
window_status = ''
while 'closed' not in window_status:
# get a unit of work
window_status = conn_recv.recv()
# report
print(f'got {window_status}', flush=True)
instance.stop()
@eli-kha
Copy link
Author

eli-kha commented Mar 11, 2023

This can obviously be written to be much more functional but the goal was an initial POC to share in the NiceGUI discussions.
They main goal here was co-existance of pywebview and uvicorn (both want to control the execution loop and be the main thread) and making sure the webserver closes when you close the window.

If you want more dynamic control over the webview windows, you'll need to make the pip duplex and implement a control protocol via pipe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment