Last active
May 8, 2025 01:43
-
-
Save giswqs/39a608e9e88c0aa07ccaa74641bf9618 to your computer and use it in GitHub Desktop.
Upload widget
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "f621e964", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "f0a2e1d4869f41d1babfde5290b70e32", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "HBox(children=(FileUpload(value=(), accept='.geojson', description='Upload', layout=Layout(margin='12px'), mul…" | |
| ] | |
| }, | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "import os\n", | |
| "import tempfile\n", | |
| "import ipywidgets as widgets\n", | |
| "\n", | |
| "uploader = widgets.FileUpload(\n", | |
| " accept=\".geojson\", \n", | |
| " multiple=True, \n", | |
| " description=\"Upload\",\n", | |
| " layout=widgets.Layout(margin='12px'),\n", | |
| ")\n", | |
| "output = widgets.Output()\n", | |
| "\n", | |
| "upload_widget = widgets.HBox([uploader, output])\n", | |
| "\n", | |
| "def on_upload(change):\n", | |
| " with output:\n", | |
| " output.clear_output()\n", | |
| " print(\"Uploading ...\")\n", | |
| " temp_dir = tempfile.mkdtemp()\n", | |
| " for value in uploader.value:\n", | |
| " filename = value[\"name\"]\n", | |
| " content = value[\"content\"]\n", | |
| " file_path = os.path.join(temp_dir, filename)\n", | |
| " with open(file_path, 'wb') as f:\n", | |
| " f.write(content)\n", | |
| " output.clear_output()\n", | |
| "\n", | |
| "uploader.observe(on_upload, names=\"value\")\n", | |
| "upload_widget" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "geo", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.12.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment