notebook/IPython/html/widgets/widget_string.py

96 lines
3.3 KiB
Python
Raw Normal View History

"""String class.
2013-10-26 02:31:48 +08:00
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-09-18 05:04:03 +08:00
from .widget import DOMWidget, CallbackDispatcher, register
from IPython.utils.traitlets import Unicode, Bool
from IPython.utils.warn import DeprecatedClass
2013-10-17 14:16:12 +08:00
2013-10-26 02:31:48 +08:00
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class _String(DOMWidget):
"""Base class used to create widgets that represent a string."""
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)
placeholder = Unicode("", help="Placeholder text to display when nothing has been typed", sync=True)
2013-11-19 07:05:52 +08:00
def __init__(self, value=None, **kwargs):
if value is not None:
kwargs['value'] = value
super(_String, self).__init__(**kwargs)
2013-11-19 07:05:52 +08:00
2014-10-14 09:45:16 +08:00
@register('IPython.HTML')
class HTML(_String):
"""Renders the string `value` as HTML."""
_view_name = Unicode('HTMLView', sync=True)
2014-01-22 08:44:57 +08:00
2014-10-14 09:45:16 +08:00
@register('IPython.Latex')
class Latex(_String):
"""Renders math inside the string `value` as Latex (requires $ $ or $$ $$
and similar latex tags)."""
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
2014-10-14 09:45:16 +08:00
@register('IPython.Textarea')
class Textarea(_String):
"""Multiline text area widget."""
_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"})
2014-10-14 09:45:16 +08:00
@register('IPython.Text')
class Text(_String):
"""Single line textbox widget."""
_view_name = Unicode('TextView', sync=True)
2014-01-15 00:24:35 +08:00
def __init__(self, *args, **kwargs):
super(Text, self).__init__(*args, **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)
# Remove in IPython 4.0
HTMLWidget = DeprecatedClass(HTML, 'HTMLWidget')
LatexWidget = DeprecatedClass(Latex, 'LatexWidget')
TextareaWidget = DeprecatedClass(Textarea, 'TextareaWidget')
TextWidget = DeprecatedClass(Text, 'TextWidget')