Demo of issues with zip file downloads from JupyterLab
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", | |
"id": "0c995618", | |
"metadata": {}, | |
"source": [ | |
"# Zipfile download errors from JupyterLab\n", | |
"\n", | |
"[](https://mybinder.org/v2/gist/fperez/33e34dc363408cc9421aa15fa5c9f549/HEAD?urlpath=lab)\n", | |
"\n", | |
"This notebook illustrates a problem that arises in JupyterLab, where accessing local file links for binary data (at least we're seeing it with zip files, it may occur with other types).\n", | |
"\n", | |
"When using Classic _under JupyterHub_, the zip files do download correctly, though a local version of Classic also displays a similar error." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "b2324a6d-70f9-492f-9eba-e5d6928a7ef9", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def showfiles(tpath, zpath):\n", | |
" from IPython.display import display, HTML, FileLink\n", | |
" \n", | |
" print(\"File links generated without download attributes:\")\n", | |
" display(HTML(f'<p>Files: <a href={tpath} target=\"_blank\">text</a>, '\n", | |
" f'<a href={zpath} target=\"_blank\">zip</a>.'\n", | |
" f'</p>'))\n", | |
" \n", | |
" print(\"File links generated with download attributes:\")\n", | |
" display(HTML(f'<p>Files: <a href={tpath} download={tpath} target=\"_blank\">text</a>, '\n", | |
" f'<a href={zpath} download={tpath} target=\"_blank\">zip</a>.'\n", | |
" f'</p>'))\n", | |
" print(\"File links generated with IPython.display.FileLink:\")\n", | |
" display(\"Text:\", FileLink(tpath), \"Zip:\", FileLink(zpath))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "62e45125-a412-4603-a80b-ee8d7bce8d6c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from zipfile import ZipFile\n", | |
"\n", | |
"zpath = 'test.zip'\n", | |
"tpath = 'msg.txt'\n", | |
"\n", | |
"with open(tpath, 'w') as t:\n", | |
" t.write('blah blah...')\n", | |
" \n", | |
"with ZipFile(zpath, 'w') as z:\n", | |
" z.write(tpath)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3e344799-4a3c-4769-9cfc-c69d8eb1d995", | |
"metadata": {}, | |
"source": [ | |
"Now, let's run this function, which will produce several links. Try clicking on each - a JHub-hosted version of Classic allows the user to download the zip file, while under Lab, a \"File Load Error ...\n", | |
"<file> is not UTF-8 encoded\" error dialog is produced.\n", | |
" \n", | |
"Both Classic and Lab open the text file correctly, each in their text editor." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "4b829990-fdf7-45ef-9707-53074533ca92", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"File links generated without download attributes:\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<p>Files: <a href=msg.txt target=\"_blank\">text</a>, <a href=test.zip target=\"_blank\">zip</a>.</p>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"File links generated with download attributes:\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<p>Files: <a href=msg.txt download=msg.txt target=\"_blank\">text</a>, <a href=test.zip download=msg.txt target=\"_blank\">zip</a>.</p>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"File links generated with IPython.display.FileLink:\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'Text:'" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<a href='msg.txt' target='_blank'>msg.txt</a><br>" | |
], | |
"text/plain": [ | |
"/Users/fperez/Downloads/msg.txt" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'Zip:'" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<a href='test.zip' target='_blank'>test.zip</a><br>" | |
], | |
"text/plain": [ | |
"/Users/fperez/Downloads/test.zip" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"showfiles(tpath, zpath)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "95fa5407-69d8-4f7d-b7f8-5a76c5f3304b", | |
"metadata": {}, | |
"source": [ | |
"For completeness, we can check that the same behavior happens with markdown-created links as well:" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5f1880ec-8790-4d7e-b0a0-976fd9e0c800", | |
"metadata": {}, | |
"source": [ | |
"---\n", | |
"This is a markdown cell with HTML links to the same two files:\n", | |
"* [Text file](msg.txt).\n", | |
"* [Zip file](test.zip)." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.9.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment