types.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright 2021-2025 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 typing as t
  12. from enum import Enum
  13. from inspect import isclass
  14. from .data import Decimator
  15. from .utils import (
  16. _TaipyBase,
  17. _TaipyBool,
  18. _TaipyContent,
  19. _TaipyContentHtml,
  20. _TaipyContentImage,
  21. _TaipyData,
  22. _TaipyDate,
  23. _TaipyDateRange,
  24. _TaipyDict,
  25. _TaipyLoNumbers,
  26. _TaipyLov,
  27. _TaipyLovValue,
  28. _TaipyNumber,
  29. _TaipyTime,
  30. _TaipyToJson,
  31. )
  32. class _WsType(Enum):
  33. ACTION = "A"
  34. MULTIPLE_UPDATE = "MU"
  35. REQUEST_UPDATE = "RU"
  36. DATA_UPDATE = "DU"
  37. UPDATE = "U"
  38. ALERT = "AL"
  39. BLOCK = "BL"
  40. NAVIGATE = "NA"
  41. CLIENT_ID = "ID"
  42. APP_ID = "AID"
  43. MULTIPLE_MESSAGE = "MS"
  44. DOWNLOAD_FILE = "DF"
  45. PARTIAL = "PR"
  46. ACKNOWLEDGEMENT = "ACK"
  47. GET_MODULE_CONTEXT = "GMC"
  48. GET_DATA_TREE = "GDT"
  49. GET_ROUTES = "GR"
  50. FAVICON = "FV"
  51. BROADCAST = "BC"
  52. LOCAL_STORAGE = "LS"
  53. NumberTypes = {"int", "int64", "float", "float64"}
  54. class PropertyType(Enum):
  55. """
  56. All the possible element property types.
  57. This is used when creating custom visual elements, where you have
  58. to indicate the type of each property.
  59. Some types are 'dynamic', meaning that if the property value is modified, it
  60. is automatically handled by Taipy and propagated to the entire application.
  61. See `ElementProperty^` for more details.
  62. """
  63. any = "any"
  64. """
  65. The property holds a value of any serializable type.
  66. """
  67. dynamic_any = "dynamicany"
  68. """
  69. The property is dynamic and holds a value of any serializable type.
  70. """
  71. boolean = "boolean"
  72. """
  73. The property holds a Boolean value.
  74. """
  75. toHtmlContent = _TaipyContentHtml
  76. content = _TaipyContent
  77. data = _TaipyData
  78. date = _TaipyDate
  79. date_range = _TaipyDateRange
  80. dict = "dict"
  81. time = _TaipyTime
  82. """
  83. The property holds a dictionary.
  84. """
  85. dynamic_date = "dynamicdate"
  86. """
  87. The property is dynamic and holds a date.
  88. """
  89. dynamic_dict = _TaipyDict
  90. """
  91. The property is dynamic and holds a dictionary.
  92. """
  93. dynamic_number = _TaipyNumber
  94. """
  95. The property is dynamic and holds a number.
  96. """
  97. dynamic_lo_numbers = _TaipyLoNumbers
  98. """
  99. The property is dynamic and holds a list of numbers.
  100. """
  101. dynamic_boolean = _TaipyBool
  102. """
  103. The property is dynamic and holds a Boolean value.
  104. """
  105. dynamic_list = "dynamiclist"
  106. """
  107. The property is dynamic and holds a list.
  108. The React component must have two parameters: "<propertyName>" that must be a list of object, and
  109. "default<PropertyName>" that must be a string, set to the JSON representation of the initial value
  110. of the property.
  111. """
  112. dynamic_string = "dynamicstring"
  113. """
  114. The property is dynamic and holds a string.
  115. """
  116. function = "function"
  117. """
  118. The property holds a reference to a function.
  119. """
  120. image = _TaipyContentImage
  121. json = "json"
  122. single_lov = "singlelov"
  123. lov = _TaipyLov
  124. lov_no_default = "lovnodefault"
  125. """
  126. The property holds a LoV (list of values).
  127. """
  128. lov_value = _TaipyLovValue
  129. """
  130. The property holds a value in a LoV (list of values).
  131. """
  132. number = "number"
  133. """
  134. The property holds a number.
  135. """
  136. react = "react"
  137. broadcast = "broadcast"
  138. string = "string"
  139. """
  140. The property holds a string.
  141. """
  142. string_or_number = "string|number"
  143. """
  144. The property holds a string or a number.
  145. This is typically used to handle CSS dimension values, where a unit can be used.
  146. """
  147. boolean_or_list = "boolean|list"
  148. slider_value = "number|number[]|lovValue"
  149. toggle_value = "boolean|lovValue"
  150. string_list = "stringlist"
  151. decimator = Decimator
  152. """
  153. The property holds an inner attributes that is defined by a library and cannot be overridden by the user.
  154. """
  155. inner = "inner"
  156. to_json = _TaipyToJson
  157. @t.overload # noqa: F811
  158. def _get_taipy_type(a_type: None) -> None: ...
  159. @t.overload
  160. def _get_taipy_type(a_type: t.Type[_TaipyBase]) -> t.Type[_TaipyBase]: # noqa: F811
  161. ...
  162. @t.overload
  163. def _get_taipy_type(a_type: PropertyType) -> t.Type[_TaipyBase]: # noqa: F811
  164. ...
  165. @t.overload
  166. def _get_taipy_type( # noqa: F811
  167. a_type: t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]],
  168. ) -> t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]]: ...
  169. def _get_taipy_type( # noqa: F811
  170. a_type: t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]],
  171. ) -> t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]]:
  172. if a_type is None:
  173. return None
  174. if isinstance(a_type, PropertyType) and not isinstance(a_type.value, str):
  175. return a_type.value
  176. if isclass(a_type) and not isinstance(a_type, PropertyType) and issubclass(a_type, _TaipyBase):
  177. return a_type
  178. if a_type == PropertyType.boolean:
  179. return _TaipyBool
  180. elif a_type == PropertyType.number:
  181. return _TaipyNumber
  182. return None