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

42
我知道我可以调用!ls来向shell发出ls命令。
但我想要像历史记录或标签补全这样的功能。
在Google Colab中是否可能实现?

为什么 !<cmd> 对你来说不够用? - Charlie Parker
1
我想要像历史记录或选项补全这样的功能。 - korakot
5个回答

61

您可以使用jQuery终端模拟器,支持google.colab.kernel.invokeFunction

这是一个示例笔记本

关键部分在于在shell函数中支持它。

def shell(command):
  return JSON([getoutput(command)])
output.register_callback('shell', shell)

以下是如何使用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));
}

这是一个截图。

在此输入图片描述

更新(7/2020)

我已将@Anant的答案添加到我的库中,现在你可以轻松地使用以下命令运行控制台:

!pip install kora
from kora import console
console.start()  # and click link

更新(2020年12月)

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

终端图标


@gorjan 我已经添加了一张截图。 - korakot
1
可能并不是每个人都可以使用,例如我就没有。 - gorjan
2
我相当确定在2020年12月20日之后,Colab的终端仅在付费的美国专业版中提供。我正在使用免费版,并没有看到终端窗口。 - Kevin Mullet
3
Teleconsole(kora使用的工具)已停止更新。 - Ilya V. Schurov
Kora路由不起作用。 - Calcutta
显示剩余2条评论

33

2
太好了,谢谢! - Dave Flynn
这个东西太疯狂了,谢谢!! - Hanna
太棒了!这是在实例上运行Linux的最佳选择!如果您使用此选项,请给@popcorny的repo点亮星星,以便他们获得这项工作的荣誉:https://github.com/InfuseAI/colab-xterm - mikey
1
不再工作了吗?终端被无尽的404错误填满了...太糟糕了,它只能正常运行约15秒。 - phdscm

19

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

!bash

它以密码格式显示命令。如果你使用浏览器开发工具将输入类型从password更改为text,会非常有帮助。


16
这会以密码格式显示我的命令,即字符会被隐藏。 - amzon-ex
使用浏览器的开发工具,将输入框的类型从“密码”更改为“文本”。 - 0xB00B

11

Google Colab Shell

免费终端,供所有人使用

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

安装设置

pip install google-colab-shell

Usage

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

## Anytime you want to open a terminal

getshell()

getshell(height=400) # custom height of the terminal

重要提示:确保 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)

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

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


10

建议尝试以下步骤 -

  1. 在Colab上安装Teleconsole, 这是一个可以通过互联网使用终端的软件包 -

 !curl https://www.teleconsole.com/get.sh | sh
在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()) 
你应该会得到类似以下的输出 -
 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
  • 要么通过访问终端的链接打开Web界面,要么打开本地shell终端并使用命令安装Teleconsole -
  •  curl https://www.teleconsole.com/get.sh | sh
    
    请使用以下代码,使用第2步中获得的Teleconsole Id加入终端。
     teleconsole join <Teleconsole ID>
    

    使用一些额外的步骤,也可以通过ssh隧道来实现此方法。


    应该将 "process.stdout.readline().decode()" 更改为 "p.stdout.readline().decode()"。 - cannin
    2
    自2021年4月9日起,Teleconsole已关闭。 - Abishek Bashyal

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接