3

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?
Bruno
  • 437
  • 4
  • 8
  • 1
    Maybe this is what you're looking for: http://stackoverflow.com/questions/32409629/what-is-the-right-way-to-debug-in-ipython-notebook – adabsurdum Sep 05 '16 at 16:57
  • @adabsurdum Thanks, I did not think about this solution. Now I have to check how I could make `ipdb` work with `interact` in order to make a user-friendly interface. – Bruno Sep 06 '16 at 12:18

0 Answers0