run_with.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 .app import App
  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. globals.ui_run_has_been_called = True
  37. globals.title = title
  38. globals.viewport = viewport
  39. globals.favicon = favicon
  40. globals.dark = dark
  41. globals.language = language
  42. globals.binding_refresh_interval = binding_refresh_interval
  43. globals.reconnect_timeout = reconnect_timeout
  44. globals.tailwind = tailwind
  45. globals.prod_js = prod_js
  46. storage.set_storage_secret(storage_secret)
  47. app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))
  48. app.on_event('shutdown')(handle_shutdown)
  49. app.mount(mount_path, globals.app)