Skip to content

Commit

Permalink
docs: add examples for FileInput
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobuikhuizen committed Sep 7, 2020
1 parent b926aca commit e6d11bd
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 0 deletions.
148 changes: 148 additions & 0 deletions examples/extra/FileInput example random access.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from ipyvuetify.extra import FileInput\n",
"from IPython.display import Image\n",
"import zipfile\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Download http://images.cocodataset.org/zips/val2017.zip and choose it in the FileInput below\n",
"\n",
"file_input = FileInput(show_progress=False,multiple=False)\n",
"file_input"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"myfile = file_input.get_files()[0]\n",
"myfile"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"archive = zipfile.ZipFile(myfile['file_obj'], 'r')\n",
"listing = archive.namelist()\n",
"# After reading from the file the output has changed to the cell that contains the FileInput, so we display the listing in the next cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"listing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jpgs = [x for x in listing if x.endswith('jpg')]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image1 = archive.read(jpgs[random.randint(0, len(jpgs)-1)])\n",
"\n",
"# After reading from the file the output has changed to the cell that contains the FileInput, so we display the widget in the next cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"Image(image1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = widgets.Image(format='jpg',)\n",
"\n",
"def load_rnd_image(*ignored):\n",
" show_progress=True\n",
" image.value = archive.read(jpgs[random.randint(0, len(jpgs)-1)])\n",
" show_progress=False\n",
" \n",
"btn = widgets.Button(description='show random image')\n",
"btn.on_click(load_rnd_image)\n",
"\n",
"widgets.VBox([widgets.Box([image]), btn])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'Percentage of data read: {file_input.stats[0]/myfile[\"size\"]}%')"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
106 changes: 106 additions & 0 deletions examples/extra/FileInput.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from ipyvuetify.extra import FileInput\n",
"import hashlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_input = FileInput()\n",
"file_input"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# first select some files in the FileInput above\n",
"\n",
"myfiles = file_input.get_files()\n",
"myfiles[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"output = widgets.Output()\n",
"output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_input.disabled = True\n",
"\n",
"for file in myfiles:\n",
" file['file_obj'].seek(0)\n",
" data = file['file_obj'].read()\n",
" md5_sum = hashlib.md5(data).hexdigest()\n",
" with output:\n",
" print(f'file: {file[\"name\"]}, md5: {md5_sum}')\n",
"\n",
"file_input.disabled = False\n",
"with output:\n",
" print(f'done.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_input.clear()"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit e6d11bd

Please sign in to comment.