1
0

stat.py 2.1 KB

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