Skip to content

Instantly share code, notes, and snippets.

@gfacciol
Last active November 15, 2020 15:19
Show Gist options
  • Save gfacciol/2e66885b881b040e35712ecc3c75e10a to your computer and use it in GitHub Desktop.
Save gfacciol/2e66885b881b040e35712ecc3c75e10a to your computer and use it in GitHub Desktop.
appmode jupyter notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# appmode example\n",
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gist/gfacciol/2e66885b881b040e35712ecc3c75e10a/master?urlpath=%2Fapps%2Fappmode-notebook.ipynb)\n",
"\n",
"https://github.com/oschuett/appmode\n",
"\n",
"\n",
"not sure if relevant ?\n",
"\n",
"https://makina-corpus.com/blog/metier/2019/augmenter-linteractivite-de-vos-notebooks-jupyter-1\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# hide the appmode edit button not really secure \n",
"# https://github.com/oschuett/appmode/issues/7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%javascript\n",
"$('#appmode-leave').hide();\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import ipywidgets as widgets\n",
"import ipywidgets as ipw\n",
"\n",
"\n",
"\n",
"outputtext = widgets.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"), disabled=True)\n",
"\n",
"\n",
"#file = open(\"images/WidgetArch.png\", \"rb\")\n",
"img = widgets.Image(\n",
"# value=image,\n",
" format='png',\n",
"# width=300,\n",
"# height=400,\n",
")\n",
"\n",
"d=0\n",
"\n",
"\n",
"widgets.FileUpload(\n",
" accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n",
" multiple=True # True to accept multiple files upload else False\n",
")\n",
"\n",
"\n",
"uploader = widgets.FileUpload()\n",
"\n",
"#out = widgets.Output() # unused\n",
" \n",
" \n",
"#\n",
"#def derivative(img):\n",
"# import matplotlib.pylab as plt\n",
"# import numpy as np\n",
"# \n",
"# der = np.roll(img,1, axis=0) - img\n",
"# \n",
"## plt.figure()\n",
"# plt.imshow(der)\n",
"# return \"ok \"\n",
"# \n",
"# \n",
"#def convert_image_to_numpy_array(img):\n",
"# import io\n",
"# from PIL import Image\n",
"# import numpy as np \n",
"# \n",
"# imgx = Image.open(io.BytesIO(img.value))\n",
"# imgx = np.array(imgx, dtype=float)\n",
"# return imgx\n",
"#\n",
"\n",
"\n",
"\n",
"# process the file upload events (or call to load button)\n",
"def tt(x):\n",
" global d\n",
" if not uploader.value == {}:\n",
" ll = [ uploader.value[f][\"content\"] for f in uploader.value.keys()] \n",
" img.value= ll[0]\n",
" d=d-1\n",
" \n",
" #imgx = convert_image_to_numpy_array(img)\n",
" #result = derivative(imgx)\n",
" \n",
" outputtext.value+=\"ok\" \n",
" else:\n",
" d=d+1\n",
" outputtext.value='%d'%d\n",
" \n",
"\n",
"# call upload event upon change of upload: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html\n",
"uploader.observe(tt)\n",
"\n",
"\n",
"# redundant with uploader\n",
"#btn = widgets.Button(description='run', layout=widgets.Layout(width=\"150px\"))\n",
"#btn.on_click(tt)\n",
"\n",
"\n",
"widgets.VBox((uploader, #btn, \n",
" outputtext, img))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## example with a from \n",
"https://github.com/oschuett/appmode/blob/master/example_app.ipynb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import division\n",
"import ipywidgets as ipw\n",
"\n",
"output = ipw.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"), disabled=True)\n",
"\n",
"def on_click(btn):\n",
" if btn.description == \"=\":\n",
" try:\n",
" output.value = str(eval(output.value))\n",
" except:\n",
" output.value = \"ERROR\"\n",
" elif btn.description == \"AC\":\n",
" output.value = \"\"\n",
" elif btn.description == \"del\":\n",
" output.value = output.value[:-1]\n",
" else:\n",
" output.value = output.value + btn.description\n",
"\n",
"def mk_btn(description):\n",
" btn = ipw.Button(description=description, layout=ipw.Layout(width=\"45px\"))\n",
" btn.on_click(on_click)\n",
" return btn\n",
"\n",
"row0 = ipw.HBox([mk_btn(d) for d in (\"(\", \")\", \"del\", \"AC\")])\n",
"row1 = ipw.HBox([mk_btn(d) for d in (\"7\", \"8\", \"9\", \" / \")])\n",
"row2 = ipw.HBox([mk_btn(d) for d in (\"4\", \"5\", \"6\", \" * \")])\n",
"row3 = ipw.HBox([mk_btn(d) for d in (\"1\", \"2\", \"3\", \" - \")])\n",
"row4 = ipw.HBox([mk_btn(d) for d in (\"0\", \".\", \"=\", \" + \")])\n",
"ipw.VBox((output, row0, row1, row2, row3, row4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## more examples with volume rendering\n",
"\n",
"https://github.com/binder-examples/appmode\n",
"\n",
"https://ipyvolume.readthedocs.io/en/latest/"
]
},
{
"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
}
channels:
- conda-forge
dependencies:
- appmode
- pillow
- matplotlib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment