Skip to content

Instantly share code, notes, and snippets.

@andilabs
Created December 8, 2022 22:34
Show Gist options
  • Save andilabs/95fe6dcd08b1906c1839c007de5722a4 to your computer and use it in GitHub Desktop.
Save andilabs/95fe6dcd08b1906c1839c007de5722a4 to your computer and use it in GitHub Desktop.
download-file.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyN662A/xeXZ6/h+AsmoXT1m",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/andilabs/95fe6dcd08b1906c1839c007de5722a4/download-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": 1,
"metadata": {
"id": "8VwRQuML1oFE"
},
"outputs": [],
"source": [
"from google.colab import files\n"
]
},
{
"cell_type": "code",
"source": [
"with open('export.csv', 'w') as F:\n",
" for i in range(100):\n",
" F.write(f'{i}\\n')"
],
"metadata": {
"id": "T4L8NWg21skC"
},
"execution_count": 14,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Preview the file"
],
"metadata": {
"id": "A3FRKI3Z3C3i"
}
},
{
"cell_type": "code",
"source": [
"files.view('export.csv')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
},
"id": "m8sOGrxA3BH8",
"outputId": "01842ce6-72f9-46f7-d909-8dcbecc5f88c"
},
"execution_count": 17,
"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/export.csv\")"
]
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": [
"# Download the file"
],
"metadata": {
"id": "-vDeAJZ43EYu"
}
},
{
"cell_type": "code",
"source": [
"files.download('export.csv')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "MAfZmAe61qvO",
"outputId": "d1a14937-f384-4d30-dcf0-de5f31d86f42"
},
"execution_count": 15,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"download(\"download_e4b1238e-9baa-40e2-865a-27294759da86\", \"export.csv\", 290)"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
},
"id": "nAWzJjxK2kKH",
"outputId": "b1c18744-13a4-4f87-c84f-d4c62fb79df3"
},
"execution_count": 16,
"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/export.csv\")"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "53xyHm_V2nwf"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment