example_library.py 3.4 KB

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