run.py 1.0 KB

123456789101112131415161718192021222324252627
  1. import inspect
  2. import sys
  3. import webbrowser
  4. import uvicorn
  5. from .config import config # NOTE: before justpy
  6. import justpy as jp
  7. if not config.interactive and config.reload and not inspect.stack()[-2].filename.endswith('spawn.py'):
  8. if config.show:
  9. webbrowser.open(f'http://{config.host}:{config.port}/')
  10. uvicorn.run('nicegui:app', host=config.host, port=config.port, lifespan='on',
  11. reload=True, log_level=config.uvicorn_logging_level)
  12. sys.exit()
  13. def run(self, *,
  14. host: str = '0.0.0.0',
  15. port: int = 80,
  16. title: str = 'NiceGUI',
  17. favicon: str = 'favicon.ico',
  18. reload: bool = True,
  19. show: bool = True,
  20. uvicorn_logging_level: str = 'warning',
  21. ):
  22. if config.interactive or reload == False: # NOTE: if reload == True we already started uvicorn above
  23. if show:
  24. webbrowser.open(f'http://{host if host != "0.0.0.0" else "127.0.0.1"}:{port}/')
  25. uvicorn.run(jp.app, host=host, port=port, log_level=config.uvicorn_logging_level, lifespan='on')