-
-
Save pplonski/771523e89348af65b7f01e2567821002 to your computer and use it in GitHub Desktop.
Calling python function from JS in Colab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import HTML, JSON | |
from google.colab import output | |
# first, register the python function to use | |
def array(): | |
return JSON([1,2,3]) # can send any JSON | |
output.register_callback('array', array) | |
# then invoke it later in an %%html block | |
%%html | |
{{PRINT}} | |
<script> | |
(async function() { | |
invoke = google.colab.kernel.invokeFunction | |
result = await invoke('array', [], {}) // *args, **kw | |
print(result.data['application/json']) | |
})() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment