Skip to content

Commit 0d63f77

Browse files
committed
Remove button_style pass-through property, clean up upload widget
1 parent 8ec07cd commit 0d63f77

File tree

5 files changed

+120
-162
lines changed

5 files changed

+120
-162
lines changed

docs/source/examples/Widget List.ipynb

+7-16
Original file line numberDiff line numberDiff line change
@@ -1431,19 +1431,10 @@
14311431
],
14321432
"source": [
14331433
"widgets.FileUpload(\n",
1434-
" # https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept\n",
1435-
" # eg. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
1436-
" accept='', # default\n",
1437-
" # True to accept multiple files upload else False\n",
1438-
" multiple=False, # default\n",
1439-
" # True to disable the button else False to enable it\n",
1440-
" disabled=False, # default\n",
1441-
" # CSS transparently passed to button (a button element overlays the input[type=file] element for better styling)\n",
1442-
" # e.g. 'color: darkblue; background-color: lightsalmon; width: 180px;'\n",
1443-
" style_button='', # default\n",
1444-
" # to compress data from browser to kernel\n",
1445-
" # compress level from 1 to 9 incl. - 0 for no compression\n",
1446-
" compress_level=0 # default\n",
1434+
" accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
1435+
" multiple=False, # True to accept multiple files upload else False\n",
1436+
" disabled=False,\n",
1437+
" compress_level=0 # Compression level from 1 to 9 incl. - 0 for no compression\n",
14471438
")"
14481439
]
14491440
},
@@ -1749,9 +1740,9 @@
17491740
],
17501741
"metadata": {
17511742
"kernelspec": {
1752-
"display_name": "ipywidgets",
1743+
"display_name": "Python 3",
17531744
"language": "python",
1754-
"name": "ipywidgets"
1745+
"name": "python3"
17551746
},
17561747
"language_info": {
17571748
"codemirror_mode": {
@@ -1763,7 +1754,7 @@
17631754
"name": "python",
17641755
"nbconvert_exporter": "python",
17651756
"pygments_lexer": "ipython3",
1766-
"version": "3.7.0"
1757+
"version": "3.7.3"
17671758
},
17681759
"widgets": {
17691760
"application/vnd.jupyter.widget-state+json": {

ipywidgets/widgets/tests/test_widget_upload.py

-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ def test_construction(self):
1616
assert uploader.accept == ''
1717
assert not uploader.multiple
1818
assert not uploader.disabled
19-
assert uploader.style_button == ''
2019
assert uploader.compress_level == 0
2120

2221
def test_construction_with_params(self):
2322
uploader = FileUpload(accept='.txt',
2423
multiple=True,
2524
disabled=True,
26-
style_button='color: red',
2725
compress_level=9)
2826
assert uploader.accept == '.txt'
2927
assert uploader.multiple
3028
assert uploader.disabled
31-
assert uploader.style_button == 'color: red'
3229
assert uploader.compress_level == 9

ipywidgets/widgets/widget_upload.py

+27-65
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
"""
88

99
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
1111

1212
from .widget_description import DescriptionWidget
1313
from .valuewidget import ValueWidget
1414
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
1718

1819

1920
def content_from_json(value, widget):
@@ -36,81 +37,42 @@ class FileUpload(DescriptionWidget, ValueWidget, CoreWidget):
3637
"""
3738
_model_name = Unicode('FileUploadModel').tag(sync=True)
3839
_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(False, help='Enable or disable button').tag(sync=True)
45+
icon = Unicode(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)
9456

9557
@validate('compress_level')
9658
def _valid_compress_level(self, proposal):
97-
if proposal['value'] not in range(10):
59+
if proposal.value not in range(10):
9860
raise TraitError('compress_level must be an int from 0 to 9 incl.')
99-
return proposal['value']
61+
return proposal.value
10062

10163
@observe('_counter')
10264
def on_incr_counter(self, change):
10365
"""
10466
counter increment triggers the update of trait value
10567
"""
10668
res = {}
107-
10869
msg = 'Error: length of li_metadata and li_content must be equal'
10970
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):
11372
name = metadata['name']
11473
res[name] = {'metadata': metadata, 'content': content}
74+
self.set_trait('value', res)
11575

116-
self.value = res
76+
@default('description')
77+
def _default_description(self):
78+
return 'Upload'

0 commit comments

Comments
 (0)