ecolab.register_js_fn

Contents

ecolab.register_js_fn#

etils.ecolab.register_js_fn(fn: etils.ecolab.pyjs_com.py_js_com._FnT) etils.ecolab.pyjs_com.py_js_com._FnT[source]#

Decorator to make a function callable from Javascript.

Usage:

In Python:

@register_js_fn
def my_fn(*args, **kwargs):
  return {'x': 123}

The function can then be called from Javascript:

# Currently has to be executed in the same cell to install the library
IPython.display.display(IPython.display.HTML(ecolab.pyjs_import()))

IPython.display.HTML(\"\"\"
<script>
  async function main() {
    out = await call_python('my_fn', [1, 2], {z: 3});
    console.log(out['sum']);  // my_fn(1, 2, z=3)  == {'sum': 6}
  }
  main();
</script>
\"\"\")

Note that Javascript require the pyjs_com.js_import() statement to be present in the HTML from the cell.

Parameters:

fn – The Python function, can return any json-like value or dict

Returns:

The Python function, unmodified