run_with.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from pathlib import Path
  2. from typing import Optional, Union
  3. from fastapi import FastAPI
  4. from nicegui import globals
  5. from nicegui.helpers import set_storage_secret
  6. from nicegui.language import Language
  7. from nicegui.nicegui import handle_shutdown, handle_startup
  8. def run_with(
  9. app: FastAPI, *,
  10. title: str = 'NiceGUI',
  11. viewport: str = 'width=device-width, initial-scale=1',
  12. favicon: Optional[Union[str, Path]] = None,
  13. dark: Optional[bool] = False,
  14. language: Language = 'en-US',
  15. binding_refresh_interval: float = 0.1,
  16. exclude: str = '',
  17. mount_path: str = '/',
  18. storage_secret: Optional[str] = None,
  19. ) -> None:
  20. globals.ui_run_has_been_called = True
  21. globals.title = title
  22. globals.viewport = viewport
  23. globals.favicon = favicon
  24. globals.dark = dark
  25. globals.language = language
  26. globals.binding_refresh_interval = binding_refresh_interval
  27. globals.excludes = [e.strip() for e in exclude.split(',')]
  28. globals.tailwind = True
  29. set_storage_secret(storage_secret)
  30. app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))
  31. app.on_event('shutdown')(lambda: handle_shutdown())
  32. app.mount(mount_path, globals.app)