stat.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """Statistics components."""
  2. from typing import Optional
  3. from reflex.components.chakra import ChakraComponent
  4. from reflex.components.component import Component
  5. from reflex.vars import Var
  6. class Stat(ChakraComponent):
  7. """The Stat component is used to display some statistics. It can take in a label, a number and a help text."""
  8. tag: str = "Stat"
  9. @classmethod
  10. def create(
  11. cls, *children, label=None, number=0, help_text=None, arrow_type=None, **props
  12. ) -> Component:
  13. """Create a stat component.
  14. Args:
  15. *children: The children of the component.
  16. label: A label for the stat component.
  17. number: The value of the stat component.
  18. help_text: A text added to the stat component.
  19. arrow_type: The type of the arrow ("increase", "decrease", None)
  20. **props: The properties of the component.
  21. Returns:
  22. The stat component.
  23. """
  24. if len(children) == 0:
  25. children = []
  26. if label:
  27. children.append(StatLabel.create(label))
  28. children.append(StatNumber.create(number))
  29. if help_text:
  30. if arrow_type:
  31. children.append(
  32. StatHelpText.create(
  33. help_text, StatArrow.create(type_=arrow_type)
  34. )
  35. )
  36. else:
  37. children.append(StatHelpText.create(help_text))
  38. return super().create(*children, **props)
  39. class StatLabel(ChakraComponent):
  40. """A stat label component."""
  41. tag: str = "StatLabel"
  42. class StatNumber(ChakraComponent):
  43. """The stat to display."""
  44. tag: str = "StatNumber"
  45. class StatHelpText(ChakraComponent):
  46. """A helper text to display under the stat."""
  47. tag: str = "StatHelpText"
  48. class StatArrow(ChakraComponent):
  49. """A stat arrow component indicating the direction of change."""
  50. tag: str = "StatArrow"
  51. # The type of arrow, either increase or decrease.
  52. type_: Optional[Var[str]] = None
  53. class StatGroup(ChakraComponent):
  54. """A stat group component to evenly space out the stats."""
  55. tag: str = "StatGroup"