Last active
December 9, 2022 00:13
-
-
Save andilabs/23bebf0f27fa5435324e09e4920b6d1d to your computer and use it in GitHub Desktop.
upload_file.ipynb
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/andilabs/23bebf0f27fa5435324e09e4920b6d1d/upload_file.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"id": "zzAQAcCX6aD8" | |
}, | |
"outputs": [], | |
"source": [ | |
"from google.colab import files\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 109 | |
}, | |
"id": "uxIDm7hr6awJ", | |
"outputId": "24884b8d-ff6d-456a-c1fe-51cb4d522018" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
], | |
"text/html": [ | |
"\n", | |
" <input type=\"file\" id=\"files-379a9f93-28bf-48b1-89b9-d892b1275a84\" name=\"files[]\" multiple disabled\n", | |
" style=\"border:none\" />\n", | |
" <output id=\"result-379a9f93-28bf-48b1-89b9-d892b1275a84\">\n", | |
" Upload widget is only available when the cell has been executed in the\n", | |
" current browser session. Please rerun this cell to enable.\n", | |
" </output>\n", | |
" <script>// Copyright 2017 Google LLC\n", | |
"//\n", | |
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n", | |
"// you may not use this file except in compliance with the License.\n", | |
"// You may obtain a copy of the License at\n", | |
"//\n", | |
"// http://www.apache.org/licenses/LICENSE-2.0\n", | |
"//\n", | |
"// Unless required by applicable law or agreed to in writing, software\n", | |
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n", | |
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | |
"// See the License for the specific language governing permissions and\n", | |
"// limitations under the License.\n", | |
"\n", | |
"/**\n", | |
" * @fileoverview Helpers for google.colab Python module.\n", | |
" */\n", | |
"(function(scope) {\n", | |
"function span(text, styleAttributes = {}) {\n", | |
" const element = document.createElement('span');\n", | |
" element.textContent = text;\n", | |
" for (const key of Object.keys(styleAttributes)) {\n", | |
" element.style[key] = styleAttributes[key];\n", | |
" }\n", | |
" return element;\n", | |
"}\n", | |
"\n", | |
"// Max number of bytes which will be uploaded at a time.\n", | |
"const MAX_PAYLOAD_SIZE = 100 * 1024;\n", | |
"\n", | |
"function _uploadFiles(inputId, outputId) {\n", | |
" const steps = uploadFilesStep(inputId, outputId);\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" // Cache steps on the outputElement to make it available for the next call\n", | |
" // to uploadFilesContinue from Python.\n", | |
" outputElement.steps = steps;\n", | |
"\n", | |
" return _uploadFilesContinue(outputId);\n", | |
"}\n", | |
"\n", | |
"// This is roughly an async generator (not supported in the browser yet),\n", | |
"// where there are multiple asynchronous steps and the Python side is going\n", | |
"// to poll for completion of each step.\n", | |
"// This uses a Promise to block the python side on completion of each step,\n", | |
"// then passes the result of the previous step as the input to the next step.\n", | |
"function _uploadFilesContinue(outputId) {\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" const steps = outputElement.steps;\n", | |
"\n", | |
" const next = steps.next(outputElement.lastPromiseValue);\n", | |
" return Promise.resolve(next.value.promise).then((value) => {\n", | |
" // Cache the last promise value to make it available to the next\n", | |
" // step of the generator.\n", | |
" outputElement.lastPromiseValue = value;\n", | |
" return next.value.response;\n", | |
" });\n", | |
"}\n", | |
"\n", | |
"/**\n", | |
" * Generator function which is called between each async step of the upload\n", | |
" * process.\n", | |
" * @param {string} inputId Element ID of the input file picker element.\n", | |
" * @param {string} outputId Element ID of the output display.\n", | |
" * @return {!Iterable<!Object>} Iterable of next steps.\n", | |
" */\n", | |
"function* uploadFilesStep(inputId, outputId) {\n", | |
" const inputElement = document.getElementById(inputId);\n", | |
" inputElement.disabled = false;\n", | |
"\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" outputElement.innerHTML = '';\n", | |
"\n", | |
" const pickedPromise = new Promise((resolve) => {\n", | |
" inputElement.addEventListener('change', (e) => {\n", | |
" resolve(e.target.files);\n", | |
" });\n", | |
" });\n", | |
"\n", | |
" const cancel = document.createElement('button');\n", | |
" inputElement.parentElement.appendChild(cancel);\n", | |
" cancel.textContent = 'Cancel upload';\n", | |
" const cancelPromise = new Promise((resolve) => {\n", | |
" cancel.onclick = () => {\n", | |
" resolve(null);\n", | |
" };\n", | |
" });\n", | |
"\n", | |
" // Wait for the user to pick the files.\n", | |
" const files = yield {\n", | |
" promise: Promise.race([pickedPromise, cancelPromise]),\n", | |
" response: {\n", | |
" action: 'starting',\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" cancel.remove();\n", | |
"\n", | |
" // Disable the input element since further picks are not allowed.\n", | |
" inputElement.disabled = true;\n", | |
"\n", | |
" if (!files) {\n", | |
" return {\n", | |
" response: {\n", | |
" action: 'complete',\n", | |
" }\n", | |
" };\n", | |
" }\n", | |
"\n", | |
" for (const file of files) {\n", | |
" const li = document.createElement('li');\n", | |
" li.append(span(file.name, {fontWeight: 'bold'}));\n", | |
" li.append(span(\n", | |
" `(${file.type || 'n/a'}) - ${file.size} bytes, ` +\n", | |
" `last modified: ${\n", | |
" file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() :\n", | |
" 'n/a'} - `));\n", | |
" const percent = span('0% done');\n", | |
" li.appendChild(percent);\n", | |
"\n", | |
" outputElement.appendChild(li);\n", | |
"\n", | |
" const fileDataPromise = new Promise((resolve) => {\n", | |
" const reader = new FileReader();\n", | |
" reader.onload = (e) => {\n", | |
" resolve(e.target.result);\n", | |
" };\n", | |
" reader.readAsArrayBuffer(file);\n", | |
" });\n", | |
" // Wait for the data to be ready.\n", | |
" let fileData = yield {\n", | |
" promise: fileDataPromise,\n", | |
" response: {\n", | |
" action: 'continue',\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" // Use a chunked sending to avoid message size limits. See b/62115660.\n", | |
" let position = 0;\n", | |
" do {\n", | |
" const length = Math.min(fileData.byteLength - position, MAX_PAYLOAD_SIZE);\n", | |
" const chunk = new Uint8Array(fileData, position, length);\n", | |
" position += length;\n", | |
"\n", | |
" const base64 = btoa(String.fromCharCode.apply(null, chunk));\n", | |
" yield {\n", | |
" response: {\n", | |
" action: 'append',\n", | |
" file: file.name,\n", | |
" data: base64,\n", | |
" },\n", | |
" };\n", | |
"\n", | |
" let percentDone = fileData.byteLength === 0 ?\n", | |
" 100 :\n", | |
" Math.round((position / fileData.byteLength) * 100);\n", | |
" percent.textContent = `${percentDone}% done`;\n", | |
"\n", | |
" } while (position < fileData.byteLength);\n", | |
" }\n", | |
"\n", | |
" // All done.\n", | |
" yield {\n", | |
" response: {\n", | |
" action: 'complete',\n", | |
" }\n", | |
" };\n", | |
"}\n", | |
"\n", | |
"scope.google = scope.google || {};\n", | |
"scope.google.colab = scope.google.colab || {};\n", | |
"scope.google.colab._files = {\n", | |
" _uploadFiles,\n", | |
" _uploadFilesContinue,\n", | |
"};\n", | |
"})(self);\n", | |
"</script> " | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Saving export.csv to export.csv\n", | |
"Saving projekty-karola-sliwki.jpeg to projekty-karola-sliwki.jpeg\n" | |
] | |
} | |
], | |
"source": [ | |
"uploaded_files = files.upload()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "9tAPWhok7Xdn", | |
"outputId": "789e99ed-4f29-4171-a87b-a9eb91814a4a" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"['export.csv', 'projekty-karola-sliwki.jpeg']" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
], | |
"source": [ | |
"list(uploaded_files.keys())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "4FeeGP-L6gqx" | |
}, | |
"outputs": [], | |
"source": [ | |
"uploaded_files['projekty-karola-sliwki.jpeg']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"id": "EQRVl1FN6tWa", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "a6baffe8-7fac-4b1f-b0a9-dd326b2924c7" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"b'0\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12\\n13\\n14\\n15\\n16\\n17\\n18\\n19\\n20\\n21\\n22\\n23\\n24\\n25\\n26\\n27\\n28\\n29\\n30\\n31\\n32\\n33\\n34\\n35\\n36\\n37\\n38\\n39\\n40\\n41\\n42\\n43\\n44\\n45\\n46\\n47\\n48\\n49\\n50\\n51\\n52\\n53\\n54\\n55\\n56\\n57\\n58\\n59\\n60\\n61\\n62\\n63\\n64\\n65\\n66\\n67\\n68\\n69\\n70\\n71\\n72\\n73\\n74\\n75\\n76\\n77\\n78\\n79\\n80\\n81\\n82\\n83\\n84\\n85\\n86\\n87\\n88\\n89\\n90\\n91\\n92\\n93\\n94\\n95\\n96\\n97\\n98\\n99\\n'" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 12 | |
} | |
], | |
"source": [ | |
"uploaded_files['export.csv']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"files.view('export.csv')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 17 | |
}, | |
"id": "gfzoos1JALjF", | |
"outputId": "2bb16cac-c9d0-491e-f664-2b76fb2d807c" | |
}, | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"\n", | |
" ((filepath) => {{\n", | |
" if (!google.colab.kernel.accessAllowed) {{\n", | |
" return;\n", | |
" }}\n", | |
" google.colab.files.view(filepath);\n", | |
" }})(\"/content/sample_data/export.csv\")" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"files.view('projekty-karola-sliwki.jpeg')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 17 | |
}, | |
"id": "9mp-ogDfATTQ", | |
"outputId": "4baef2c2-308a-4162-942b-b2e833a462d6" | |
}, | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"\n", | |
" ((filepath) => {{\n", | |
" if (!google.colab.kernel.accessAllowed) {{\n", | |
" return;\n", | |
" }}\n", | |
" google.colab.files.view(filepath);\n", | |
" }})(\"/content/sample_data/projekty-karola-sliwki.jpeg\")" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# where I am?" | |
], | |
"metadata": { | |
"id": "Nazmn9P0N9hO" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%pwd" | |
], | |
"metadata": { | |
"id": "iNPAZF4MN9NW", | |
"outputId": "39b4b0ae-1286-47ce-8452-f1055c5b78d3", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'/content/sample_data'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 15 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# are my uploaded files here?" | |
], | |
"metadata": { | |
"id": "_tCwMxvjOCWA" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%ls -l" | |
], | |
"metadata": { | |
"id": "jzE1J69eOEkn", | |
"outputId": "edf29bdd-640e-4e10-cfcd-caf5395dfdab", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"total 55648\n", | |
"-rwxr-xr-x 1 root root 1697 Jan 1 2000 \u001b[0m\u001b[01;32manscombe.json\u001b[0m*\n", | |
"-rw-r--r-- 1 root root 301141 Dec 7 14:41 california_housing_test.csv\n", | |
"-rw-r--r-- 1 root root 1706430 Dec 7 14:41 california_housing_train.csv\n", | |
"-rw-r--r-- 1 root root 290 Dec 9 00:11 export.csv\n", | |
"-rw-r--r-- 1 root root 18289443 Dec 7 14:41 mnist_test.csv\n", | |
"-rw-r--r-- 1 root root 36523880 Dec 7 14:41 mnist_train_small.csv\n", | |
"-rw-r--r-- 1 root root 139840 Dec 9 00:11 projekty-karola-sliwki.jpeg\n", | |
"-rwxr-xr-x 1 root root 930 Jan 1 2000 \u001b[01;32mREADME.md\u001b[0m*\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# mount the google drive" | |
], | |
"metadata": { | |
"id": "MG-W1KLrA4SG" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import drive\n", | |
"drive.mount('/content/drive')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Era3j1p4Aapd", | |
"outputId": "8fa479b3-790b-4988-9f9d-0957af27a930" | |
}, | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Mounted at /content/drive\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# navigate to proper directory" | |
], | |
"metadata": { | |
"id": "V2bH8v01NdGK" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import os\n", | |
"os.chdir('/content/drive/My Drive/Colab Notebooks/upload file demo')" | |
], | |
"metadata": { | |
"id": "VztEahNxCXKc" | |
}, | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# make sure you are indeed there" | |
], | |
"metadata": { | |
"id": "yB4aT16mNgay" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%pwd" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"id": "gySLSlyDDBLS", | |
"outputId": "89fae2d0-39dd-4b1e-85bc-5df829b63778" | |
}, | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'/content/drive/My Drive/Colab Notebooks/upload file demo'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 20 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"uploaded_files = files.upload()" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 109 | |
}, | |
"id": "BJnDUbhwBvAM", | |
"outputId": "22da5a3f-64dd-4f7e-c103-106e92f5e9bb" | |
}, | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
], | |
"text/html": [ | |
"\n", | |
" <input type=\"file\" id=\"files-2468840b-6659-41b8-9735-3b444b39bccd\" name=\"files[]\" multiple disabled\n", | |
" style=\"border:none\" />\n", | |
" <output id=\"result-2468840b-6659-41b8-9735-3b444b39bccd\">\n", | |
" Upload widget is only available when the cell has been executed in the\n", | |
" current browser session. Please rerun this cell to enable.\n", | |
" </output>\n", | |
" <script>// Copyright 2017 Google LLC\n", | |
"//\n", | |
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n", | |
"// you may not use this file except in compliance with the License.\n", | |
"// You may obtain a copy of the License at\n", | |
"//\n", | |
"// http://www.apache.org/licenses/LICENSE-2.0\n", | |
"//\n", | |
"// Unless required by applicable law or agreed to in writing, software\n", | |
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n", | |
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | |
"// See the License for the specific language governing permissions and\n", | |
"// limitations under the License.\n", | |
"\n", | |
"/**\n", | |
" * @fileoverview Helpers for google.colab Python module.\n", | |
" */\n", | |
"(function(scope) {\n", | |
"function span(text, styleAttributes = {}) {\n", | |
" const element = document.createElement('span');\n", | |
" element.textContent = text;\n", | |
" for (const key of Object.keys(styleAttributes)) {\n", | |
" element.style[key] = styleAttributes[key];\n", | |
" }\n", | |
" return element;\n", | |
"}\n", | |
"\n", | |
"// Max number of bytes which will be uploaded at a time.\n", | |
"const MAX_PAYLOAD_SIZE = 100 * 1024;\n", | |
"\n", | |
"function _uploadFiles(inputId, outputId) {\n", | |
" const steps = uploadFilesStep(inputId, outputId);\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" // Cache steps on the outputElement to make it available for the next call\n", | |
" // to uploadFilesContinue from Python.\n", | |
" outputElement.steps = steps;\n", | |
"\n", | |
" return _uploadFilesContinue(outputId);\n", | |
"}\n", | |
"\n", | |
"// This is roughly an async generator (not supported in the browser yet),\n", | |
"// where there are multiple asynchronous steps and the Python side is going\n", | |
"// to poll for completion of each step.\n", | |
"// This uses a Promise to block the python side on completion of each step,\n", | |
"// then passes the result of the previous step as the input to the next step.\n", | |
"function _uploadFilesContinue(outputId) {\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" const steps = outputElement.steps;\n", | |
"\n", | |
" const next = steps.next(outputElement.lastPromiseValue);\n", | |
" return Promise.resolve(next.value.promise).then((value) => {\n", | |
" // Cache the last promise value to make it available to the next\n", | |
" // step of the generator.\n", | |
" outputElement.lastPromiseValue = value;\n", | |
" return next.value.response;\n", | |
" });\n", | |
"}\n", | |
"\n", | |
"/**\n", | |
" * Generator function which is called between each async step of the upload\n", | |
" * process.\n", | |
" * @param {string} inputId Element ID of the input file picker element.\n", | |
" * @param {string} outputId Element ID of the output display.\n", | |
" * @return {!Iterable<!Object>} Iterable of next steps.\n", | |
" */\n", | |
"function* uploadFilesStep(inputId, outputId) {\n", | |
" const inputElement = document.getElementById(inputId);\n", | |
" inputElement.disabled = false;\n", | |
"\n", | |
" const outputElement = document.getElementById(outputId);\n", | |
" outputElement.innerHTML = '';\n", | |
"\n", | |
" const pickedPromise = new Promise((resolve) => {\n", | |
" inputElement.addEventListener('change', (e) => {\n", | |
" resolve(e.target.files);\n", | |
" });\n", | |
" });\n", | |
"\n", | |
" const cancel = document.createElement('button');\n", | |
" inputElement.parentElement.appendChild(cancel);\n", | |
" cancel.textContent = 'Cancel upload';\n", | |
" const cancelPromise = new Promise((resolve) => {\n", | |
" cancel.onclick = () => {\n", | |
" resolve(null);\n", | |
" };\n", | |
" });\n", | |
"\n", | |
" // Wait for the user to pick the files.\n", | |
" const files = yield {\n", | |
" promise: Promise.race([pickedPromise, cancelPromise]),\n", | |
" response: {\n", | |
" action: 'starting',\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" cancel.remove();\n", | |
"\n", | |
" // Disable the input element since further picks are not allowed.\n", | |
" inputElement.disabled = true;\n", | |
"\n", | |
" if (!files) {\n", | |
" return {\n", | |
" response: {\n", | |
" action: 'complete',\n", | |
" }\n", | |
" };\n", | |
" }\n", | |
"\n", | |
" for (const file of files) {\n", | |
" const li = document.createElement('li');\n", | |
" li.append(span(file.name, {fontWeight: 'bold'}));\n", | |
" li.append(span(\n", | |
" `(${file.type || 'n/a'}) - ${file.size} bytes, ` +\n", | |
" `last modified: ${\n", | |
" file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() :\n", | |
" 'n/a'} - `));\n", | |
" const percent = span('0% done');\n", | |
" li.appendChild(percent);\n", | |
"\n", | |
" outputElement.appendChild(li);\n", | |
"\n", | |
" const fileDataPromise = new Promise((resolve) => {\n", | |
" const reader = new FileReader();\n", | |
" reader.onload = (e) => {\n", | |
" resolve(e.target.result);\n", | |
" };\n", | |
" reader.readAsArrayBuffer(file);\n", | |
" });\n", | |
" // Wait for the data to be ready.\n", | |
" let fileData = yield {\n", | |
" promise: fileDataPromise,\n", | |
" response: {\n", | |
" action: 'continue',\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" // Use a chunked sending to avoid message size limits. See b/62115660.\n", | |
" let position = 0;\n", | |
" do {\n", | |
" const length = Math.min(fileData.byteLength - position, MAX_PAYLOAD_SIZE);\n", | |
" const chunk = new Uint8Array(fileData, position, length);\n", | |
" position += length;\n", | |
"\n", | |
" const base64 = btoa(String.fromCharCode.apply(null, chunk));\n", | |
" yield {\n", | |
" response: {\n", | |
" action: 'append',\n", | |
" file: file.name,\n", | |
" data: base64,\n", | |
" },\n", | |
" };\n", | |
"\n", | |
" let percentDone = fileData.byteLength === 0 ?\n", | |
" 100 :\n", | |
" Math.round((position / fileData.byteLength) * 100);\n", | |
" percent.textContent = `${percentDone}% done`;\n", | |
"\n", | |
" } while (position < fileData.byteLength);\n", | |
" }\n", | |
"\n", | |
" // All done.\n", | |
" yield {\n", | |
" response: {\n", | |
" action: 'complete',\n", | |
" }\n", | |
" };\n", | |
"}\n", | |
"\n", | |
"scope.google = scope.google || {};\n", | |
"scope.google.colab = scope.google.colab || {};\n", | |
"scope.google.colab._files = {\n", | |
" _uploadFiles,\n", | |
" _uploadFilesContinue,\n", | |
"};\n", | |
"})(self);\n", | |
"</script> " | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Saving export.csv to export (1).csv\n", | |
"Saving projekty-karola-sliwki.jpeg to projekty-karola-sliwki (1).jpeg\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"%ls -l" | |
], | |
"metadata": { | |
"id": "8Juimy6rBXQg", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "8dad6a8d-f9ec-4305-d7df-31ba0ea29a8a" | |
}, | |
"execution_count": 27, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"total 157\n", | |
"-rw------- 1 root root 290 Dec 8 23:22 export.csv\n", | |
"-rw------- 1 root root 139840 Dec 8 23:22 projekty-karola-sliwki.jpeg\n", | |
"-rw------- 1 root root 19886 Dec 9 00:09 upload_file.ipynb\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "utMALLSDEdXm" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOSl41V4vSRmHoXL3PAKCbU", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"name": "python3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment