Reputation: 40888
I know that I can call !ls
to issue ls
command to shell.
But I want features like history or tab-completion.
Is it possible to do so in Google Colab?
Upvotes: 47
Views: 144097
Reputation: 11
ive just beautified the code of korakot, this version updates the working directory too, when you navigate
from IPython.display import JSON
from google.colab import output
from subprocess import getoutput
import os
def shell(command):
if command.startswith('cd'):
path = command.strip().split(maxsplit=1)[1]
os.chdir(path)
return JSON([''])
return JSON([getoutput(command)])
output.register_callback('shell', shell)
next cell
#@title Colab Shell
%%html
<div id="term_demo"></div>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.min.css" rel="stylesheet"/>
<style>
#term_demo {
font-size: 20px; /* Adjust the font size here */
}
</style>
<script>
let currentPath = '{current_path}';
$('#term_demo').terminal(async function(command) {
if (command !== '') {
try {
let res = await google.colab.kernel.invokeFunction('shell', [command]);
let out = res.data['application/json'][0];
if (command.startsWith('cd ')) {
// Update the current path on 'cd' commands
currentPath = await google.colab.kernel.invokeFunction('shell', ['pwd']);
currentPath = currentPath.data['application/json'][0].trim();
}
this.set_prompt(currentPath + '> ');
this.echo(new String(out));
} catch(e) {
this.error(new String(e));
}
} else {
this.echo('');
}
}, {
greetings: 'Welcome to Colab Shell',
name: 'colab_demo',
height: 300,
prompt: currentPath + '> ',
onClear: function() {
this.set_prompt(currentPath + '> ');
}
}).css("font-size", "20px"); // Set font size here;
</script>
ss: enter image description here
Upvotes: 1
Reputation: 9857
The most complete and easy shell:
!pip install git+https://github.com/0wwafa/shell &>/dev/null && echo OK || echo Fail.
from shell import shell
shell(True)
copy the code above in a cell. That's all.
Upvotes: -3
Reputation: 37
!sh
should work, it doesn't takes commands in asterisk form as mentioned by some people here. Currently working fine as of December 7, 2023
Upvotes: -1
Reputation: 1936
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
If you feel it is useful, please star this project. ⭐️
Upvotes: 48
Reputation: 3856
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
IMPORTANT: Make sure getshell
is the last command in the cell.
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)
Please Contribute: https://github.com/singhsidhukuldeep/Google-Colab-Shell#todo-want-to-contribute 🙌
Upvotes: 11
Reputation: 840
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.
Upvotes: 19
Reputation: 40888
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.
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
If you subscribe to Colab Pro, terminal is now available. Just click the 'Terminal' icon on the left pane.
Upvotes: 62
Reputation: 424
Better try this -
!curl https://www.teleconsole.com/get.sh | sh
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())
You should get output something like -
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
curl https://www.teleconsole.com/get.sh | sh
Then use the below code to join the terminal using Teleconsole Id you got in step No. 2-
teleconsole join <Teleconsole ID>
This method can also be tunneled through ssh with some additional steps.
Upvotes: 10