Show More
@@ -1,111 +1,93 | |||
|
1 | 1 | """StringWidget 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 | import inspect |
|
17 | 17 | import types |
|
18 | 18 | |
|
19 | 19 | from .widget import Widget |
|
20 | 20 | from IPython.utils.traitlets import Unicode, Bool, List, Int |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | class StringWidget(Widget): |
|
26 | 26 | target_name = Unicode('StringWidgetModel') |
|
27 | 27 | default_view_name = Unicode('TextBoxView') |
|
28 | 28 | |
|
29 | 29 | # Keys |
|
30 | 30 | _keys = ['value', 'disabled', 'description'] |
|
31 | 31 | value = Unicode(help="String value") |
|
32 | 32 | disabled = Bool(False, help="Enable or disable user changes") |
|
33 | 33 | description = Unicode(help="Description of the value this widget represents") |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def __init__(self, **kwargs): |
|
37 | 37 | super(StringWidget, self).__init__(**kwargs) |
|
38 | 38 | self._submission_callbacks = [] |
|
39 | 39 | self.on_msg(self._handle_string_msg) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def scroll_to_bottom(self): |
|
43 | 43 | self._comm.send({"method": "scroll_to_bottom"}) |
|
44 | 44 | |
|
45 | 45 | |
|
46 | def on_click(self, callback, remove=False): | |
|
47 | """Register a callback to execute when the button is clicked. The | |
|
48 | callback can either accept no parameters or one sender parameter: | |
|
49 | - callback() | |
|
50 | - callback(sender) | |
|
51 | If the callback has a sender parameter, the ButtonWidget instance that | |
|
52 | called the callback will be passed into the method as the sender. | |
|
53 | ||
|
54 | Parameters | |
|
55 | ---------- | |
|
56 | remove : bool (optional) | |
|
57 | Set to true to remove the callback from the list of callbacks.""" | |
|
58 | if remove: | |
|
59 | self._click_handlers.remove(callback) | |
|
60 | elif not callback in self._click_handlers: | |
|
61 | self._click_handlers.append(callback) | |
|
62 | ||
|
63 | ||
|
64 | 46 | def _handle_string_msg(self, content): |
|
65 | 47 | """Handle a msg from the front-end |
|
66 | 48 | |
|
67 | 49 | Parameters |
|
68 | 50 | ---------- |
|
69 | 51 | content: dict |
|
70 | 52 | Content of the msg.""" |
|
71 | 53 | if 'event' in content and content['event'] == 'submit': |
|
72 | 54 | self._handle_submit() |
|
73 | 55 | |
|
74 | 56 | |
|
75 | 57 | def on_submit(self, callback, remove=False): |
|
76 | 58 | """Register a callback to handle text submission (triggered when the |
|
77 | 59 | user clicks enter). |
|
78 | 60 | |
|
79 | 61 | Parameters |
|
80 | 62 | callback: Method handle |
|
81 | 63 | Function to be called when the text has been submitted. Function |
|
82 | 64 | can have two possible signatures: |
|
83 | 65 | callback() |
|
84 | 66 | callback(sender) |
|
85 | 67 | remove: bool (optional) |
|
86 | 68 | Whether or not to unregister the callback""" |
|
87 | 69 | if remove and callback in self._submission_callbacks: |
|
88 | 70 | self._submission_callbacks.remove(callback) |
|
89 | 71 | elif not remove and not callback in self._submission_callbacks: |
|
90 | 72 | self._submission_callbacks.append(callback) |
|
91 | 73 | |
|
92 | 74 | |
|
93 | 75 | def _handle_submit(self): |
|
94 | 76 | """Handles when a string widget view is submitted.""" |
|
95 | 77 | for handler in self._submission_callbacks: |
|
96 | 78 | if callable(handler): |
|
97 | 79 | argspec = inspect.getargspec(handler) |
|
98 | 80 | nargs = len(argspec[0]) |
|
99 | 81 | |
|
100 | 82 | # Bound methods have an additional 'self' argument |
|
101 | 83 | if isinstance(handler, types.MethodType): |
|
102 | 84 | nargs -= 1 |
|
103 | 85 | |
|
104 | 86 | # Call the callback |
|
105 | 87 | if nargs == 0: |
|
106 | 88 | handler() |
|
107 | 89 | elif nargs == 1: |
|
108 | 90 | handler(self) |
|
109 | 91 | else: |
|
110 | 92 | raise TypeError('StringWidget submit callback must ' \ |
|
111 | 93 | 'accept 0 or 1 arguments.') |
General Comments 0
You need to be logged in to leave comments.
Login now