2014-07-19 03:47:33 +08:00
|
|
|
"""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
|
2014-02-26 01:46:27 +08:00
|
|
|
from IPython.utils.traitlets import Unicode, Bool
|
2014-07-19 03:47:33 +08:00
|
|
|
from IPython.utils.warn import DeprecatedClass
|
2013-10-17 14:16:12 +08:00
|
|
|
|
2013-10-26 02:31:48 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Classes
|
|
|
|
#-----------------------------------------------------------------------------
|
2014-07-19 03:47:33 +08:00
|
|
|
class _String(DOMWidget):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""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)
|
2014-04-17 22:55:21 +08:00
|
|
|
placeholder = Unicode("", help="Placeholder text to display when nothing has been typed", sync=True)
|
2013-11-19 07:05:52 +08:00
|
|
|
|
2014-09-28 04:07:42 +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')
|
2014-07-19 03:47:33 +08:00
|
|
|
class HTML(_String):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Renders the string `value` as HTML."""
|
2014-01-27 07:46:06 +08:00
|
|
|
_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')
|
2014-07-19 03:47:33 +08:00
|
|
|
class Latex(_String):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""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')
|
2014-07-19 03:47:33 +08:00
|
|
|
class Textarea(_String):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Multiline text area widget."""
|
2014-01-29 04:51:24 +08:00
|
|
|
_view_name = Unicode('TextareaView', sync=True)
|
2013-11-19 07:05:52 +08:00
|
|
|
|
2013-11-20 00:19:28 +08:00
|
|
|
def scroll_to_bottom(self):
|
2013-11-23 03:49:40 +08:00
|
|
|
self.send({"method": "scroll_to_bottom"})
|
2013-11-20 00:19:28 +08:00
|
|
|
|
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.Text')
|
2014-07-19 03:47:33 +08:00
|
|
|
class Text(_String):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Single line textbox widget."""
|
2014-01-29 04:51:24 +08:00
|
|
|
_view_name = Unicode('TextView', sync=True)
|
2014-01-15 00:24:35 +08:00
|
|
|
|
2014-12-18 04:47:56 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Text, self).__init__(*args, **kwargs)
|
2014-01-27 07:46:06 +08:00
|
|
|
self._submission_callbacks = CallbackDispatcher()
|
2014-01-15 00:24:35 +08:00
|
|
|
self.on_msg(self._handle_string_msg)
|
|
|
|
|
2014-01-27 07:46:06 +08:00
|
|
|
def _handle_string_msg(self, _, content):
|
2014-01-16 22:20:04 +08:00
|
|
|
"""Handle a msg from the front-end.
|
2013-11-22 07:57:34 +08:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
content: dict
|
|
|
|
Content of the msg."""
|
2014-01-27 07:46:06 +08:00
|
|
|
if content.get('event', '') == 'submit':
|
2014-01-22 07:14:27 +08:00
|
|
|
self._submission_callbacks(self)
|
2013-11-22 07:57:34 +08:00
|
|
|
|
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
|
2014-01-27 07:46:06 +08:00
|
|
|
----------
|
|
|
|
callback: callable
|
|
|
|
Will be called with exactly one argument: the Widget instance
|
2013-11-19 07:05:52 +08:00
|
|
|
remove: bool (optional)
|
2014-01-27 07:46:06 +08:00
|
|
|
Whether to unregister the callback"""
|
2014-01-22 07:14:27 +08:00
|
|
|
self._submission_callbacks.register_callback(callback, remove=remove)
|
2014-07-19 03:47:33 +08:00
|
|
|
|
2014-07-22 02:37:42 +08:00
|
|
|
|
|
|
|
# Remove in IPython 4.0
|
2014-07-19 03:47:33 +08:00
|
|
|
HTMLWidget = DeprecatedClass(HTML, 'HTMLWidget')
|
|
|
|
LatexWidget = DeprecatedClass(Latex, 'LatexWidget')
|
|
|
|
TextareaWidget = DeprecatedClass(Textarea, 'TextareaWidget')
|
|
|
|
TextWidget = DeprecatedClass(Text, 'TextWidget')
|