Skip to content

Instantly share code, notes, and snippets.

@mariobuikhuizen
Created March 11, 2021 18:02
Show Gist options
  • Save mariobuikhuizen/f5170f0e3d0ab1126083436a7fc48889 to your computer and use it in GitHub Desktop.
Save mariobuikhuizen/f5170f0e3d0ab1126083436a7fc48889 to your computer and use it in GitHub Desktop.
Download
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "surprised-intranet",
"metadata": {},
"outputs": [],
"source": [
"import ipyvuetify as vy\n",
"import traitlets\n",
"\n",
"\n",
"class MyDownloader(vy.VuetifyTemplate):\n",
" file_data = traitlets.Dict().tag(sync=True)\n",
" \n",
" template = traitlets.Unicode('''\n",
" <template>\n",
" <a ref=\"dataAnchor\" :href=\"file_data.data_url\" :download=\"file_data.name\" style=\"display: none\"></a>\n",
" </template>\n",
" <script>\n",
" modules.export = {\n",
" watch: {\n",
" file_data() { \n",
" this.$nextTick(() => {\n",
" this.$refs.dataAnchor.click()\n",
" })\n",
" }\n",
" }\n",
" }\n",
" </script>\n",
" ''').tag(sync=True)\n",
"\n",
"dl = MyDownloader()\n",
"dl"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "authentic-denver",
"metadata": {},
"outputs": [],
"source": [
"from base64 import b64encode\n",
"from random import random\n",
"\n",
"btn = vy.Btn(children=[\"download\"])\n",
"\n",
"def on_click(*ignore):\n",
" kind = 'text'\n",
" content_b64 = b64encode('some text'.encode()).decode()\n",
" dl.file_data = {\n",
" 'name': 'someFile5.txt',\n",
" 'data_url': f'data:{kind};charset=utf-8;base64,{content_b64}',\n",
" 'some_random_data_so_vue_detects_a_change': random()\n",
" }\n",
"btn.on_event('click', on_click)\n",
"\n",
"btn"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "negative-bloom",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment