widget_string.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """String class.
  4. Represents a unicode string using a widget.
  5. """
  6. from .widget_description import DescriptionWidget
  7. from .valuewidget import ValueWidget
  8. from .widget import CallbackDispatcher, register
  9. from .widget_core import CoreWidget
  10. from .trait_types import TypedTuple
  11. from traitlets import Unicode, Bool, Int
  12. from warnings import warn
  13. class _String(DescriptionWidget, ValueWidget, CoreWidget):
  14. """Base class used to create widgets that represent a string."""
  15. value = Unicode(help="String value").tag(sync=True)
  16. # We set a zero-width space as a default placeholder to make sure the baseline matches
  17. # the text, not the bottom margin. See the last paragraph of
  18. # https://www.w3.org/TR/CSS2/visudet.html#leading
  19. placeholder = Unicode(u'\u200b', help="Placeholder text to display when nothing has been typed").tag(sync=True)
  20. def __init__(self, value=None, **kwargs):
  21. if value is not None:
  22. kwargs['value'] = value
  23. super(_String, self).__init__(**kwargs)
  24. _model_name = Unicode('StringModel').tag(sync=True)
  25. @register
  26. class HTML(_String):
  27. """Renders the string `value` as HTML."""
  28. _view_name = Unicode('HTMLView').tag(sync=True)
  29. _model_name = Unicode('HTMLModel').tag(sync=True)
  30. @register
  31. class HTMLMath(_String):
  32. """Renders the string `value` as HTML, and render mathematics."""
  33. _view_name = Unicode('HTMLMathView').tag(sync=True)
  34. _model_name = Unicode('HTMLMathModel').tag(sync=True)
  35. @register
  36. class Label(_String):
  37. """Label widget.
  38. It also renders math inside the string `value` as Latex (requires $ $ or
  39. $$ $$ and similar latex tags).
  40. """
  41. _view_name = Unicode('LabelView').tag(sync=True)
  42. _model_name = Unicode('LabelModel').tag(sync=True)
  43. @register
  44. class Textarea(_String):
  45. """Multiline text area widget."""
  46. _view_name = Unicode('TextareaView').tag(sync=True)
  47. _model_name = Unicode('TextareaModel').tag(sync=True)
  48. rows = Int(None, allow_none=True, help="The number of rows to display.").tag(sync=True)
  49. disabled = Bool(False, help="Enable or disable user changes").tag(sync=True)
  50. continuous_update = Bool(True, help="Update the value as the user types. If False, update on submission, e.g., pressing Enter or navigating away.").tag(sync=True)
  51. @register
  52. class Text(_String):
  53. """Single line textbox widget."""
  54. _view_name = Unicode('TextView').tag(sync=True)
  55. _model_name = Unicode('TextModel').tag(sync=True)
  56. disabled = Bool(False, help="Enable or disable user changes").tag(sync=True)
  57. continuous_update = Bool(True, help="Update the value as the user types. If False, update on submission, e.g., pressing Enter or navigating away.").tag(sync=True)
  58. def __init__(self, *args, **kwargs):
  59. super(Text, self).__init__(*args, **kwargs)
  60. self._submission_callbacks = CallbackDispatcher()
  61. self.on_msg(self._handle_string_msg)
  62. def _handle_string_msg(self, _, content, buffers):
  63. """Handle a msg from the front-end.
  64. Parameters
  65. ----------
  66. content: dict
  67. Content of the msg.
  68. """
  69. if content.get('event', '') == 'submit':
  70. self._submission_callbacks(self)
  71. def on_submit(self, callback, remove=False):
  72. """(Un)Register a callback to handle text submission.
  73. Triggered when the user clicks enter.
  74. Parameters
  75. ----------
  76. callback: callable
  77. Will be called with exactly one argument: the Widget instance
  78. remove: bool (optional)
  79. Whether to unregister the callback
  80. """
  81. import warnings
  82. warnings.warn("on_submit is deprecated. Instead, set the .continuous_update attribute to False and observe the value changing with: mywidget.observe(callback, 'value').", DeprecationWarning)
  83. self._submission_callbacks.register_callback(callback, remove=remove)
  84. @register
  85. class Password(Text):
  86. """Single line textbox widget."""
  87. _view_name = Unicode('PasswordView').tag(sync=True)
  88. _model_name = Unicode('PasswordModel').tag(sync=True)
  89. disabled = Bool(False, help="Enable or disable user changes").tag(sync=True)
  90. def _repr_keys(self):
  91. # Don't include password value in repr!
  92. super_keys = super(Password, self)._repr_keys()
  93. for key in super_keys:
  94. if key != 'value':
  95. yield key
  96. @register
  97. class Combobox(Text):
  98. """Single line textbox widget with a dropdown and autocompletion.
  99. """
  100. _model_name = Unicode('ComboboxModel').tag(sync=True)
  101. _view_name = Unicode('ComboboxView').tag(sync=True)
  102. options = TypedTuple(
  103. trait=Unicode(),
  104. help="Dropdown options for the combobox"
  105. ).tag(sync=True)
  106. ensure_option = Bool(
  107. False,
  108. help='If set, ensure value is in options. Implies continuous_update=False.'
  109. ).tag(sync=True)