notebook/IPython/html/widgets/widget_string.py

73 lines
2.4 KiB
Python
Raw Normal View History

2013-10-26 02:31:48 +08:00
"""StringWidget class.
Represents a unicode string using a widget.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
2014-01-22 07:14:27 +08:00
from .widget import DOMWidget, CallbackDispatcher
2014-01-16 19:42:38 +08:00
from IPython.utils.traitlets import Unicode, Bool, List
2013-10-17 14:16:12 +08:00
2013-10-26 02:31:48 +08:00
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
2014-01-22 08:44:57 +08:00
class _StringWidget(DOMWidget):
2014-01-14 23:15:19 +08:00
value = Unicode(help="String value", sync=True)
disabled = Bool(False, help="Enable or disable user changes", sync=True)
description = Unicode(help="Description of the value this widget represents", sync=True)
2013-11-19 07:05:52 +08:00
2014-01-22 08:44:57 +08:00
class HTMLWidget(_StringWidget):
_view_name = Unicode('HTMLView', sync=True)
2014-01-22 08:44:57 +08:00
class LatexWidget(_StringWidget):
2014-01-23 08:21:00 +08:00
_view_name = Unicode('LatexView', sync=True)
2014-01-15 00:24:35 +08:00
2013-11-19 07:05:52 +08:00
class TextareaWidget(_StringWidget):
_view_name = Unicode('TextareaView', sync=True)
2013-11-19 07:05:52 +08:00
def scroll_to_bottom(self):
2013-11-23 03:49:40 +08:00
self.send({"method": "scroll_to_bottom"})
class TextWidget(_StringWidget):
_view_name = Unicode('TextView', sync=True)
2014-01-15 00:24:35 +08:00
def __init__(self, **kwargs):
super(TextWidget, self).__init__(**kwargs)
self._submission_callbacks = CallbackDispatcher()
2014-01-15 00:24:35 +08:00
self.on_msg(self._handle_string_msg)
def _handle_string_msg(self, _, content):
2014-01-16 22:20:04 +08:00
"""Handle a msg from the front-end.
Parameters
----------
content: dict
Content of the msg."""
if content.get('event', '') == 'submit':
2014-01-22 07:14:27 +08:00
self._submission_callbacks(self)
2013-11-19 07:05:52 +08:00
def on_submit(self, callback, remove=False):
2014-01-16 22:20:04 +08:00
"""(Un)Register a callback to handle text submission.
Triggered when the user clicks enter.
2013-11-19 07:05:52 +08:00
Parameters
----------
callback: callable
Will be called with exactly one argument: the Widget instance
2013-11-19 07:05:52 +08:00
remove: bool (optional)
Whether to unregister the callback"""
2014-01-22 07:14:27 +08:00
self._submission_callbacks.register_callback(callback, remove=remove)