title: How to restart kernel and rerun all cells in Python Jupyter notebook through code or by an algorithm?
body: <p>Need a Python Script that will restart the kernel and rerun all the cells automatically without any human intervention.</p>
  <p>I tried the following code below but needs a human intervention since it uses a toggle button.</p>
  <pre><code>from IPython.display import HTML, Javascript, display
  
  def initialize():
      display(HTML(
          '''
              &lt;script&gt;
                  code_show = false;
                  function restart_run_all(){
                      IPython.notebook.kernel.restart();
                      setTimeout(function(){
                          IPython.notebook.execute_all_cells();
                      }, 10000)
                  }
                  function code_toggle() {
                      if (code_show) {
                          $('div.input').hide(200);
                      } else {
                          $('div.input').show(200);
                      }
                      code_show = !code_show
                  }
              &lt;/script&gt;
              &lt;button onclick=&quot;code_toggle()&quot;&gt;Click to toggle&lt;/button&gt;
              &lt;button onclick=&quot;restart_run_all()&quot;&gt;Click to Restart and Run all Cells&lt;/button&gt;
          '''
      ))
  initialize()
  </code></pre>