example_library.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 base64
  12. from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType
  13. class ExampleLibrary(ElementLibrary):
  14. def __init__(self) -> None:
  15. # Initialize the set of visual elements for this extension library
  16. logo_path = self.get_resource("assets/logo.png")
  17. with open(logo_path, "rb") as f:
  18. logo_base64 = base64.b64encode(f.read()).decode("utf-8")
  19. self.elements = {
  20. # A static element that displays its properties in a fraction
  21. "fraction": Element(
  22. "numerator",
  23. {
  24. "numerator": ElementProperty(PropertyType.number),
  25. "denominator": ElementProperty(PropertyType.number),
  26. },
  27. render_xhtml=ExampleLibrary._fraction_render,
  28. ),
  29. # A dynamic element that decorates its value
  30. "label": Element(
  31. "value",
  32. {"value": ElementProperty(PropertyType.dynamic_string)},
  33. # The name of the React component (ColoredLabel) that implements this custom
  34. # element, exported as ExampleLabel in front-end/src/index.ts
  35. react_component="ExampleLabel",
  36. ),
  37. "game_table": Element(
  38. "data",
  39. {
  40. "data": ElementProperty(PropertyType.data),
  41. },
  42. # The name of the React component (GameTable) that implements this custom
  43. # element, exported as GameTable in front-end/src/index.ts
  44. # react_component="GameTable",
  45. ),
  46. "visual_label_list": Element(
  47. "lov",
  48. {
  49. "lov": ElementProperty(PropertyType.lov),
  50. "sort": ElementProperty(PropertyType.string),
  51. },
  52. # The name of the React component (VisualLabelList) that implements this custom
  53. # element, exported as LabeledItemList in front-end/src/index.ts
  54. react_component="VisualLabelList",
  55. ),
  56. "logo_with_text": Element(
  57. "text",
  58. {
  59. "text": ElementProperty(PropertyType.string),
  60. "logo_path": ElementProperty(PropertyType.string, default_value=logo_base64),
  61. },
  62. ),
  63. "dashboard": Element(
  64. "data",
  65. {
  66. "data": ElementProperty(PropertyType.dict),
  67. "layout": ElementProperty(PropertyType.dict),
  68. },
  69. )
  70. }
  71. # The implementation of the rendering for the "fraction" static element
  72. @staticmethod
  73. def _fraction_render(props: dict) -> str:
  74. # Get the property values
  75. numerator = props.get("numerator")
  76. denominator = props.get("denominator")
  77. # No denominator or numerator is 0: display the numerator
  78. if denominator is None or int(numerator) == 0: # type: ignore[arg-type]
  79. return f"<span>{numerator}</span>"
  80. # Denominator is zero: display infinity
  81. if int(denominator) == 0:
  82. return '<span style="font-size: 1.6em">&#8734;</span>'
  83. # 'Normal' case
  84. return f"<span><sup>{numerator}</sup>/<sub>{denominator}</sub></span>"
  85. def get_name(self) -> str:
  86. return "example"
  87. def get_elements(self) -> dict:
  88. return self.elements
  89. def get_scripts(self) -> list[str]:
  90. # Only one JavaScript bundle for this library.
  91. return [
  92. "front-end/dist/exampleLibrary.js",
  93. "front-end/scripts/logoAnimation.js",
  94. ]