I am trying to show the execution of a Python program in Jupyter, step by step. For example, I can visualize the value of a variable in a program as in the following toy program:
from IPython.display import display, clear_output
from time import sleep
def sum_first_integers(n):
res = 0
for i in range(n+1):
res += i
clear_output(wait=True)
display(res)
sleep(.5)
return res
This shows the value of res
at each step of the algorithm, and I added a sleep(.5)
to be able to actually view the execution of the algorithm. My question is whether there exists a better way of performing this visualization:
- Is it possible (with
ipywidgets
for instance) to make a button "Next step", such that one needs to hit the button before the loop continues to its next step? - Is it possible to add other buttons such as "Run" & "Stop" such that when "Run" is clicked, the algorithm runs (with for example some speed adjustment that would use
sleep
), and when "Stop" is clicked the algorithm is stopped?