Skip to content

Commit

Permalink
Remove button_style pass-through property
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Jun 20, 2019
1 parent 8ec07cd commit e6aa936
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 91 deletions.
23 changes: 7 additions & 16 deletions docs/source/examples/Widget List.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1431,19 +1431,10 @@
],
"source": [
"widgets.FileUpload(\n",
" # https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept\n",
" # eg. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
" accept='', # default\n",
" # True to accept multiple files upload else False\n",
" multiple=False, # default\n",
" # True to disable the button else False to enable it\n",
" disabled=False, # default\n",
" # CSS transparently passed to button (a button element overlays the input[type=file] element for better styling)\n",
" # e.g. 'color: darkblue; background-color: lightsalmon; width: 180px;'\n",
" style_button='', # default\n",
" # to compress data from browser to kernel\n",
" # compress level from 1 to 9 incl. - 0 for no compression\n",
" compress_level=0 # default\n",
" accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
" multiple=False, # True to accept multiple files upload else False\n",
" disabled=False,\n",
" compress_level=0 # Compression level from 1 to 9 incl. - 0 for no compression\n",
")"
]
},
Expand Down Expand Up @@ -1749,9 +1740,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "ipywidgets",
"display_name": "Python 3",
"language": "python",
"name": "ipywidgets"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1763,7 +1754,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.7.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
3 changes: 0 additions & 3 deletions ipywidgets/widgets/tests/test_widget_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ def test_construction(self):
assert uploader.accept == ''
assert not uploader.multiple
assert not uploader.disabled
assert uploader.style_button == ''
assert uploader.compress_level == 0

def test_construction_with_params(self):
uploader = FileUpload(accept='.txt',
multiple=True,
disabled=True,
style_button='color: red',
compress_level=9)
assert uploader.accept == '.txt'
assert uploader.multiple
assert uploader.disabled
assert uploader.style_button == 'color: red'
assert uploader.compress_level == 9
70 changes: 11 additions & 59 deletions ipywidgets/widgets/widget_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,33 @@ class FileUpload(DescriptionWidget, ValueWidget, CoreWidget):
"""
_model_name = Unicode('FileUploadModel').tag(sync=True)
_view_name = Unicode('FileUploadView').tag(sync=True)

_counter = Int(0).tag(sync=True)

help = 'Type of files the input accepts. None for all. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept'
accept = Unicode(help=help).tag(sync=True)

help = 'If true, allow for multiple files upload, else only accept one'
multiple = Bool(False, help=help).tag(sync=True)

help = 'Enable or disable button'
disabled = Bool(False, help=help).tag(sync=True)

help = 'Optional style for button (label element)'
style_button = Unicode('', help=help).tag(sync=True)

help = 'Compress level: from 1 to 9 - 0 for no compression'
compress_level = Int(0, help=help).tag(sync=True)

help = 'List of file metadata'
li_metadata = List(Dict, help=help).tag(sync=True)

help = 'List of file content (bytes)'
li_content = List(Bytes, help=help).tag(sync=True, from_json=content_from_json)

help = 'Error message'
error = Unicode('', help=help).tag(sync=True)

value = Dict({}).tag(sync=False)
accept = Unicode(help='File types to accept, empty string for all').tag(sync=True)
multiple = Bool(False, help='If True, allow for multiple file upload').tag(sync=True)
disabled = Bool(False, help='Enable or disable button').tag(sync=True)
compress_level = Int(help='Compression level: from 0 to 9 - 0 for no compression').tag(sync=True)
li_metadata = List(Dict, help='List of file metadata').tag(sync=True)
li_content = List(Bytes, help='List of file content (bytes)').tag(sync=True, from_json=content_from_json)
error = Unicode(help='Error message').tag(sync=True)

def __init__(self,
accept='',
multiple=False,
disabled=False,
style_button='',
compress_level=0,
):
"""
Instantiate widget
"""

if accept is None:
accept = ''

if style_button is None:
style_button = ''

self._counter = 0
self.accept = accept
self.disabled = disabled
self.multiple = multiple
self.style_button = style_button
self.compress_level = compress_level
self.value = {}

super(FileUpload, self).__init__()
value = Dict()

@validate('compress_level')
def _valid_compress_level(self, proposal):
if proposal['value'] not in range(10):
if proposal.value not in range(10):
raise TraitError('compress_level must be an int from 0 to 9 incl.')
return proposal['value']
return proposal.value

@observe('_counter')
def on_incr_counter(self, change):
"""
counter increment triggers the update of trait value
"""
res = {}

msg = 'Error: length of li_metadata and li_content must be equal'
assert len(self.li_metadata) == len(self.li_content), msg

for metadata, content in zip(self.li_metadata,
self.li_content):
for metadata, content in zip(self.li_metadata, self.li_content):
name = metadata['name']
res[name] = {'metadata': metadata, 'content': content}

self.value = res
21 changes: 9 additions & 12 deletions packages/controls/src/widget_upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class FileUploadModel extends CoreDOMWidgetModel {
accept: '',
disabled: false,
multiple: false,
style: '',
compress_level: 0,
li_metadata: [],
li_content: [],
Expand All @@ -63,22 +62,22 @@ export class FileUploadView extends DOMWidgetView {
const that = this;
let counter;

const divLoader = document.createElement('div');
this.el.appendChild(divLoader);
this.el = document.createElement('div');
this.el.classList.add('jupyter-widgets');

const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.multiple = this.model.get('multiple');
fileInput.setAttribute('style', 'display: none');
divLoader.appendChild(fileInput);
this.el.appendChild(fileInput);
this.fileInput = fileInput;

const btn = document.createElement('button');
btn.innerHTML = build_btn_inner_html(null);
btn.className = 'p-Widget jupyter-widgets jupyter-button widget-button';
btn.classList.add('jupyter-button');
btn.classList.add('widget-button');
btn.disabled = this.model.get('disabled');
btn.setAttribute('style', this.model.get('style_button'));
divLoader.appendChild(btn);
this.el.appendChild(btn);
this.btn = btn;

btn.addEventListener('click', () => {
Expand Down Expand Up @@ -159,21 +158,19 @@ export class FileUploadView extends DOMWidgetView {
that.model.on('change:accept', that.update_accept, that);
that.model.on('change:disabled', that.toggle_disabled, that);
that.model.on('change:multiple', that.update_multiple, that);
that.model.on('change:style_button', that.update_style_button, that);
}

update_accept() {
this.fileInput.accept = this.model.get('accept');
}

toggle_disabled() {
this.btn.disabled = this.model.get('disabled');
}

update_multiple() {
this.fileInput.multiple = this.model.get('multiple');
}
update_style_button() {
this.btn.setAttribute('style', this.model.get('style_button'));
}

el: HTMLImageElement;
el: HTMLDivElement;
}
1 change: 0 additions & 1 deletion packages/schema/jupyterwidgetmodels.latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ Attribute | Type | Default | Help
`li_metadata` | array | `[]` | List of file metadata
`multiple` | boolean | `false` | If true, allow for multiple files upload, else only accept one
`style` | reference to DescriptionStyle widget | reference to new instance | Styling customizations
`style_button` | string | `''` | Optional style for button (label element)

### FloatLogSliderModel (@jupyter-widgets/controls, 1.4.0); FloatLogSliderView (@jupyter-widgets/controls, 1.4.0)

Expand Down

0 comments on commit e6aa936

Please sign in to comment.