1
0

example_library.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  64. # The implementation of the rendering for the "fraction" static element
  65. @staticmethod
  66. def _fraction_render(props: dict) -> str:
  67. # Get the property values
  68. numerator = props.get("numerator")
  69. denominator = props.get("denominator")
  70. # No denominator or numerator is 0: display the numerator
  71. if denominator is None or int(numerator) == 0: # type: ignore[arg-type]
  72. return f"<span>{numerator}</span>"
  73. # Denominator is zero: display infinity
  74. if int(denominator) == 0:
  75. return '<span style="font-size: 1.6em">&#8734;</span>'
  76. # 'Normal' case
  77. return f"<span><sup>{numerator}</sup>/<sub>{denominator}</sub></span>"
  78. def get_name(self) -> str:
  79. return "example"
  80. def get_elements(self) -> dict:
  81. return self.elements
  82. def get_scripts(self) -> list[str]:
  83. # Only one JavaScript bundle for this library.
  84. return [
  85. "front-end/dist/exampleLibrary.js",
  86. "front-end/scripts/logoAnimation.js",
  87. ]