Code Monkey home page Code Monkey logo

Comments (10)

blois avatar blois commented on May 17, 2024 7

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.

from colabtools.

blois avatar blois commented on May 17, 2024 4

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.

from colabtools.

korakot avatar korakot commented on May 17, 2024 1

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.

from colabtools.

blois avatar blois commented on May 17, 2024 1

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

from colabtools.

blois avatar blois commented on May 17, 2024

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

from colabtools.

korakot avatar korakot commented on May 17, 2024

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.

from colabtools.

oeway avatar oeway commented on May 17, 2024

@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?

from colabtools.

blois avatar blois commented on May 17, 2024

@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.

from colabtools.

oeway avatar oeway commented on May 17, 2024

from colabtools.

blois avatar blois commented on May 17, 2024

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

from colabtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.