ui_run_with.py 2.5 KB

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