Show More
@@ -1,95 +1,95 b'' | |||
|
1 | 1 | """String class. |
|
2 | 2 | |
|
3 | 3 | Represents a unicode string using a widget. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from .widget import DOMWidget, CallbackDispatcher, register |
|
17 | 17 | from IPython.utils.traitlets import Unicode, Bool |
|
18 | 18 | from IPython.utils.warn import DeprecatedClass |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | class _String(DOMWidget): |
|
24 | 24 | """Base class used to create widgets that represent a string.""" |
|
25 | 25 | value = Unicode(help="String value", sync=True) |
|
26 | 26 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
27 | 27 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
28 | 28 | placeholder = Unicode("", help="Placeholder text to display when nothing has been typed", sync=True) |
|
29 | 29 | |
|
30 | 30 | def __init__(self, value=None, **kwargs): |
|
31 | 31 | if value is not None: |
|
32 | 32 | kwargs['value'] = value |
|
33 | 33 | super(_String, self).__init__(**kwargs) |
|
34 | 34 | |
|
35 | 35 | @register('IPython.HTML') |
|
36 | 36 | class HTML(_String): |
|
37 | 37 | """Renders the string `value` as HTML.""" |
|
38 | 38 | _view_name = Unicode('HTMLView', sync=True) |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | @register('IPython.Latex') |
|
42 | 42 | class Latex(_String): |
|
43 | 43 | """Renders math inside the string `value` as Latex (requires $ $ or $$ $$ |
|
44 | 44 | and similar latex tags).""" |
|
45 | 45 | _view_name = Unicode('LatexView', sync=True) |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | @register('IPython.Textarea') |
|
49 | 49 | class Textarea(_String): |
|
50 | 50 | """Multiline text area widget.""" |
|
51 | 51 | _view_name = Unicode('TextareaView', sync=True) |
|
52 | 52 | |
|
53 | 53 | def scroll_to_bottom(self): |
|
54 | 54 | self.send({"method": "scroll_to_bottom"}) |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | @register('IPython.Text') |
|
58 | 58 | class Text(_String): |
|
59 | 59 | """Single line textbox widget.""" |
|
60 | 60 | _view_name = Unicode('TextView', sync=True) |
|
61 | 61 | |
|
62 |
def __init__(self, * |
|
|
63 |
super(Text, self).__init__(* |
|
|
62 | def __init__(self, *args, **kwargs): | |
|
63 | super(Text, self).__init__(*args, **kwargs) | |
|
64 | 64 | self._submission_callbacks = CallbackDispatcher() |
|
65 | 65 | self.on_msg(self._handle_string_msg) |
|
66 | 66 | |
|
67 | 67 | def _handle_string_msg(self, _, content): |
|
68 | 68 | """Handle a msg from the front-end. |
|
69 | 69 | |
|
70 | 70 | Parameters |
|
71 | 71 | ---------- |
|
72 | 72 | content: dict |
|
73 | 73 | Content of the msg.""" |
|
74 | 74 | if content.get('event', '') == 'submit': |
|
75 | 75 | self._submission_callbacks(self) |
|
76 | 76 | |
|
77 | 77 | def on_submit(self, callback, remove=False): |
|
78 | 78 | """(Un)Register a callback to handle text submission. |
|
79 | 79 | |
|
80 | 80 | Triggered when the user clicks enter. |
|
81 | 81 | |
|
82 | 82 | Parameters |
|
83 | 83 | ---------- |
|
84 | 84 | callback: callable |
|
85 | 85 | Will be called with exactly one argument: the Widget instance |
|
86 | 86 | remove: bool (optional) |
|
87 | 87 | Whether to unregister the callback""" |
|
88 | 88 | self._submission_callbacks.register_callback(callback, remove=remove) |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | # Remove in IPython 4.0 |
|
92 | 92 | HTMLWidget = DeprecatedClass(HTML, 'HTMLWidget') |
|
93 | 93 | LatexWidget = DeprecatedClass(Latex, 'LatexWidget') |
|
94 | 94 | TextareaWidget = DeprecatedClass(Textarea, 'TextareaWidget') |
|
95 | 95 | TextWidget = DeprecatedClass(Text, 'TextWidget') |
General Comments 0
You need to be logged in to leave comments.
Login now