number.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import justpy as jp
  2. from typing import Callable
  3. from .float_element import FloatElement
  4. class Number(FloatElement):
  5. def __init__(self,
  6. label: str = None,
  7. *,
  8. placeholder: str = None,
  9. value: float = None,
  10. format: str = None,
  11. on_change: Callable = None,
  12. ):
  13. """Number Input Element
  14. :param label: displayed name for the number input
  15. :param placeholder: text to show if no value is entered
  16. :param value: the inital value of the field
  17. :param format: a string like '%.2f' to format the displayed value
  18. :param on_change: callback to execute when the input is confirmed by leaving the focus
  19. """
  20. view = jp.QInput(
  21. type='number',
  22. label=label,
  23. placeholder=placeholder,
  24. change=self.handle_change,
  25. )
  26. super().__init__(view, value=value, format=format, on_change=on_change)
  27. def handle_change(self, msg):
  28. msg['value'] = float(msg['value'])
  29. return super().handle_change(msg)