You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was able to get the progress bar sample to mostly work with anywidget by doing something like the following:
importanywidgetimporttraitletsimportthreadingimporttimeclassBackgroundProgress(anywidget.AnyWidget):
_esm=""" export function render({ model, el }) { let output = document.createElement('div'); output.classList.add('progress'); let bar = document.createElement('div'); bar.classList.add('progress-bar'); bar.style.height = "20px"; bar.style.backgroundColor = "blue"; output.appendChild(bar); el.append(output); function update_output() { bar.style.width = `${model.get('value')}px`; } update_output(); model.on('change:value', update_output); } """_css=""" """value=traitlets.Int(0).tag(sync=True)
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# if msg.get('event') == 'increment':# Define the blocking work to be done in a separate threaddefblocking_work():
for_inrange(100):
time.sleep(1.0) # Simulate blocking workself.value+=1# Start a new thread to run the blocking workthread=threading.Thread(target=blocking_work)
thread.start()
# Instantiate and display the widgetcounter_widget=BackgroundProgress()
counter_widget
For responding to user input while another cell is running, I haven't been able to find a solution.
This gets close-ish, but the on_msg handler doesn't run until any other running cells have stopped. I've been using the following cell in Jupyter Lab to test this out:
While it is running, the button widget doesn't get any UI updates.
I'm curious to hear if folks have found a solution to this? Perhaps it has to do with the fact that Traitlets is pretty synchronous? I found jupyter-widgets/ipywidgets#3293 for a request to migrate off of it for better true async support.
I'm aware of 3 solutions for concurrency right now in Python:
Multiprocessing: I don't think this applies to anywidget, but correct me if I'm wrong.
Threading: Kinda works for long-running work, but I couldn't figure out how to get event handlers to run in a background thread.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In the Jupyter Widgets docs, I found this short guide on building widgets that can update and/or respond to user input asynchronously / non-blocking. https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Asynchronous.html#updating-a-widget-in-the-background
I was able to get the progress bar sample to mostly work with anywidget by doing something like the following:
For responding to user input while another cell is running, I haven't been able to find a solution.
This gets close-ish, but the
on_msghandler doesn't run until any other running cells have stopped. I've been using the following cell in Jupyter Lab to test this out:While it is running, the button widget doesn't get any UI updates.
I'm curious to hear if folks have found a solution to this? Perhaps it has to do with the fact that Traitlets is pretty synchronous? I found jupyter-widgets/ipywidgets#3293 for a request to migrate off of it for better true async support.
I'm aware of 3 solutions for concurrency right now in Python:
Beta Was this translation helpful? Give feedback.
All reactions