7
7
"""
8
8
9
9
import zlib
10
- from traitlets import observe , validate , Unicode , Dict , List , Int , Bool , Bytes , TraitError
10
+ from traitlets import observe , validate , default , Unicode , Dict , List , Int , Bool , Bytes , TraitError , CaselessStrEnum
11
11
12
12
from .widget_description import DescriptionWidget
13
13
from .valuewidget import ValueWidget
14
14
from .widget_core import CoreWidget
15
- from .widget import register
16
- from .trait_types import bytes_serialization
15
+ from .widget_button import ButtonStyle
16
+ from .widget import register , widget_serialization
17
+ from .trait_types import bytes_serialization , InstanceDict
17
18
18
19
19
20
def content_from_json (value , widget ):
@@ -36,81 +37,42 @@ class FileUpload(DescriptionWidget, ValueWidget, CoreWidget):
36
37
"""
37
38
_model_name = Unicode ('FileUploadModel' ).tag (sync = True )
38
39
_view_name = Unicode ('FileUploadView' ).tag (sync = True )
39
-
40
- _counter = Int (0 ).tag (sync = True )
41
-
42
- help = 'Type of files the input accepts. None for all. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept'
43
- accept = Unicode (help = help ).tag (sync = True )
44
-
45
- help = 'If true, allow for multiple files upload, else only accept one'
46
- multiple = Bool (False , help = help ).tag (sync = True )
47
-
48
- help = 'Enable or disable button'
49
- disabled = Bool (False , help = help ).tag (sync = True )
50
-
51
- help = 'Optional style for button (label element)'
52
- style_button = Unicode ('' , help = help ).tag (sync = True )
53
-
54
- help = 'Compress level: from 1 to 9 - 0 for no compression'
55
- compress_level = Int (0 , help = help ).tag (sync = True )
56
-
57
- help = 'List of file metadata'
58
- li_metadata = List (Dict , help = help ).tag (sync = True )
59
-
60
- help = 'List of file content (bytes)'
61
- li_content = List (Bytes , help = help ).tag (sync = True , from_json = content_from_json )
62
-
63
- help = 'Error message'
64
- error = Unicode ('' , help = help ).tag (sync = True )
65
-
66
- value = Dict ({}).tag (sync = False )
67
-
68
- def __init__ (self ,
69
- accept = '' ,
70
- multiple = False ,
71
- disabled = False ,
72
- style_button = '' ,
73
- compress_level = 0 ,
74
- ):
75
- """
76
- Instantiate widget
77
- """
78
-
79
- if accept is None :
80
- accept = ''
81
-
82
- if style_button is None :
83
- style_button = ''
84
-
85
- self ._counter = 0
86
- self .accept = accept
87
- self .disabled = disabled
88
- self .multiple = multiple
89
- self .style_button = style_button
90
- self .compress_level = compress_level
91
- self .value = {}
92
-
93
- super (FileUpload , self ).__init__ ()
40
+ _counter = Int ().tag (sync = True )
41
+
42
+ accept = Unicode (help = 'File types to accept, empty string for all' ).tag (sync = True )
43
+ multiple = Bool (help = 'If True, allow for multiple files upload' ).tag (sync = True )
44
+ disabled = Bool (help = 'Enable or disable button' ).tag (sync = True )
45
+ icon = Unicode ('upload' , help = "Font-awesome icon name, without the 'fa-' prefix." ).tag (sync = True )
46
+ button_style = CaselessStrEnum (
47
+ values = ['primary' , 'success' , 'info' , 'warning' , 'danger' , '' ], default_value = '' ,
48
+ help = """Use a predefined styling for the button.""" ).tag (sync = True )
49
+ style = InstanceDict (ButtonStyle ).tag (sync = True , ** widget_serialization )
50
+ compress_level = Int (help = 'Compression level: from 0 to 9 - 0 for no compression' ).tag (sync = True )
51
+ li_metadata = List (Dict , help = 'List of file metadata' ).tag (sync = True )
52
+ li_content = List (Bytes , help = 'List of file content (bytes)' ).tag (sync = True , from_json = content_from_json )
53
+ error = Unicode (help = 'Error message' ).tag (sync = True )
54
+
55
+ value = Dict (read_only = True )
94
56
95
57
@validate ('compress_level' )
96
58
def _valid_compress_level (self , proposal ):
97
- if proposal [ ' value' ] not in range (10 ):
59
+ if proposal . value not in range (10 ):
98
60
raise TraitError ('compress_level must be an int from 0 to 9 incl.' )
99
- return proposal [ ' value' ]
61
+ return proposal . value
100
62
101
63
@observe ('_counter' )
102
64
def on_incr_counter (self , change ):
103
65
"""
104
66
counter increment triggers the update of trait value
105
67
"""
106
68
res = {}
107
-
108
69
msg = 'Error: length of li_metadata and li_content must be equal'
109
70
assert len (self .li_metadata ) == len (self .li_content ), msg
110
-
111
- for metadata , content in zip (self .li_metadata ,
112
- self .li_content ):
71
+ for metadata , content in zip (self .li_metadata , self .li_content ):
113
72
name = metadata ['name' ]
114
73
res [name ] = {'metadata' : metadata , 'content' : content }
74
+ self .set_trait ('value' , res )
115
75
116
- self .value = res
76
+ @default ('description' )
77
+ def _default_description (self ):
78
+ return 'Upload'
0 commit comments