<jupyter_start><jupyter_text>notebookJS Colab HelloWorldIn this notebook, we show how to use notebookJS to run custom Javascript code. We also show how to send messages between Python and Javascript.<jupyter_code>!pip install notebookjs<jupyter_output><empty_output><jupyter_text>Defining the JavaScript drawing function.The function requests a message to python every 1 second. The callback identifier is called *get_hello*<jupyter_code>helloworld_js = """
function helloworld(div_id, data){
    comm = new CommAPI("get_hello", (ret) => {
      document.querySelector(div_id).textContent = ret.text;
    });
    setInterval(() => {comm.call({})}, 1000);
    comm.call({});
}
"""<jupyter_output><empty_output><jupyter_text>Defining the Python callback<jupyter_code>import random
def hello_world_random(data):
  hello_world_languages = [
      "Ola Mundo", # Portuguese
      "Hello World", # English
      "<NAME>", # Spanish
      "Geiá sou Kósme", # Greek
      "Kon'nichiwa sekai", # Japanese
      "Hallo Welt", # German
      "namaste duniya" #Hindi
  ]
  i = random.randint(0, len(hello_world_languages)-1)
  return {'text': hello_world_languages[i]}<jupyter_output><empty_output><jupyter_text>Running the javascript function with Python callbacks<jupyter_code>from notebookjs import execute_js
execute_js(helloworld_js, "helloworld", callbacks={"get_hello": hello_world_random})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.111660777385159, 'fraction_numerical': 0.0049469964664310955, 'mean_word_length': 5.710900473933649, 'pattern_counts': {'":': 1, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '140483', 'license': 'MIT', 'n_tokens_mistral': 469, 'n_tokens_neox': 454, 'n_words': 109, 'path': 'Examples/6_HelloWorld_Bidirectional/HelloWorld_Bidirectional.ipynb', 'repo_name': 'mateuszz0000/notebookJS', 'size': '1419'}