widget_button.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Button class.
  4. Represents a button in the frontend using a widget. Allows user to listen for
  5. click events on the button and trigger backend code when the clicks are fired.
  6. """
  7. from .domwidget import DOMWidget
  8. from .widget import CallbackDispatcher, register, widget_serialization
  9. from .widget_core import CoreWidget
  10. from .widget_style import Style
  11. from .trait_types import Color, InstanceDict
  12. from traitlets import Unicode, Bool, CaselessStrEnum, Instance, validate, default
  13. import warnings
  14. @register
  15. class ButtonStyle(Style, CoreWidget):
  16. """Button style widget."""
  17. _model_name = Unicode('ButtonStyleModel').tag(sync=True)
  18. button_color = Color(None, allow_none=True, help="Color of the button").tag(sync=True)
  19. font_weight = Unicode(help="Button text font weight.").tag(sync=True)
  20. @register
  21. class Button(DOMWidget, CoreWidget):
  22. """Button widget.
  23. This widget has an `on_click` method that allows you to listen for the
  24. user clicking on the button. The click event itself is stateless.
  25. Parameters
  26. ----------
  27. description: str
  28. description displayed next to the button
  29. tooltip: str
  30. tooltip caption of the toggle button
  31. icon: str
  32. font-awesome icon name
  33. disabled: bool
  34. whether user interaction is enabled
  35. """
  36. _view_name = Unicode('ButtonView').tag(sync=True)
  37. _model_name = Unicode('ButtonModel').tag(sync=True)
  38. description = Unicode(help="Button label.").tag(sync=True)
  39. tooltip = Unicode(help="Tooltip caption of the button.").tag(sync=True)
  40. disabled = Bool(False, help="Enable or disable user changes.").tag(sync=True)
  41. icon = Unicode('', help="Font-awesome icon name, without the 'fa-' prefix.").tag(sync=True)
  42. button_style = CaselessStrEnum(
  43. values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
  44. help="""Use a predefined styling for the button.""").tag(sync=True)
  45. style = InstanceDict(ButtonStyle).tag(sync=True, **widget_serialization)
  46. def __init__(self, **kwargs):
  47. super(Button, self).__init__(**kwargs)
  48. self._click_handlers = CallbackDispatcher()
  49. self.on_msg(self._handle_button_msg)
  50. @validate('icon')
  51. def _validate_icon(self, proposal):
  52. """Strip 'fa-' if necessary'"""
  53. value = proposal['value']
  54. if value.startswith('fa-'):
  55. warnings.warn("icons names no longer start with 'fa-', "
  56. "just use the class name itself (for example, 'check' instead of 'fa-check')", DeprecationWarning)
  57. value = value[3:]
  58. return value
  59. def on_click(self, callback, remove=False):
  60. """Register a callback to execute when the button is clicked.
  61. The callback will be called with one argument, the clicked button
  62. widget instance.
  63. Parameters
  64. ----------
  65. remove: bool (optional)
  66. Set to true to remove the callback from the list of callbacks.
  67. """
  68. self._click_handlers.register_callback(callback, remove=remove)
  69. def click(self):
  70. """Programmatically trigger a click event.
  71. This will call the callbacks registered to the clicked button
  72. widget instance.
  73. """
  74. self._click_handlers(self)
  75. def _handle_button_msg(self, _, content, buffers):
  76. """Handle a msg from the front-end.
  77. Parameters
  78. ----------
  79. content: dict
  80. Content of the msg.
  81. """
  82. if content.get('event', '') == 'click':
  83. self.click()