Created
August 6, 2018 04:38
-
-
Save korakot/d10a43490f3da17d4915cdc1f200b180 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