1
0

example_library.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType
  2. class ExampleLibrary(ElementLibrary):
  3. def __init__(self) -> None:
  4. # Initialize the set of visual elements for this extension library
  5. self.elements = {
  6. # A static element that displays its properties in a fraction
  7. "fraction": Element(
  8. "numerator",
  9. {
  10. "numerator": ElementProperty(PropertyType.number),
  11. "denominator": ElementProperty(PropertyType.number),
  12. },
  13. render_xhtml=ExampleLibrary._fraction_render,
  14. ),
  15. # A dynamic element that decorates its value
  16. "label": Element(
  17. "value",
  18. {"value": ElementProperty(PropertyType.dynamic_string)},
  19. # The name of the React component (ColoredLabel) that implements this custom
  20. # element, exported as ExampleLabel in front-end/src/index.ts
  21. react_component="ExampleLabel",
  22. ),
  23. }
  24. # The implementation of the rendering for the "fraction" static element
  25. @staticmethod
  26. def _fraction_render(props: dict) -> str:
  27. # Get the property values
  28. numerator = props.get("numerator")
  29. denominator = props.get("denominator")
  30. # No denominator or numerator is 0: display the numerator
  31. if denominator is None or int(numerator) == 0:
  32. return f"<span>{numerator}</span>"
  33. # Denominator is zero: display infinity
  34. if int(denominator) == 0:
  35. return '<span style="font-size: 1.6em">&#8734;</span>'
  36. # 'Normal' case
  37. return f"<span><sup>{numerator}</sup>/<sub>{denominator}</sub></span>"
  38. def get_name(self) -> str:
  39. return "example"
  40. def get_elements(self) -> dict:
  41. return self.elements
  42. def get_scripts(self) -> list[str]:
  43. # Only one JavaScript bundle for this library.
  44. return ["front-end/dist/exampleLibrary.js"]