-
-
Save s1lvester/2c73c6e7095e3d6214f4884270c53f20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "\nimport threading\nfrom IPython.display import display\nimport ipywidgets as widgets\nimport time\n\ndef get_ioloop():\n import IPython, zmq\n ipython = IPython.get_ipython()\n if ipython and hasattr(ipython, 'kernel'):\n return zmq.eventloop.ioloop.IOLoop.instance()\n\n\n#The IOloop is shared\nioloop = get_ioloop()\n\n\nclass MyThread(threading.Thread):\n def __init__(self):\n super().__init__()\n \n self.progress = widgets.FloatProgress(value=0, min=0, max=10)\n \n self._quit = threading.Event()\n self.start()\n \n def run(self):\n i = 0\n while not self._quit.isSet():\n def update_progress(i=i):\n if self._quit.isSet():\n return\n if self.progress.value == 10:\n self.progress.value = 0\n self.progress.value = i\n\n time.sleep(2)\n\n ioloop.add_callback(update_progress)\n\n i += 1\n if i > 10:\n i = 0\n self.progress.value = 10\n print(\"Quit\")\n \n def quit(self):\n self._quit.set()\n\ndef run_progress():\n thread = MyThread()\n display(thread.progress)\n return thread\n", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#Non blocking cell\nt1 = run_progress()", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "69dca3ea7ff544968303b0161cc1f8a6", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": "FloatProgress(value=0.0, max=10.0)" | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#Non blocking cell\nt2 = run_progress()", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "bfc25cf7eece4fe9bd090a11677c2c5f", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": "FloatProgress(value=0.0, max=10.0)" | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#Blocking cell\ntime.sleep(5)", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "t1.quit()", | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "t2.quit()", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Quit\nQuit\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.8.1", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment