_run.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import sys
  12. import typing as t
  13. from flask import Flask
  14. from taipy.core import Orchestrator
  15. from taipy.gui import Gui
  16. from taipy.rest import Rest
  17. if sys.version_info >= (3, 10):
  18. from typing import TypeGuard
  19. _AppType = t.Union[Gui, Rest, Orchestrator]
  20. _AppTypeT = t.TypeVar("_AppTypeT", Gui, Rest, Orchestrator)
  21. def _run(*services: _AppType, **kwargs) -> t.Optional[Flask]:
  22. """Run one or multiple Taipy services.
  23. A Taipy service is an instance of a class that runs code as a Web application.
  24. Parameters:
  25. *services (Union[`Gui^`, `Rest^`, `Orchestrator^`]): Services to run, as separate arguments.<br/>
  26. If several services are provided, all the services run simultaneously.<br/>
  27. If this is empty or set to None, this method does nothing.
  28. **kwargs: Other parameters to provide to the services.
  29. """
  30. gui = __get_app(services, Gui)
  31. rest = __get_app(services, Rest)
  32. orchestrator = __get_app(services, Orchestrator)
  33. if gui and orchestrator:
  34. from taipy.core._cli._core_cli_factory import _CoreCLIFactory
  35. from taipy.gui._gui_cli import _GuiCLI
  36. _CoreCLIFactory._build_cli().create_parser()
  37. _GuiCLI.create_parser()
  38. if rest or orchestrator:
  39. if not orchestrator:
  40. orchestrator = Orchestrator()
  41. orchestrator.run()
  42. if not rest and not gui:
  43. return None
  44. if gui and rest:
  45. gui._set_flask(rest._app) # type: ignore[union-attr]
  46. return gui.run(**kwargs)
  47. else:
  48. app = rest or gui
  49. assert app is not None # Avoid pyright typing error
  50. return app.run(**kwargs)
  51. if sys.version_info >= (3, 10):
  52. def __get_app(apps: t.Tuple[_AppType, ...], type_: t.Type[_AppTypeT]) -> t.Optional[_AppType]:
  53. def filter_isinstance(tl: _AppType) -> TypeGuard[_AppTypeT]:
  54. return isinstance(tl, type_)
  55. return next(filter(filter_isinstance, apps), None)
  56. else:
  57. def __get_app(apps: t.Tuple[_AppType, ...], type_: t.Type[_AppTypeT]) -> t.Optional[_AppType]:
  58. return next(filter(lambda a: isinstance(a, type_), apps), None)