5
5
import anywidget
6
6
import traitlets
7
7
import os
8
- from traitlets import Unicode , validate , TraitError
8
+ from traitlets import Unicode , validate , TraitError , Any , observe
9
9
from .frontend import module_name , module_version
10
10
11
11
from .utils import (
@@ -28,7 +28,10 @@ class WidgetCodeInput(anywidget.AnyWidget):
28
28
29
29
function_name = Unicode ('example' ).tag (sync = True )
30
30
function_parameters = Unicode ('' ).tag (sync = True )
31
- docstring = Unicode ('\n ' ).tag (sync = True )
31
+ docstring = Any (default_value = None , allow_none = True ).tag (sync = True )
32
+
33
+
34
+ # docstring = Unicode('\n').tag(sync=True)
32
35
function_body = Unicode ('' ).tag (sync = True )
33
36
code_theme = Unicode ('' ).tag (sync = True )
34
37
widget_instance_count_trait = Unicode (f'' ).tag (sync = True )
@@ -62,8 +65,6 @@ def _valid_docstring(self, docstring):
62
65
"""
63
66
Validate that the docstring do not contain triple double quotes
64
67
"""
65
- if '"""' in docstring ['value' ]:
66
- raise TraitError ('The docstring cannot contain triple double quotes (""")' )
67
68
return docstring ['value' ]
68
69
69
70
@@ -73,7 +74,7 @@ def __init__( # pylint: disable=too-many-arguments
73
74
self ,
74
75
function_name ,
75
76
function_parameters = "" ,
76
- docstring = " \n " ,
77
+ docstring = None ,
77
78
function_body = "" ,
78
79
code_theme = "basicLight" ,
79
80
):
@@ -94,7 +95,18 @@ def __init__( # pylint: disable=too-many-arguments
94
95
95
96
self .function_name = function_name
96
97
self .function_parameters = function_parameters
97
- self .docstring = docstring
98
+ if docstring is None :
99
+ # we cannot store docstring as None so we use
100
+ # a variable to signify that it was None
101
+ self .docstring = ""
102
+ self ._display_docstring = False
103
+ elif docstring .startswith ("\" \" \" " ) and docstring .endswith ("\" \" \" " ):
104
+ # assume the quotation marks have been added so we do not need to add them
105
+ self .docstring = docstring .strip ('\" \" \" ' )
106
+ self ._display_docstring = True
107
+ else :
108
+ self .docstring = docstring
109
+ self ._display_docstring = True
98
110
self .function_body = function_body
99
111
self .code_theme = code_theme
100
112
self .widget_instance_count_trait = f"{ WidgetCodeInput .widget_instance_count } "
@@ -104,7 +116,15 @@ def __init__( # pylint: disable=too-many-arguments
104
116
105
117
name = traitlets .Unicode ().tag (sync = True )
106
118
107
-
119
+
120
+ @observe ("docstring" )
121
+ def _on_docstring_changed (self , change ):
122
+ if change ["new" ] is None :
123
+ # Use set_trait to avoid infinite recursion
124
+ self .set_trait ("docstring" , "" )
125
+ self ._display_docstring = False
126
+ else :
127
+ self ._display_docstring = True
108
128
109
129
110
130
@property
@@ -114,7 +134,7 @@ def full_function_code(self):
114
134
including signature, docstring and body
115
135
"""
116
136
return build_function (
117
- self .function_signature , self .docstring , self .function_body
137
+ self .function_signature , self .docstring if self . _display_docstring else None , self .function_body
118
138
)
119
139
120
140
@property
@@ -174,4 +194,3 @@ def wrapper(*args, **kwargs):
174
194
175
195
return catch_exceptions (function_object )
176
196
177
-
0 commit comments