-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Currently there is basic colab support, but it is a bit of a hack. Basically the problem is that while Colab does support Jupyter notebooks, there is one bit of behavior that differs from "native" jupyter notebooks/lab: updating widget state from a background thread simply does not work; the updates do not get broadcast to the client widget at all. This is easy to reproduce with any widget being updated in a thread.
The current workaound is as follows:
- Use the
google.colab.output.register_callbackfunction in the kernel to register a callback for the client side calledlmk.widget.sync. This callback just calls thesend_state()method on the widget itself, which manually flushes the state of the widget to the client side. - On the client side, use a
setInterval()invoke that callback viagoogle.colab.kernel.invokeFunctionevery 2 seconds.
(I learned about this colab-specific functionality in the advanced_outputs.ipynb notebook provided by Google).
This works fine, but also isn't the ideal solution. These are the known limitations:
- Calling the
lmk.widget.synccallback causes the cell where the widget is displayed to show it is being executed whenever called. This is a bit annoying, and can be visually distracting. - After closing the tab and re-opening it, the client widget loses the permission to execute callbacks on the server side. This means that in order for the widget to become interactive again, the cell that renders the widget must be re-executed after re-opening the tab. Aside from being an annoying chore, obviously if the notebook is running something else, this is not possible until it's finished.
- Since the UI updates happen on a 2 second interval, the widget is not as responsive as it should be.
- Runtimes other than colab-hosted runtimes don't seem to work at all. It's not even clear if it's possible to use custom widgets with runtimes other than the colab-hosted one since enabling them requires the
google.colabpackage, which comes installed on colab-hosted runtimes but doesn't seem to be a package that's actively maintained on PyPI or versioned. It seems like this repo is the source code for the package, but installing from the repo URL didn't work when I tried it. I might be able to just copy the code that installs the custom widget manager though: https://github.com/googlecolab/colabtools/blob/main/google/colab/output/_widgets.py . I tried copying that code into a notebook and executing it directly using a local runtime, but then ran into the issue that as described about, the colab support for LMK depends ongoogle.colab.outputs.register_callback, not just the custom widget manager. It's not even clear to me how that functionality works--it seems like something calls the_invoke_callbackfunction here, but it's not referenced anywhere else in the package so it might just be some custom code within the colab ipython kernel that invokes it. Not clear if there's any way to hook into that.
This issue is here for people to indicate their support for better Colab support. If you ran into an issue using lmk with Colab please leave a comment describing what wrong, or leave a 👍 if you just want to see better Colab support.