welcome.py 915 B

1234567891011121314151617181920212223242526272829
  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 collect_urls() -> None:
  11. """Print a welcome message with URLs to access the NiceGUI app."""
  12. host = os.environ.get('NICEGUI_HOST')
  13. port = os.environ.get('NICEGUI_PORT')
  14. if not host or not port:
  15. return
  16. ips = set((await run.io_bound(_get_all_ips)) if host == '0.0.0.0' else [])
  17. ips.discard('127.0.0.1')
  18. urls = [(f'http://{ip}:{port}' if port != '80' else f'http://{ip}') for ip in ['localhost', *sorted(ips)]]
  19. core.app.urls.update(urls)
  20. if len(urls) >= 2:
  21. urls[-1] = 'and ' + urls[-1]
  22. if core.app.config.show_welcome_message:
  23. print(f'NiceGUI ready to go on {", ".join(urls)}', flush=True)