55import anywidget
66import traitlets
77import os
8- from traitlets import Unicode , validate , TraitError
8+ from traitlets import Unicode , validate , TraitError , Any , observe
99from .frontend import module_name , module_version
1010
1111from .utils import (
@@ -28,7 +28,10 @@ class WidgetCodeInput(anywidget.AnyWidget):
2828
2929 function_name = Unicode ('example' ).tag (sync = True )
3030 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)
3235 function_body = Unicode ('' ).tag (sync = True )
3336 code_theme = Unicode ('' ).tag (sync = True )
3437 widget_instance_count_trait = Unicode (f'' ).tag (sync = True )
@@ -62,8 +65,6 @@ def _valid_docstring(self, docstring):
6265 """
6366 Validate that the docstring do not contain triple double quotes
6467 """
65- if '"""' in docstring ['value' ]:
66- raise TraitError ('The docstring cannot contain triple double quotes (""")' )
6768 return docstring ['value' ]
6869
6970
@@ -73,7 +74,7 @@ def __init__( # pylint: disable=too-many-arguments
7374 self ,
7475 function_name ,
7576 function_parameters = "" ,
76- docstring = " \n " ,
77+ docstring = None ,
7778 function_body = "" ,
7879 code_theme = "basicLight" ,
7980 ):
@@ -94,7 +95,18 @@ def __init__( # pylint: disable=too-many-arguments
9495
9596 self .function_name = function_name
9697 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
98110 self .function_body = function_body
99111 self .code_theme = code_theme
100112 self .widget_instance_count_trait = f"{ WidgetCodeInput .widget_instance_count } "
@@ -104,7 +116,15 @@ def __init__( # pylint: disable=too-many-arguments
104116
105117 name = traitlets .Unicode ().tag (sync = True )
106118
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
108128
109129
110130 @property
@@ -114,7 +134,7 @@ def full_function_code(self):
114134 including signature, docstring and body
115135 """
116136 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
118138 )
119139
120140 @property
@@ -174,4 +194,3 @@ def wrapper(*args, **kwargs):
174194
175195 return catch_exceptions (function_object )
176196
177-
0 commit comments