6

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

1 Answers1

1

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()
paxton4416
  • 495
  • 3
  • 7
  • I also tried things like colab.notebook.restart() etc in inspector mode of Chrome but got no effect. But I didnt tried colab.global... Thank you! And I came to document.dispatchEvent(new KeyboardEvent('keydown', {ctrlKey:true, bubbles:true, keyCode:120})); also )) – Sergeika Uarabeika May 19 '21 at 19:41
  • Would be nice to know where to put this code. – ctrl-alt-delor Jul 11 '21 at 08:26
  • @ctrl-alt-delor where you put it depends on your application, but it has to be executed from the notebook frontend (in the browser) rather than the kernel (on the Colab VM). Unlike Jupyter, Colab heavily restricts interaction between the two for "security reasons." I've had success running this manually from the Chrome/Firefox console and also via selenium. – paxton4416 Jul 14 '21 at 18:06
  • Has something changed on Colabs? I cannot get this method of run all to work: https://colab.research.google.com/drive/1DcmicPxB8LNwpoWoIM6DdAkdNbS5tX6s?usp=sharing – WASasquatch Aug 13 '22 at 19:22
  • Yeah, it seems to be because it's using iframes, you have to target the parent to run Ctrl + F9, and thus, you get blocked by cross site origin. – WASasquatch Aug 13 '22 at 19:48