Browse Source

#855 include all IPs in welcome message

Falko Schindler 2 years ago
parent
commit
83203d6bd7
2 changed files with 17 additions and 3 deletions
  1. 14 1
      nicegui/nicegui.py
  2. 3 2
      nicegui/run.py

+ 14 - 1
nicegui/nicegui.py

@@ -1,3 +1,4 @@
+import socket
 import asyncio
 import os
 import time
@@ -76,7 +77,19 @@ def handle_startup(with_welcome_message: bool = True) -> None:
     background_tasks.create(prune_slot_stacks())
     globals.state = globals.State.STARTED
     if with_welcome_message:
-        print(f'NiceGUI ready to go on {os.environ["NICEGUI_URL"]}')
+        print_welcome_message()
+
+
+def print_welcome_message():
+    host = os.environ['NICEGUI_HOST']
+    port = os.environ['NICEGUI_PORT']
+    ips = ['localhost']
+    if host == '0.0.0.0':
+        ips += set(ip for _, _, _, _, (ip, _) in socket.getaddrinfo(socket.gethostname(), None))
+    addresses = [f'http://{ip}:{port}'.removesuffix(':80') for ip in ips]
+    if len(addresses) >= 2:
+        addresses[-1] = 'and ' + addresses[-1]
+    print(f'NiceGUI ready to go on {", ".join(addresses)}')
 
 
 @app.on_event('shutdown')

+ 3 - 2
nicegui/run.py

@@ -88,8 +88,9 @@ def run(*,
     else:
         host = host or '0.0.0.0'
 
-    # NOTE: We save the URL in an environment variable so the subprocess started in reload mode can access it.
-    os.environ['NICEGUI_URL'] = f'http://{host}:{port}'
+    # NOTE: We save host and port in environment variables so the subprocess started in reload mode can access them.
+    os.environ['NICEGUI_HOST'] = host
+    os.environ['NICEGUI_PORT'] = str(port)
 
     if show:
         helpers.schedule_browser(host, port)