Skip to content

Instantly share code, notes, and snippets.

@the-moog
Last active February 2, 2023 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save the-moog/94b09b49232731bd2a3cedd24501e23b to your computer and use it in GitHub Desktop.
Save the-moog/94b09b49232731bd2a3cedd24501e23b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@the-moog
Copy link
Author

the-moog commented Jan 5, 2020

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

@sapristi
Copy link

sapristi commented Oct 21, 2020

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

@Ivorforce
Copy link

Ivorforce commented Dec 11, 2020

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
Copy link

sapristi commented Dec 12, 2020

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