welcome.py 849 B

123456789101112131415161718192021222324252627
  1. import os
  2. from typing import List
  3. import ifaddr
  4. from . import core, run
  5. def _get_all_ips() -> List[str]:
  6. ips: List[str] = []
  7. for adapter in ifaddr.get_adapters():
  8. ips.extend(str(i.ip) for i in adapter.ips if i.is_IPv4)
  9. return ips
  10. async def print_message() -> None:
  11. """Print a welcome message with URLs to access the NiceGUI app."""
  12. print('NiceGUI ready to go ', end='', flush=True)
  13. host = os.environ['NICEGUI_HOST']
  14. port = os.environ['NICEGUI_PORT']
  15. ips = set((await run.io_bound(_get_all_ips)) if host == '0.0.0.0' else [])
  16. ips.discard('127.0.0.1')
  17. urls = [(f'http://{ip}:{port}' if port != '80' else f'http://{ip}') for ip in ['localhost'] + sorted(ips)]
  18. core.app.urls.update(urls)
  19. if len(urls) >= 2:
  20. urls[-1] = 'and ' + urls[-1]
  21. print(f'on {", ".join(urls)}', flush=True)