如何在 Google Colab 中运行 shell(终端)?

Kor*_*ich 23 shell terminal command-line google-colaboratory

我知道我可以调用向 shell!ls发出ls命令。

但我想要历史记录或制表符完成等功能。

在 Google Colab 中可以这样做吗?

pop*_*rny 39

我刚刚写了一个在colab中运行xterm的工具。只需三行代码,您就可以获得一个交互式终端。

\n
!pip install colab-xterm\n%load_ext colabxterm\n%xterm\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您觉得有用,请为该项目star。\xe2\xad\x90\xef\xb8\x8f

\n

截屏

\n

在此输入图像描述

\n

  • 这太棒了,谢谢! (2认同)

Kor*_*ich 37

您可以使用支持的 jQuery Terminal Emulator google.colab.kernel.invokeFunction

这是一个示例笔记本

关键部分在这里,你用 shell 函数支持它。

def shell(command):
  return JSON([getoutput(command)])
output.register_callback('shell', shell)
Run Code Online (Sandbox Code Playgroud)

这是你如何使用invokeFunction

try {
    let res = await google.colab.kernel.invokeFunction('shell', [command])
    let out = res.data['application/json'][0]
    this.echo(new String(out))
} catch(e) {
    this.error(new String(e));
}
Run Code Online (Sandbox Code Playgroud)

这是一个屏幕截图。

在此处输入图片说明

更新 (7/2020)

我已经接受了@Anant 的回答并将其添加到我的图书馆中。现在您可以轻松地运行控制台

!pip install kora
from kora import console
console.start()  # and click link
Run Code Online (Sandbox Code Playgroud)

更新 (12/2020)

如果您订阅了 Colab Pro,则终端现在可用。只需单击左侧窗格中的“终端”图标。

终端图标

  • Teleconsole(kora 使用的)已停止使用。 (3认同)
  • 我相当确定,截至 2020 年 12 月 20 日,终端仅在付费、仅限美国的 Colab 专业级别上可用。我正在使用免费级别,但没有看到终端窗口。![](https://i.imgur.com/E7tghNE.png) (2认同)

Raj*_*aju 12

只需键入以下内容。它将产生一个 bash 会话。

!bash
Run Code Online (Sandbox Code Playgroud)

  • 这以密码格式显示我的命令,即隐藏字符。 (10认同)

Kul*_*dhu 11

谷歌 Colab 外壳

适合所有人的免费终端
显示 Google Colab 的终端

https://github.com/singhsidhukuldeep/Google-Colab-Shell

设置

pip install google-colab-shell
Run Code Online (Sandbox Code Playgroud)

用法

https://colab.research.google.com/github/singhsidhukuldeep/Google-Colab-Shell/blob/master/Google_Colab_Shell.ipynb

# import the module once
from google_colab_shell import getshell
Run Code Online (Sandbox Code Playgroud)
## Anytime you want to open a terminal

getshell()

getshell(height=400) # custom height of the terminal
Run Code Online (Sandbox Code Playgroud)

重要提示: 确保getshell这是单元格中的最后一个命令。

文档

Displays a terminal for Google Colab. <3 Google

Make sure this is the last command in the cell.

    Parameters
    ----------
    height : int, default 400
        height of the rendered terminal

    Returns
    -------
    IPython.display.HTML
        Displays the terminal

    Examples
    --------
    >>> getshell()

    >>> getshell(height=400)
Run Code Online (Sandbox Code Playgroud)

https://github.com/singhsidhukuldeep/Google-Colab-Shell

请贡献:https://github.com/singhsidhukuldeep/Google-Colab-Shell#todo-want-to-contribute


Ana*_*ant 10

最好试试这个——

  1. 在 Colab 上安装 Teleconsole,它是一个通过互联网使用终端的包 -
     !curl https://www.teleconsole.com/get.sh | sh
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在 colab 笔记本上运行以下代码以使用 Teleconsole -
     import subprocess as sp
     process = sp.Popen("teleconsole",shell=True,stdin=sp.PIPE,stdout=sp.PIPE,stderr=sp.PIPE)
     for i in range(6):
       print(process.stdout.readline().decode()) 
    
    Run Code Online (Sandbox Code Playgroud) 你应该得到类似的输出 -
     Starting local SSH server on localhost...
     Requesting a disposable SSH proxy on eu.teleconsole.com for root...
     Checking status of the SSH tunnel...
    
     Your Teleconsole ID: eu88d75d24084905shgdjhjhfgd1934e55c3786438a3
    
     WebUI for this session: 
     https://eu.teleconsole.com/s/88d75d24084905shgdjhjhfgd1934e55c3786438a3
    
    Run Code Online (Sandbox Code Playgroud)
  3. 按照访问终端的链接打开 Web 界面,或打开本地 shell 终端并使用命令安装 Teleconsole -
     curl https://www.teleconsole.com/get.sh | sh
    
    Run Code Online (Sandbox Code Playgroud) 然后使用以下代码使用您在第 2 步中获得的 Teleconsole Id 加入终端 -
     teleconsole join <Teleconsole ID>
    
    Run Code Online (Sandbox Code Playgroud)

这种方法也可以通过一些额外的步骤通过 ssh 进行隧道传输。

  • Teleconsole 自 2021 年 4 月 9 日起关闭。 (2认同)