paint-brush
How To use Google Colab with VS Codeby@davisdavid
29,019 reads
29,019 reads

How To use Google Colab with VS Code

by Davis DavidJuly 2nd, 2021
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Google Colab and VS Code are the most popular editor tools used by many Python developers and Researchers to develop tech solutions or systems (especially in Machine Learning & Data Science) In this article, you will learn how to install the colabcode python package. How to start the VS Code (code server) How to run a simple python file that trains a machine-learning algorithm to classify iris flowers into three species (setosa, versicolor, or virginica) and then make a prediction.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How To use Google Colab with VS Code
Davis David HackerNoon profile picture

Google Colab and VS Code are the most popular editor tools used by many Python developers and Researchers to develop different tech solutions or systems (especially in Machine Learning & Data Science).

Many Python developers and Data scientists know how to use Google Colab, but do you know that you can set up a VS Code on Google Colab and use it as an editor the same way as in your local machine?

In this article, you will learn

  1. How to install the colabcode python package.
  2. How to start the VS Code (code server).
  3. How to access the online VS Code.
  4. Open the terminal.
  5. Run a python file.

Steps on How to Use Google Colab with VS Code

1.Open Colab Notebook

The first step is to launch a new colab notebook in your Google Colab and you can rename the file as you want.

For example,  run_vscode.ipynb

2.Install colabcode python package.

To use Google Colab with VS Code (code server), you need to install the colabcode python package. This is an awesome open-source python package developed by Abhishek Thakur.

To install the package, run the following command in your notebook cell.

 !pip install colabcode

3.Import ColabCode

The next step is to import ColabCode class from the package.

from colabcode import ColabCode

4.Create an instance of ColabCode

 After importing the ColabCode, you need to create an instance of ColabCode and set the following arguments.

  •  port  - The port you want to run code-server on. For example port=10000
  •  password  - You can set a password to protect your code-server from unauthorized access. This is an optional argument.
  •  mount_drive  - If you want to use your Google drive. This is a Boolean argument means you can set True or False. This is an optional argument.
ColabCode(port=10000)

5.Start the Code Server

After running the ColabCode instance, it will start the server and show the link to access the code server.

You need to click the link and it will open in a new tab.

Now you can take advantage of a full-fledged code editor and run different experiments on the Colab VM. 

Note: If you check on your Colab Notebook, you will observe the cell that runs the ColabCode instance is continuously running. Don't close your Colab notebook unless you want to close the code server that runs VS Code.

Tips to use VS Code on Google Colab

After launching the code server, the following tips will help you start using VS Code on Google Colab.

1.Open Terminal

To open the terminal on VS Code that runs on Google Colab, use the following shortcut command 

Ctrl + Shift + `

2.Change Theme

You can change the theme of the editor by clicking the setting icon (bottom-left corner) and then click "Color Theme". It will open a popup window with different theme options you can select.

3.Run Python File

You can create a python file by clicking the "File" section on the sidebar and then select a "New File" tab.

In the following example, you will see how to run a simple python file that trains a machine-learning algorithm to classify iris flowers into three species (setosa, versicolor, or virginica) and then make a prediction.

Final Thoughts on How to use Google Colab with VS Code

Congratulations 👏👏, you have made it to the end of this article! I hope you have learned something new. You can set up VS Code on Google Colab and take your coding to the next level.

You can also use the colabcode python package on the Kaggle platform to run VS Code. You just need to follow the same steps mentioned above.

If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!

You can also find me on Twitter @Davis_McDavid.

And you can read more articles like this here.

Want to keep up to date with all the latest in python? Subscribe to our newsletter in the footer below!