Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active February 28, 2022 22:26
Show Gist options
  • Save fomightez/f7e6844751b887cb0f4eaaf206fd1642 to your computer and use it in GitHub Desktop.
Save fomightez/f7e6844751b887cb0f4eaaf206fd1642 to your computer and use it in GitHub Desktop.
close to solution to widget_example_finish_indicator
# towards addressing https://discourse.jupyter.org/t/how-to-make-start-and-stop-buttons/8331?u=fomightez
# Paste this into a cell after going to https://github.com/fekad/jupyter-jsmol and pressing `launch binder` to start a session.
# Currently it doesn't update the status text below if you run the process to completion (10 seconds). I tried several ways to trigger that and none seemed to update or add that to `self.putput`
import ipywidgets as widgets
from ipywidgets import Output
from IPython.display import display, Javascript
from traitlets import Unicode, validate
import time
import threading
class Timer():
def __init__(self,limit=10):
#self.value = Unicode('00:00:00').tag(sync=True)
#self.labwidg = widgets.Button(description="Start",layout=lay(120,50))
self.labwidg = widgets.Button(description="Start")
self.labwidg.on_click(self.threadTimer)
#self.labwidgstop =widgets.Button(description="Stop",layout=lay(120,50))
self.labwidgstop =widgets.Button(description="Stop")
self.labwidgstop.on_click(self.stopTime)
self.labwidg.on_click(self.threadTimer)
self.output = widgets.Output()
with self.output:
print("\nProcess status:")
#self.labwidg.style.button_color = "gree"
#widgets.Label('00:00:00')
#display(self.labwidg)
self.limit = limit
self.stopTimer = False
self.thread = None
self.buttonState = "Start"
def timeit(self, limit=10):
#display(self)
hours = 0
mins = 0
secs = 0
for i in range(1,(limit+1)):
if i > 9:
with self.output:
print("\nProcess finished")
self.labwidg.description = 'Finished'
self.buttonState == "Finished"
self.thread.stop = True
if(self.buttonState == "Start"):
self.labwidg.button_style="primary"
#self.labwidg.style.font_weight = "50px"
elif(self.buttonState == "Stop"):
self.labwidg.button_style="success"
#self.labwidg.style.font_weight = "50px"
elif(self.buttonState == "Reset"):
self.labwidg.button_style="warning"
#self.labwidg.style.font_weight = "50px"
if(self.stopTimer):
self.stopTimer = False
self.thread = None
break
if i%60 == 0:
if i%3600 == 0:
secs = 0
mins = 0
hours += 1
else:
secs = 0
mins += 1
else:
secs += 1
time.sleep(0.5)
#self.labwidg.description = '{hour:02}:{minute:02}:{second:02}'.format(hour=hours,minute=mins,second=secs)
self.labwidg.description = f"\rRound {i}"
self.labwidg.description = 'Finished'
#def twrap(self,b,limit=180):
# self.timeit(limit=limit)
def threadTimer(self,b):
#b.description = self.buttonState
if(self.buttonState == "Start"):
if(self.thread == None):
self.thread = threading.Thread(target=self.timeit,args=(self.limit,))
self.thread.start()
with self.output:
print("\nProcess started")
self.buttonState = "Stop"
elif(self.buttonState == "Stop"):
if(self.thread != None):
self.stopTimer=True
self.buttonState = "Reset"
else:
self.labwidg.description = 'Start'
self.Thread = None
self.stopTimer=False
self.buttonState = "Start"
def stopTime(self,b):
self.stopTimer=True
with self.output:
print("\nProcess stopped")
#def startTimer(b,timer):
#, args=(,))
def lay(width,height):
return widgets.Layout(width=str(width)+"px",height=str(height)+"px")
def display_timer():
timer = Timer()
display(timer.labwidg)
display(timer.labwidgstop)
display(timer.output)
display_timer()
@fomightez
Copy link
Author

fomightez commented Feb 28, 2022

There's an example here of a related example of using a button to stop am iterating process in answer to Kill a loop with Button Jupyter Notebook?. It has multiple approaches, including one with multiprocessing.

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