小编Ser*_*ika的帖子

以编程方式在 Google Colab 中运行“运行所有单元格”命令

我需要以编程方式从 Google Colab 菜单“运行时”运行某些命令“运行全部”。它没有任何明显的“onclick”事件处理程序,我可以从该页面上的 JavaScript 代码调用它。页面上的其他“div”可以从js调用,例如,我可以使用js代码连接到运行时:

document.querySelector('#top-toolbar > colab-connect-button').shadowRoot.querySelector('#connect').click();
Run Code Online (Sandbox Code Playgroud)

运行时菜单是一个下拉菜单,我尝试了其中的.click()每个项目,但没有效果。<div>

此外,“运行全部”命令有一个热键Ctrl + F9,但向document元素分派事件没有效果。但我可以Enter使用以下代码将命令发送到笔记本内的任何输入字段:

document.querySelector('input.raw_input').dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}))
Run Code Online (Sandbox Code Playgroud)

使用 Chrome 代码检查器,Ctrl + Shift + I我查看了“Run all”命令,它看起来像:

<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>
Run Code Online (Sandbox Code Playgroud)

因此,我在页面上检查器代码的“源”选项卡内进行搜索,并在https://colab.research.google.com/v2/external/external_polymer_binary.js文件中发现“runall”的出现:

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

F9顺便说一句,120 - 是按钮的键码。我还发现我认为所需菜单项的确切位置被称为:

        case "runall":
            d.runAll();
            break;
Run Code Online (Sandbox Code Playgroud)

但我几乎不可能理解它是什么d.以及它的参考在哪里!

我还发现了许多其他有趣且有用的命令,例如this.notebook.getKernel().isRunning()or …

javascript python frontend dispatchevent google-colaboratory

6
推荐指数
1
解决办法
3955
查看次数