Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for comms messages #6

Closed
korakot opened this issue Dec 19, 2017 · 10 comments
Closed

Add support for comms messages #6

korakot opened this issue Dec 19, 2017 · 10 comments

Comments

@korakot
Copy link

korakot commented Dec 19, 2017

Some tasks need interactions between JS frontend and python backend.
Please add support for IPython.notebook.kernel.execute(python_code)

Also, some modules like ipyleaflet, bqplot, and other Jupyter Widgets don't work without them.

@blois
Copy link
Contributor

blois commented Dec 27, 2017

There are a couple of separate issues here-

IPython.notebook.kernel.execute: for security concerns, Colab does not allow execution of aritrary python code from Javascript. Rather, Javascript will be able to invoke specific Python callbacks. The callback must be registered from Python using the google.colab.output.register_callback method: https://github.com/googlecolab/colabtools/blob/master/google/colab/output/_js.py#L45. This has not yet rolled out into production:

import google.colab.output

def DoSomething(a, b, c=0):
  return "a=%d b=%d c=%d" % (a, b, c)

google.colab.output.register_callback('DoSomething', DoSomething)

Once the python callback is registered, Javascript from an output which has executed within the current session can call google.colab.kernel.invokeFunction:

const result = await google.colab.kernel.invokeFunction('DoSomething', [1, 2], {c: 3})

Comms: currently as you noted Comms are not supported in Colab. One important distinction is that in Colab, each cell's output is in it's own iframe and all user-controlled code is executed in those iframes. We could broadcast these messages to each iframe, but in the case of something like Jupyter Widgets, it's not typically expecting multiple instances of the widget manager running on the page.

I'd still like to see what level of trickery we could add to get some level of compatibility here, but I'm pretty sure it's going to involve a good deal more work than just plumbing through the comms messages.

@korakot
Copy link
Author

korakot commented Dec 27, 2017

I’m satisfied with google.colab.kernel.invokeFunction

I only need a way to interact with output cell and make change to python variables. This suffices for my need. Thanks.

So, it will roll out to production sometime soon, right? At that time, please make a simple example, e.g. click a button to do something, or allow drawing on canvas and send the image to python.

Really appriciate Colab.

@blois
Copy link
Contributor

blois commented Dec 27, 2017

Yes, it should roll out shortly after the holidays.

FWIW, the frontend code is already available and you can patch in the Python code via:

from google.colab.output import _js as __js_239
import json

functions = {}

def register_callback(name, function):
  functions[name] = function

def invoke_function(name, args_json, kwargs_json):
  args = json.loads(args_json)
  kwargs = json.loads(kwargs_json)
  return functions[name](*args, **kwargs)
 
__js_239.register_callback = register_callback
__js_239._invoke_function = invoke_function

@blois
Copy link
Contributor

blois commented Jan 4, 2018

google.colab.kernel.invokeFunction is now available, a notebook showing how to use it (and a bit more):
https://colab.research.google.com/notebook#fileId=1sYg2maTOT3GrCGS7R7WSxJcM3RsHjHAH

@blois blois closed this as completed Jan 4, 2018
@korakot
Copy link
Author

korakot commented Jan 5, 2018

The last example "Persisting Updates" does not work with Python 3.

BTW, how do I change the output of other cells? The last example show how to use output.redirect_to_element to change an element within the current cell. But I may want to call many python codes from different cells but want all the output to redirect to the first output cell. Maybe something like a dashboard where you try different python commands, but on the same output.

@blois
Copy link
Contributor

blois commented Jan 5, 2018

I opened #18 for the Python 3 issue in the last cell.

Also opened #19 to track allowing communication between outputframes. The technique with this API would be to send a message from one output to another output then invokeFunction from the output which is to be modified by the kernel.

@oeway
Copy link

oeway commented Oct 18, 2019

@blois
Would it be possible to support invoking functions defined in JS from Python? I know that we can do eval_js to get around, however, I think for real-time communication (as supported by comms message) there is a big overhead.

I think it would be great if the same API can be supported the other way around. What do you think?

@blois
Copy link
Contributor

blois commented Oct 18, 2019

@oeway
eval_js has a second argument- ignore_result that when set to true will not block the Python code waiting for the result. Otherwise there should not be significant overhead between eval_js and comms messages- they are all JSON messages delivered over the same IPython iopub channel.

@oeway
Copy link

oeway commented Oct 18, 2019 via email

@blois
Copy link
Contributor

blois commented Oct 18, 2019

IPython iopub does support it but Colab does not at this time- #587.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants