Skip to content

Instantly share code, notes, and snippets.

@the-moog
Last active February 2, 2023 12:30
Show Gist options
  • Select an option

  • Save the-moog/94b09b49232731bd2a3cedd24501e23b to your computer and use it in GitHub Desktop.

Select an option

Save the-moog/94b09b49232731bd2a3cedd24501e23b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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
}
@the-moog

the-moog commented Jan 5, 2020

Copy link
Copy Markdown
Author

I think this is emulating what the Python 3 Asyncio module does, I'll try and write a version that uses that instead

@sapristi

sapristi commented Oct 21, 2020

Copy link
Copy Markdown

Thank you for this ! Only snippet that I managed to achieve what I wanted to do with !

@Ivorforce

Ivorforce commented Dec 11, 2020

Copy link
Copy Markdown

For anyone seeking a more minimalized, technical example:

import threading
from IPython.display import display
import ipywidgets as widgets
import time
from zmq.eventloop.ioloop import IOLoop

progress = widgets.FloatProgress(value=0, min=0, max=10)
display(progress)

def run(progress, ioloop):
    def update_progress(i, progress=progress):
        progress.value = i

    for i in range(10):
        time.sleep(1)
        ioloop.add_callback(update_progress, i)

def run_progress(*args, **kwargs):
    thread = threading.Thread(target=run, args=(progress, IOLoop.instance()))
    thread.start()

button = widgets.Button(description="Start")
button.on_click(run_progress)
display(button)

@sapristi

sapristi commented Dec 12, 2020

Copy link
Copy Markdown

Also I suggest importing IOLoop directly from tornado, since that's what's used by jupyter, and there's no need to install zmq to get it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment