_page.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. from __future__ import annotations
  12. import typing as t
  13. from abc import ABC, abstractmethod
  14. from ..page import Page as BasePage
  15. from ..utils.singleton import _Singleton
  16. class Page(BasePage):
  17. """NOT DOCUMENTED
  18. A custom page for external application that can be added to Taipy GUI"""
  19. def __init__(
  20. self,
  21. resource_handler: ResourceHandler,
  22. binding_variables: t.Optional[t.List[str]] = None,
  23. metadata: t.Optional[t.Dict[str, t.Any]] = None,
  24. **kwargs,
  25. ) -> None:
  26. if binding_variables is None:
  27. binding_variables = []
  28. if metadata is None:
  29. metadata = {}
  30. super().__init__(**kwargs)
  31. self._resource_handler = resource_handler
  32. self._binding_variables = binding_variables
  33. self._metadata: t.Dict[str, t.Any] = metadata
  34. class ResourceHandler(ABC):
  35. """NOT DOCUMENTED
  36. Resource handler for custom pages.
  37. User can implement this class to provide custom resources for the custom pages
  38. """
  39. rh_id: str = ""
  40. def __init__(self) -> None:
  41. _ExternalResourceHandlerManager().register(self)
  42. def get_id(self) -> str:
  43. return self.rh_id if self.rh_id != "" else str(id(self))
  44. @abstractmethod
  45. def get_resources(self, path: str, taipy_resource_path: str) -> t.Any:
  46. raise NotImplementedError
  47. class _ExternalResourceHandlerManager(object, metaclass=_Singleton):
  48. """NOT DOCUMENTED
  49. Manager for resource handlers.
  50. This class is used to manage resource handlers for custom pages
  51. """
  52. def __init__(self) -> None:
  53. self.__handlers: t.Dict[str, ResourceHandler] = {}
  54. def register(self, handler: ResourceHandler) -> None:
  55. """Register a resource handler
  56. Arguments:
  57. handler (ResourceHandler): The resource handler to register
  58. """
  59. self.__handlers[handler.get_id()] = handler
  60. def get(self, id: str) -> t.Optional[ResourceHandler]:
  61. """Get a resource handler by its id
  62. Arguments:
  63. id (str): The id of the resource handler
  64. Returns:
  65. ResourceHandler: The resource handler
  66. """
  67. return self.__handlers.get(id, None)
  68. def get_all(self) -> t.List[ResourceHandler]:
  69. """Get all resource handlers
  70. Returns:
  71. List[ResourceHandler]: The list of resource handlers
  72. """
  73. return list(self.__handlers.values())