Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run shell (terminal) in Google Colab?

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?

like image 565
korakot Avatar asked Dec 13 '19 08:12

korakot


People also ask

Where is terminal in Colab?

If you subscribe to Colab Pro, terminal is now available. Just click the 'Terminal' icon on the left pane.

How do I run a shell command in Python?

If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the RunShellCommand() function. The first option is easier to run one line of code and then exit, but it isn't as flexible when using arguments or producing text output.

Can I SSH into Google Colab?

Start a SSH session in Colab with ngrok. Copy and paste it in your preferred terminal emulator, insert carbonara when a password is required and enjoy your SSH session! Of course, you can change the password with whatever you want!


1 Answers

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.

enter image description here

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.

Terminal Icon

like image 200
korakot Avatar answered Oct 11 '22 14:10

korakot