ui_run_with.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from pathlib import Path
  2. from typing import Optional, Union
  3. from fastapi import FastAPI
  4. from . import globals, storage # pylint: disable=redefined-builtin
  5. from .language import Language
  6. from .nicegui import handle_shutdown, handle_startup
  7. def run_with(
  8. app: FastAPI, *,
  9. title: str = 'NiceGUI',
  10. viewport: str = 'width=device-width, initial-scale=1',
  11. favicon: Optional[Union[str, Path]] = None,
  12. dark: Optional[bool] = False,
  13. language: Language = 'en-US',
  14. binding_refresh_interval: float = 0.1,
  15. reconnect_timeout: float = 3.0,
  16. mount_path: str = '/',
  17. tailwind: bool = True,
  18. prod_js: bool = True,
  19. storage_secret: Optional[str] = None,
  20. ) -> None:
  21. """Run NiceGUI with FastAPI.
  22. :param app: FastAPI app
  23. :param title: page title (default: `'NiceGUI'`, can be overwritten per page)
  24. :param viewport: page meta viewport content (default: `'width=device-width, initial-scale=1'`, can be overwritten per page)
  25. :param favicon: relative filepath, absolute URL to a favicon (default: `None`, NiceGUI icon will be used) or emoji (e.g. `'🚀'`, works for most browsers)
  26. :param dark: whether to use Quasar's dark mode (default: `False`, use `None` for "auto" mode)
  27. :param language: language for Quasar elements (default: `'en-US'`)
  28. :param binding_refresh_interval: time between binding updates (default: `0.1` seconds, bigger is more CPU friendly)
  29. :param reconnect_timeout: maximum time the server waits for the browser to reconnect (default: 3.0 seconds)
  30. :param mount_path: mount NiceGUI at this path (default: `'/'`)
  31. :param tailwind: whether to use Tailwind CSS (experimental, default: `True`)
  32. :param prod_js: whether to use the production version of Vue and Quasar dependencies (default: `True`)
  33. :param storage_secret: secret key for browser-based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
  34. """
  35. globals.ui_run_has_been_called = True
  36. globals.title = title
  37. globals.viewport = viewport
  38. globals.favicon = favicon
  39. globals.dark = dark
  40. globals.language = language
  41. globals.binding_refresh_interval = binding_refresh_interval
  42. globals.reconnect_timeout = reconnect_timeout
  43. globals.tailwind = tailwind
  44. globals.prod_js = prod_js
  45. storage.set_storage_secret(storage_secret)
  46. app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))
  47. app.on_event('shutdown')(handle_shutdown)
  48. app.mount(mount_path, globals.app)