Last active
May 25, 2023 15:12
-
-
Save juntyr/b78392f990d423bb835e49dd4e23a260 to your computer and use it in GitHub Desktop.
Upload a local File to JupyterLite-Pyodide without copying it into memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "python", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8" | |
}, | |
"kernelspec": { | |
"name": "python", | |
"display_name": "Python (Pyodide)", | |
"language": "python" | |
} | |
}, | |
"nbformat_minor": 4, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": "import IPython\nfrom IPython.display import HTML\nsession = IPython.get_ipython().kernel._parent_header['header']['session']\nsession", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"execution_count": 1, | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "'1ab81c31-4548-42fe-aa10-53f6b8c87fd7'" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "from pathlib import Path\n\nimport js\nimport pyodide\nimport pyodide_js\n\ndef files_upload_callback(event):\n if not getattr(event, \"data\", None) or not getattr(event.data, \"files\", None) or not getattr(event.data, \"uuid\", None):\n return\n \n upload_path = Path(\"/uploads\") / event.data.uuid\n upload_path.mkdir(parents=True, exist_ok=False)\n \n pyodide_js.FS.mount(\n pyodide_js.FS.filesystems.WORKERFS,\n pyodide.ffi.to_js({ \"files\": event.data.files }, dict_converter=js.Object.fromEntries, create_pyproxies=False),\n str(upload_path),\n )\n \n print(\", \".join(str(path) for path in upload_path.iterdir()))\n\njupyterLiteFileUpload = js.Reflect.construct(\n js.BroadcastChannel,\n pyodide.ffi.to_js([f\"jupyter-lite-file-upload-{session}\"]),\n)\njupyterLiteFileUpload.onmessage = files_upload_callback", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "display(HTML(fr'''\n<input type=\"file\" onchange=\"new BroadcastChannel('jupyter-lite-file-upload-{session}').postMessage({{ files: this.files, uuid: crypto.randomUUID() }})\"></input>\n'''))", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.HTML object>", | |
"text/html": "\n<input type=\"file\" onchange=\"new BroadcastChannel('jupyter-lite-file-upload-1ab81c31-4548-42fe-aa10-53f6b8c87fd7').postMessage({ files: this.files, uuid: crypto.randomUUID() })\"></input>\n" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"name": "stdout", | |
"text": "/uploads/d8efac67-4ef0-48e9-8f53-03978220140e/large.file\n", | |
"output_type": "stream" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "", | |
"metadata": {}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment