How can I run shell (terminal) in Google Colab?
Better try this -
- Install Teleconsole on Colab, its a package to use terminal over internet -
!curl https://www.teleconsole.com/get.sh | sh
- Run below code on colab notebook to use Teleconsole -
You should get output something like -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
- Either open the web interface by following the link for accessing the terminal or open a local shell terminal and install Teleconsole using the command -
Then use the below code to join the terminal using Teleconsole Id you got in step No. 2-curl https://www.teleconsole.com/get.sh | sh
teleconsole join <Teleconsole ID>
This method can also be tunneled through ssh with some additional steps.
I just wrote a tool to run xterm in colab. Just three lines of code and you can get an interactive terminal.
!pip install colab-xterm
%load_ext colabxterm
%xterm
- Notebook:
- Github: https://github.com/InfuseAI/colab-xterm
- Blog post: https://blog.infuseai.io/run-a-full-tty-terminal-in-google-colab-without-colab-pro-2759b9f8a74a
If you feel it is useful, please star this project. ⭐️
Screenshot
Just type the following. It will spawn a bash session.
!bash
It displays commands in a password format. If you change the input type from password
to text
using browser dev tools, it will be really helpful.
You can use jQuery Terminal Emulator backed with google.colab.kernel.invokeFunction
Here's an example notebook.
The key part is here, where you back it with shell function.
def shell(command):
return JSON([getoutput(command)])
output.register_callback('shell', shell)
And here's how you use 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));
}
Here's a screenshot.
Update (7/2020)
I have taken @Anant's answer and add it into my library. Now you can run console easily with just
!pip install kora
from kora import console
console.start() # and click link
Update (12/2020)
If you subscribe to Colab Pro, terminal is now available. Just click the 'Terminal' icon on the left pane.