widget_bool.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Bool class.
  4. Represents a boolean using a widget.
  5. """
  6. from .widget_description import DescriptionWidget
  7. from .widget_core import CoreWidget
  8. from .valuewidget import ValueWidget
  9. from .widget import register
  10. from traitlets import Unicode, Bool, CaselessStrEnum
  11. class _Bool(DescriptionWidget, ValueWidget, CoreWidget):
  12. """A base class for creating widgets that represent booleans."""
  13. value = Bool(False, help="Bool value").tag(sync=True)
  14. disabled = Bool(False, help="Enable or disable user changes.").tag(sync=True)
  15. def __init__(self, value=None, **kwargs):
  16. if value is not None:
  17. kwargs['value'] = value
  18. super(_Bool, self).__init__(**kwargs)
  19. _model_name = Unicode('BoolModel').tag(sync=True)
  20. @register
  21. class Checkbox(_Bool):
  22. """Displays a boolean `value` in the form of a checkbox.
  23. Parameters
  24. ----------
  25. value : {True,False}
  26. value of the checkbox: True-checked, False-unchecked
  27. description : str
  28. description displayed next to the checkbox
  29. indent : {True,False}
  30. indent the control to align with other controls with a description. The style.description_width attribute controls this width for consistence with other controls.
  31. """
  32. _view_name = Unicode('CheckboxView').tag(sync=True)
  33. _model_name = Unicode('CheckboxModel').tag(sync=True)
  34. indent = Bool(True, help="Indent the control to align with other controls with a description.").tag(sync=True)
  35. @register
  36. class ToggleButton(_Bool):
  37. """Displays a boolean `value` in the form of a toggle button.
  38. Parameters
  39. ----------
  40. value : {True,False}
  41. value of the toggle button: True-pressed, False-unpressed
  42. description : str
  43. description displayed next to the button
  44. tooltip: str
  45. tooltip caption of the toggle button
  46. icon: str
  47. font-awesome icon name
  48. """
  49. _view_name = Unicode('ToggleButtonView').tag(sync=True)
  50. _model_name = Unicode('ToggleButtonModel').tag(sync=True)
  51. tooltip = Unicode(help="Tooltip caption of the toggle button.").tag(sync=True)
  52. icon = Unicode('', help= "Font-awesome icon.").tag(sync=True)
  53. button_style = CaselessStrEnum(
  54. values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
  55. help="""Use a predefined styling for the button.""").tag(sync=True)
  56. @register
  57. class Valid(_Bool):
  58. """Displays a boolean `value` in the form of a green check (True / valid)
  59. or a red cross (False / invalid).
  60. Parameters
  61. ----------
  62. value: {True,False}
  63. value of the Valid widget
  64. """
  65. readout = Unicode('Invalid', help="Message displayed when the value is False").tag(sync=True)
  66. _view_name = Unicode('ValidView').tag(sync=True)
  67. _model_name = Unicode('ValidModel').tag(sync=True)