Skip to content

Commit

Permalink
Make FileUpload data and metadata private
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Jan 7, 2020
1 parent dab72aa commit 250fd81
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions ipywidgets/widgets/widget_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class FileUpload(DescriptionWidget, ValueWidget, CoreWidget):
_view_name = Unicode('FileUploadView').tag(sync=True)
_counter = Int(read_only=True).tag(sync=True)
_file_count = Int().tag(sync=True)
_metadata = List(Dict(), read_only=True, help='List of file metadata').tag(sync=True)
_data = List(Bytes(), read_only=True, help='List of file content (bytes)').tag(
sync=True, from_json=content_from_json
)

accept = Unicode(help='File types to accept, empty string for all').tag(sync=True)
multiple = Bool(help='If True, allow for multiple files upload').tag(sync=True)
Expand All @@ -44,19 +48,15 @@ class FileUpload(DescriptionWidget, ValueWidget, CoreWidget):
values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
help="""Use a predefined styling for the button.""").tag(sync=True)
style = InstanceDict(ButtonStyle).tag(sync=True, **widget_serialization)
metadata = List(Dict(), read_only=True, help='List of file metadata').tag(sync=True)
data = List(Bytes(), read_only=True, help='List of file content (bytes)').tag(
sync=True, from_json=content_from_json
)
error = Unicode(help='Error message').tag(sync=True)
value = Dict(read_only=True)

@observe('_counter')
def on_incr_counter(self, change):
res = {}
msg = 'Error: length of metadata and data must be equal'
assert len(self.metadata) == len(self.data), msg
for metadata, content in zip(self.metadata, self.data):
assert len(self._metadata) == len(self._data), msg
for metadata, content in zip(self._metadata, self._data):
name = metadata['name']
res[name] = {'metadata': metadata, 'content': content}
self.set_trait('value', res)
Expand Down
10 changes: 5 additions & 5 deletions packages/controls/src/widget_upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ export class FileUploadModel extends CoreDOMWidgetModel {

_counter: 0,
_file_count: 0,
_data: [],
_metadata: [],
accept: '',
description: 'Upload',
tooltip: '',
disabled: false,
icon: 'upload',
button_style: '',
multiple: false,
metadata: [],
data: [],
error: '',
style: null
});
}

static serializers = {
...CoreDOMWidgetModel.serializers,
data: { serialize: (buffers: any) => { return [...buffers]; } },
_data: { serialize: (buffers: any) => { return [...buffers]; } },
};
}

Expand Down Expand Up @@ -107,8 +107,8 @@ export class FileUploadView extends DOMWidgetView {
this.model.set({
_counter: counter + contents.length,
_file_count: contents.length,
metadata,
data: li_buffer,
_metadata: metadata,
_data: li_buffer,
error: '',
});
this.touch();
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/jupyterwidgetmodels.latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ Attribute | Type | Default | Help
Attribute | Type | Default | Help
-----------------|------------------|------------------|----
`_counter` | number (integer) | `0` |
`_data` | array | `[]` | List of file content (bytes)
`_dom_classes` | array of string | `[]` | CSS classes applied to widget DOM element
`_file_count` | number (integer) | `0` |
`_metadata` | array | `[]` | List of file metadata
`_model_module` | string | `'@jupyter-widgets/controls'` |
`_model_module_version` | string | `'1.5.0'` |
`_model_name` | string | `'FileUploadModel'` |
Expand All @@ -384,14 +386,12 @@ Attribute | Type | Default | Help
`_view_name` | string | `'FileUploadView'` |
`accept` | string | `''` | File types to accept, empty string for all
`button_style` | string (one of `'primary'`, `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the button.
`data` | array | `[]` | List of file content (bytes)
`description` | string | `''` | Description of the control.
`description_tooltip` | `null` or string | `null` | Tooltip for the description (defaults to description).
`disabled` | boolean | `false` | Enable or disable button
`error` | string | `''` | Error message
`icon` | string | `'upload'` | Font-awesome icon name, without the 'fa-' prefix.
`layout` | reference to Layout widget | reference to new instance |
`metadata` | array | `[]` | List of file metadata
`multiple` | boolean | `false` | If True, allow for multiple files upload
`style` | reference to ButtonStyle widget | reference to new instance |

Expand Down

0 comments on commit 250fd81

Please sign in to comment.