Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending FileInput: Add accept param #602

Merged
merged 3 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion examples/reference/widgets/FileInput.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"\n",
"##### Core\n",
"\n",
"* **``accept``** (str): A list of file input filters that restrict what files the user can pick from.\n",
"* **``filename``** (str): The filename of the uploaded file\n",
"* **``mime_type``** (str): The mime type of the uploaded file\n",
"* **``value``** (bytes): A bytes object containing the file data\n",
Expand Down Expand Up @@ -78,12 +79,50 @@
"if file_input.value is not None:\n",
" file_input.save('test.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `accept` parameter restricts what files the user can pick from. This consists of comma-separated list of standard HTML\n",
"file input filters. Values can be:\n",
"\n",
"* `<file extension>` - Specific file extension(s) (e.g: .gif, .jpg, .png, .doc) are pickable\n",
"* `audio/*` - all sound files are pickable\n",
"* `video/*` - all video files are pickable\n",
"* `image/*` - all image files are pickable\n",
"* `<media type>` - A valid [IANA Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml), with no parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_input = pn.widgets.FileInput(accept='.csv,.json')\n",
"\n",
"file_input"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"pygments_lexer": "ipython3"
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
3 changes: 2 additions & 1 deletion panel/tests/widgets/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_date_picker(document, comm):


def test_file_input(document, comm):
file_input = FileInput()
file_input = FileInput(accept='.txt')

widget = file_input.get_root(document, comm=comm)

Expand All @@ -62,6 +62,7 @@ def test_file_input(document, comm):
file_input._comm_change({'mime_type': 'text/plain', 'value': 'U29tZSB0ZXh0Cg=='})
assert file_input.value == b'Some text\n'
assert file_input.mime_type == 'text/plain'
assert file_input.accept == '.txt'


def test_literal_input(document, comm):
Expand Down
2 changes: 2 additions & 0 deletions panel/widgets/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class TextInput(Widget):

class FileInput(Widget):

accept = param.String(default=None)

filename = param.String(default=None)

mime_type = param.String(default=None)
Expand Down