Lauch simultaneous python methods from javascript in Google Colab

Viewed 344

This code create a small html in the cell with two buttons who execute python methods. I would like that when I press the two buttons, the two methods run simultaneously instead of one at a time. This code is meant to be placed in a Google Colab.

import time
from IPython.display import display, Javascript, HTML
from google.colab import output

def funA():
  for i in range(10):
    time.sleep(0.5)
    print("A : {}".format(i))
    pass
  return

def funB():
  for i in range(10):
    time.sleep(0.5)
    print("B : {}".format(i))
    pass
  return

output.register_callback('funA', funA) 
output.register_callback('funB', funB)

display(Javascript('''
butA = document.createElement("button");
butB = document.createElement("button");
butA.innerHTML = "A";
butB.innerHTML = "B";
invoke = google.colab.kernel.invokeFunction;
butA.onclick = async function () {await invoke("funA", [], {});}
butB.onclick = async function () {await invoke("funB", [], {});}
document.body.appendChild(butA);
document.body.appendChild(butB);
'''))
0 Answers
Related