Execute algorithm step by step in Jupyter

我的梦境 提交于 2019-12-10 10:06:07

问题


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?

来源:https://stackoverflow.com/questions/39334589/execute-algorithm-step-by-step-in-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!