|
| 1 | +from webui import webui # GUI |
| 2 | +import socket # To get local IP |
| 3 | + |
| 4 | + |
| 5 | +def get_local_ip(): |
| 6 | + # The IP address of the local machine is found by creating a socket connection. |
| 7 | + # The socket connects to an external address, but does not send any data. |
| 8 | + try: |
| 9 | + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 10 | + s.connect(("8.8.8.8", 80)) |
| 11 | + local_ip = s.getsockname()[0] |
| 12 | + except Exception: |
| 13 | + # Failed, return 'localhost' |
| 14 | + local_ip = 'localhost' |
| 15 | + finally: |
| 16 | + s.close() |
| 17 | + return local_ip |
| 18 | + |
| 19 | +def all_events(e : webui.event): |
| 20 | + if e.event_type == webui.eventType.CONNECTED: |
| 21 | + print('Connected.') |
| 22 | + if e.event_type == webui.eventType.DISCONNECTED: |
| 23 | + print('Disconnected.') |
| 24 | + |
| 25 | +def exit(e : webui.event): |
| 26 | + webui.exit() |
| 27 | + |
| 28 | +def main(): |
| 29 | + html = """ |
| 30 | + <html> |
| 31 | + <script src="webui.js"></script> |
| 32 | + Hello! This is a public UI.<br> |
| 33 | + <br> |
| 34 | + <button id="Exit">Exit</button> |
| 35 | + </html> |
| 36 | + """ |
| 37 | + |
| 38 | + # New window |
| 39 | + MyWindow = webui.window() |
| 40 | + |
| 41 | + # Make the window URL accessible from public networks |
| 42 | + MyWindow.set_public(True) |
| 43 | + |
| 44 | + # Wait forever (Otherwise WebUI will timeout after 30s) |
| 45 | + webui.set_timeout(0) |
| 46 | + |
| 47 | + # Bind |
| 48 | + MyWindow.bind('', all_events) |
| 49 | + MyWindow.bind('Exit', exit) |
| 50 | + |
| 51 | + # Start the window without any browser |
| 52 | + MyWindow.show(html, webui.browser.NoBrowser) |
| 53 | + |
| 54 | + # Get URL of the window |
| 55 | + url = MyWindow.get_url() |
| 56 | + |
| 57 | + # Get local IP |
| 58 | + local_ip = get_local_ip() |
| 59 | + |
| 60 | + # Replace `localhost` with IP |
| 61 | + link = url.replace('localhost', local_ip) |
| 62 | + |
| 63 | + # Print |
| 64 | + print(f'The UI link is: {link}') |
| 65 | + |
| 66 | + # Wait until all windows are closed |
| 67 | + webui.wait() |
| 68 | + print('Thank you.') |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments