|
958 | 958 | "source": [
|
959 | 959 | "## File Upload\n",
|
960 | 960 | "\n",
|
961 |
| - "The `FileUpload` allows to upload any type of file(s) as bytes." |
| 961 | + "The `FileUpload` allows to upload any type of file(s) into memory in the kernel." |
962 | 962 | ]
|
963 | 963 | },
|
964 | 964 | {
|
|
973 | 973 | ")"
|
974 | 974 | ]
|
975 | 975 | },
|
| 976 | + { |
| 977 | + "cell_type": "markdown", |
| 978 | + "metadata": {}, |
| 979 | + "source": [ |
| 980 | + "The upload widget exposes a `value` attribute that contains the files uploaded. The value attribute is a list with a dictionary for each upload. For instance:\n", |
| 981 | + "\n", |
| 982 | + "```python\n", |
| 983 | + "uploader = widgets.FileUpload()\n", |
| 984 | + "display(uploader)\n", |
| 985 | + "\n", |
| 986 | + "# upload something...\n", |
| 987 | + "\n", |
| 988 | + "# once a file is uploaded, use the `.value` attribute to retrieve the content:\n", |
| 989 | + "uploader.value\n", |
| 990 | + "#=> [\n", |
| 991 | + "#=> {\n", |
| 992 | + "#=> 'metadata': {'name': 'example.txt', 'type': 'text/plain', 'size': 36, 'lastModified': 1578647380733}, \n", |
| 993 | + "#=> 'content': <memory at 0x10c1b37c8>\n", |
| 994 | + "#=> }\n", |
| 995 | + "#=> ]\n", |
| 996 | + "```\n", |
| 997 | + "\n", |
| 998 | + "The contents of the file uploaded are in the value of the `content` key. They are a memory view:\n", |
| 999 | + "\n", |
| 1000 | + "```python\n", |
| 1001 | + "[uploaded_file] = uploader.value\n", |
| 1002 | + "uploaded_file[\"content\"]\n", |
| 1003 | + "#=> <memory at 0x10c1b37c8>\n", |
| 1004 | + "```\n", |
| 1005 | + "\n", |
| 1006 | + "You can extract the content to bytes:\n", |
| 1007 | + "\n", |
| 1008 | + "```python\n", |
| 1009 | + "uploaded_file[\"content\"].tobytes()\n", |
| 1010 | + "#=> b'This is the content of example.txt.\\n'\n", |
| 1011 | + "```\n", |
| 1012 | + "\n", |
| 1013 | + "To get the contents as a string:\n", |
| 1014 | + "\n", |
| 1015 | + "```python\n", |
| 1016 | + "uploaded_file[\"content\"].tobytes().decode(\"utf-8\")\n", |
| 1017 | + "#=> 'This is the content of example.txt.\\n'\n", |
| 1018 | + "```\n", |
| 1019 | + "\n", |
| 1020 | + "You can save the uploaded file to the filesystem from the kernel:\n", |
| 1021 | + "\n", |
| 1022 | + "```python\n", |
| 1023 | + "with open(\"./saved-output.txt\", \"wb\") as fp:\n", |
| 1024 | + " fp.write(uploaded_file[\"content\"])\n", |
| 1025 | + "```\n", |
| 1026 | + "\n", |
| 1027 | + "To convert the uploaded file into a Pandas dataframe, you need to convert it to a StringIO object:\n", |
| 1028 | + "\n", |
| 1029 | + "```python\n", |
| 1030 | + "pd.read_csv(io.StringIO(uploaded_file[\"content\"].tobytes().decode(\"utf-8\")))\n", |
| 1031 | + "```\n", |
| 1032 | + "\n", |
| 1033 | + "<div class=\"alert alert-info\">\n", |
| 1034 | + "Changes in *ipywidgets 8*:\n", |
| 1035 | + " \n", |
| 1036 | + "The `FileUpload` changed significantly in ipywidgets 8:\n", |
| 1037 | + " \n", |
| 1038 | + "- The `.value` traitlet is now a list of dictionaries with a `metadata` and `content` entry, rather than a dictionary mapping the uploaded name to the content. To retrieve the original form, use `{f[\"metadata\"][\"name\"]: f[\"content\"].tobytes() for f in uploader.value}`.\n", |
| 1039 | + "- The `.data` traitlet has been removed. To retrieve it, use `[f[\"content\"].tobytes() for f in uploader.value]`.\n", |
| 1040 | + "- The `.metadata` traitlet has been removed. To retrieve it, use `[f[\"metadata\"]. for f in uploader.value]`.\n", |
| 1041 | + "</div>" |
| 1042 | + ] |
| 1043 | + }, |
976 | 1044 | {
|
977 | 1045 | "cell_type": "markdown",
|
978 | 1046 | "metadata": {},
|
|
0 commit comments