1
+
1
2
# Copyright (c) Jupyter Development Team.
2
3
# Distributed under the terms of the Modified BSD License.
3
4
7
8
group other widgets together and control their
8
9
relative layouts.
9
10
"""
11
+ from __future__ import annotations
12
+
13
+ import typing
14
+
15
+ from traitlets import CaselessStrEnum , TraitError , TraitType , Unicode
10
16
11
- from .widget import register , widget_serialization , Widget
17
+ from .docutils import doc_subst
12
18
from .domwidget import DOMWidget
19
+ from .widget import Widget , register , widget_serialization
13
20
from .widget_core import CoreWidget
14
- from .docutils import doc_subst
15
- from .trait_types import TypedTuple
16
- from traitlets import Unicode , CaselessStrEnum , Instance
17
-
18
21
19
22
_doc_snippets = {}
20
23
_doc_snippets ['box_params' ] = """
25
28
one of 'success', 'info', 'warning' or 'danger', or ''.
26
29
Applies a predefined style to the box. Defaults to '',
27
30
which applies no pre-defined style.
31
+
32
+ validate_mode: str
33
+ one of 'raise', 'warning', error'.
34
+ How invalid children will be treated.
35
+ 'raise' will raise a trait error.
36
+ 'warning' and 'error' will log an error using box.log dropping
37
+ the invalid items from children.
28
38
"""
29
39
40
+ class Children (TraitType ['tuple[Widget,...]' , typing .Iterable [Widget ]]):
41
+ default_value = ()
42
+
43
+ def validate (self , obj : Box , value : typing .Iterable [Widget ]):
44
+ valid , invalid = [], []
45
+ for v in value :
46
+ if isinstance (v , Widget ) and v ._repr_mimebundle_ :
47
+ valid .append (v )
48
+ else :
49
+ invalid .append (v )
50
+ if invalid :
51
+ msg = f'Invalid or closed items found: { invalid } '
52
+ if obj .validate_mode == 'log_warning' :
53
+ obj .log .warning (msg )
54
+ elif obj .validate_mode == 'log_error' :
55
+ obj .log .error (msg )
56
+ else :
57
+ raise TraitError (msg )
58
+ return tuple (valid )
59
+
30
60
31
61
@register
32
62
@doc_subst (_doc_snippets )
@@ -48,19 +78,23 @@ class Box(DOMWidget, CoreWidget):
48
78
"""
49
79
_model_name = Unicode ('BoxModel' ).tag (sync = True )
50
80
_view_name = Unicode ('BoxView' ).tag (sync = True )
81
+ tooltip = Unicode ('' , allow_none = True , help = 'A tooltip caption.' ).tag (sync = True )
82
+ validate_mode = CaselessStrEnum (['raise' , 'log_warning' , 'log_error' ], 'raise' )
51
83
52
84
# Child widgets in the container.
53
85
# Using a tuple here to force reassignment to update the list.
54
86
# When a proper notifying-list trait exists, use that instead.
55
- children = TypedTuple (trait = Instance (Widget ), help = "List of widget children" ).tag (
56
- sync = True , ** widget_serialization )
87
+ children = Children (help = 'List of widget children' ).tag (
88
+ sync = True , ** widget_serialization
89
+ )
57
90
58
91
box_style = CaselessStrEnum (
59
92
values = ['success' , 'info' , 'warning' , 'danger' , '' ], default_value = '' ,
60
93
help = """Use a predefined styling for the box.""" ).tag (sync = True )
61
-
94
+
62
95
def __init__ (self , children = (), ** kwargs ):
63
- kwargs ['children' ] = children
96
+ if children :
97
+ kwargs ['children' ] = children
64
98
super ().__init__ (** kwargs )
65
99
66
100
@register
0 commit comments