title: Workaround for wrapping a JS function in python in Jupyter notebook
body: <p>So in an IPython Jupyter notebook one can add JavaScript functions via the <code>%% javascript</code> magic cell syntax or via the Python kernel with <code>IPython.display.HTML</code> and one can alter Python variables in JS with <code>IPython.notebook.kernel.execute</code>.</p>
  
  <p>However, the kernel call is done when the kernel is idle.</p>
  
  <p>cell 1, magic cell JS to add the function.</p>
  
  <pre><code>%%javascript
  window.act = () =&gt; IPython.notebook.kernel.execute('flag = False');
  </code></pre>
  
  <p>cell 2, python kernel</p>
  
  <pre><code>from IPython.display import display, HTML
  flag = True
  display(HTML('''&lt;p id="newDOMElement"&gt;New DOM element added &lt;/div&gt;
                  &lt;script type="text/Javascript"&gt;
                      act();
                      $('#newDOMElement').append('&lt;b&gt;and changed&lt;/b&gt;.');
                  &lt;/script&gt;'''))
  import time
  time.sleep(2)  #wait in case it's a JS async issue.
  print('JS did not change Py variable.' if flag else 'JS successfully changed Py variable.')
  &gt;&gt; New DOM element added and changed.
  &gt;&gt; JS did not change Py variable.
  </code></pre>
  
  <p>The result shows the JS is working and changing the <code>#newDOMElement</code> element. But the kernel did not change as it waited.
  In fact, once the cell was done with the kernel, the variable changed. As show with this.</p>
  
  <pre><code>print('JS did not change Py variable.' if flag else 'JS successfully changed Py variable.')
  &gt;&gt; JS successfully changed Py variable.
  </code></pre>
  
  <p>This experiment means that one cannot wrap the JS function in a Python method that changes data continually at runtime. Is there a way around this?</p>