【问题标题】:How can I run shell (terminal) in Google Colab?如何在 Google Colab 中运行 shell(终端)?
【发布时间】:2020-04-06 16:15:37
【问题描述】:

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

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

在 Google Colab 中可以这样做吗?

【问题讨论】:

  • 为什么!<cmd> 对你来说不够用?
  • 我想要历史记录或制表符补全等功能

标签: shell command-line terminal google-colaboratory


【解决方案1】:

您可以使用带有 google.colab.kernel.invokeFunction 支持的 jQuery 终端仿真器

这是example notebook

关键部分在这里,你用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));
}

这是截图。

更新(2020 年 7 月)

我已将@Anant 的答案添加到我的库中。现在您只需

即可轻松运行控制台
!pip install kora
from kora import console
console.start()  # and click link

更新(2020 年 12 月)

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

【讨论】:

  • 能否发一张终端图标所在位置的截图?另外,这是否仅适用于 Colab Pro 用户?因为找不到……
  • @gorjan 我已经添加了截图。
  • 可能还不是所有人都可以使用,比如我没有
  • 我很确定,截至 2020 年 12 月 20 日,终端仅在 Colab 的付费、美国专业级别上可用。我正在使用免费级别并且没有看到终端窗口。 ![](i.imgur.com/E7tghNE.png)
  • Teleconsole(kora 使用的)已停产。
【解决方案2】:

最好试试这个 -

  1. 在 Colab 上安装 Teleconsole,它是一个通过 Internet 使用终端的软件包 -
     !curl https://www.teleconsole.com/get.sh | sh
    
  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()) 
    
    你应该得到类似的输出 -
     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
    
  3. 要么通过访问终端的链接打开 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()
  • Teleconsole 自 2021 年 4 月 9 日起关闭。
【解决方案3】:

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

!bash

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

【讨论】:

  • 这会以密码格式显示我的命令,即隐藏字符。
  • 使用浏览器开发工具将输入框类型从“密码”更改为“文本”
【解决方案4】:

Google Colab 外壳

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

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

设置

pip install google-colab-shell

用法

# 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#todo-want-to-contribute?

【讨论】:

    【解决方案5】:

    我刚刚写了一个在 colab 中运行 xterm 的工具。

    如果您觉得它有用,请给这个项目加注星标。 ⭐️

    截图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 2021-12-29
      • 2017-10-28
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多