Search code examples
javascriptpythonfrontendgoogle-colaboratorydispatchevent

"Run all cells" command in Google Colab programmatically


I need to run certain command "Run all" from Google Colab menu "Runtime" programmatically. It does not have any obvious "onclick" eventHandler which I could call from javascript code on that page. Other "divs" on the page are OK to be called from js, for exapmle, I can connect to runtime using js code:

document.querySelector('#top-toolbar > colab-connect-button').shadowRoot.querySelector('#connect').click();

Runtime menu is a dropdown menu and I tried to .click() every <div> item inside it but no effect.

Also "Run all" command has a hotkey Ctrl + F9 but dispatching event to the document element has no effect. But I can send Enter command to any input field inside the notebook with this code:

document.querySelector('input.raw_input').dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}))

Using Chrome code inspector Ctrl + Shift + I I looked inside "Run all" command and it looks like:

<div command="runall" class="goog-menuitem" role="menuitem" id=":1w" style="user-select: none;"><div class="goog-menuitem-content" style="user-select: none;">Run all<span class="goog-menuitem-accel">Ctrl+F9</span></div></div>

So I searched inside Sources tab of inspector code on the page and found occurrences of "runall" in https://colab.research.google.com/v2/external/external_polymer_binary.js file:

, Eja = X(new W({
        id: "runall",
        description: "Run all cells in notebook",
        shortcut: IG(120)

120 - is a keycode of F9 button by the way. Also I found I think exact place where needed menu item is called:

        case "runall":
            d.runAll();
            break;

but it's almost impossible for me to understand what is d. and where its reference!

Also I found many other interesting and useful commands like this.notebook.getKernel().isRunning() or c.notebook.getKernel().restart() but the question is the same all the time: what is the root object for those commands? I tried document. and window. but the result is "undefined" or "is not a function". I think that I could call runall() command in a string like:

document.**SOMETHING I DONT KNOW**.runAll()

I am very bad with frontend/js and its very difficult to find something in obfuscated code but if we have such function as .runAll() in javascript code which is connected to required menu item I thick it is possible to run it programmatically from console or javascript injection

Or maybe it is possible to dispatch a keyboard event Ctrl + F9 to some element in order to run this command thus the question is WHAT is the required object to dispatch the keyboard event


Solution

  • I spent a while combing through that javascript file for a similar reason, and finally figured out how to make this work.

    Here's a function to programmatically run all cells:

    function runAll() {
      const F9Event = {key: "F9", code: "F9", metaKey: true, keyCode: 120};
      document.dispatchEvent(new KeyboardEvent("keydown", F9Event));
    }
    

    Note that KeyboardEvent.keyCode is deprecated in favor of KeyboardEvent.code, but you still need to provide it here (as of 5/18/21) since it's the property Colab uses to check keyboard inputs.

    You can also use metaKey: true and ctrlKey: true interchangeably, regardless of platform, since Colab just checks whether either KeyboardEvent.metaKey or KeyboardEvent.ctrlKey is present for shortcuts that require them.

    Also I found many other interesting and useful commands like this.notebook.getKernel().isRunning() or c.notebook.getKernel().restart() but the question is the same all the time: what is the root object for those commands?

    There's a global colab object that provides access to some (but not all) functionality. Most things are accessible through colab.global, e.g. to restart the kernel, you can use:

    colab.global.notebook.kernel.restart()