ui_run_with.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from contextlib import asynccontextmanager
  2. from pathlib import Path
  3. from typing import Literal, Optional, Union
  4. from fastapi import FastAPI
  5. from fastapi.middleware.gzip import GZipMiddleware
  6. from . import core, storage
  7. from .air import Air
  8. from .language import Language
  9. from .middlewares import RedirectWithPrefixMiddleware
  10. from .nicegui import _shutdown, _startup
  11. def run_with(
  12. app: FastAPI, *,
  13. title: str = 'NiceGUI',
  14. viewport: str = 'width=device-width, initial-scale=1',
  15. favicon: Optional[Union[str, Path]] = None,
  16. dark: Optional[bool] = False,
  17. language: Language = 'en-US',
  18. binding_refresh_interval: float = 0.1,
  19. reconnect_timeout: float = 3.0,
  20. message_history_length: int = 1000,
  21. mount_path: str = '/',
  22. on_air: Optional[Union[str, Literal[True]]] = None,
  23. tailwind: bool = True,
  24. prod_js: bool = True,
  25. storage_secret: Optional[str] = None,
  26. show_welcome_message: bool = True,
  27. ) -> None:
  28. """Run NiceGUI with FastAPI.
  29. :param app: FastAPI app
  30. :param title: page title (default: `'NiceGUI'`, can be overwritten per page)
  31. :param viewport: page meta viewport content (default: `'width=device-width, initial-scale=1'`, can be overwritten per page)
  32. :param favicon: relative filepath, absolute URL to a favicon (default: `None`, NiceGUI icon will be used) or emoji (e.g. `'🚀'`, works for most browsers)
  33. :param dark: whether to use Quasar's dark mode (default: `False`, use `None` for "auto" mode)
  34. :param language: language for Quasar elements (default: `'en-US'`)
  35. :param binding_refresh_interval: time between binding updates (default: `0.1` seconds, bigger is more CPU friendly)
  36. :param reconnect_timeout: maximum time the server waits for the browser to reconnect (default: 3.0 seconds)
  37. :param message_history_length: maximum number of messages that will be stored and resent after a connection interruption (default: 1000, use 0 to disable)
  38. :param mount_path: mount NiceGUI at this path (default: `'/'`)
  39. :param on_air: tech preview: `allows temporary remote access <https://nicegui.io/documentation/section_configuration_deployment#nicegui_on_air>`_ if set to `True` (default: disabled)
  40. :param tailwind: whether to use Tailwind CSS (experimental, default: `True`)
  41. :param prod_js: whether to use the production version of Vue and Quasar dependencies (default: `True`)
  42. :param storage_secret: secret key for browser-based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
  43. :param show_welcome_message: whether to show the welcome message (default: `True`)
  44. """
  45. core.app.config.add_run_config(
  46. reload=False,
  47. title=title,
  48. viewport=viewport,
  49. favicon=favicon,
  50. dark=dark,
  51. language=language,
  52. binding_refresh_interval=binding_refresh_interval,
  53. reconnect_timeout=reconnect_timeout,
  54. message_history_length=message_history_length,
  55. tailwind=tailwind,
  56. prod_js=prod_js,
  57. show_welcome_message=show_welcome_message,
  58. )
  59. storage.set_storage_secret(storage_secret)
  60. core.app.add_middleware(GZipMiddleware)
  61. core.app.add_middleware(RedirectWithPrefixMiddleware)
  62. if on_air:
  63. core.air = Air('' if on_air is True else on_air)
  64. app.mount(mount_path, core.app)
  65. main_app_lifespan = app.router.lifespan_context
  66. @asynccontextmanager
  67. async def lifespan_wrapper(app):
  68. await _startup()
  69. async with main_app_lifespan(app) as state:
  70. yield state
  71. await _shutdown()
  72. app.router.lifespan_context = lifespan_wrapper