本文目的很簡單。就是 local VS Code 寫 Python or Jupyter, 但是 remote on Colab 利用其算力。 以下是 ChatGPT 的 answers.
Not working so far!! Google Colab disallow remote SSH or proxy to access colab, at least for free or low tier users.
Method B (Not working)
Google Colab recently disabled direct SSH access due to security and policy changes. However, you can still use VS Code as a front-end for Colab by using Colab’s code-server setup, which essentially runs a version of VS Code inside the Colab environment and allows access through a browser. Here’s how to set it up:
Steps to Use VS Code as a Front-End with Google Colab
- Start a Google Colab Notebook:
- Open a new Colab notebook and make sure your runtime is set to GPU (if you need GPU access).
- Install and Launch code-server in Colab:
-
In a Colab code cell, run the following commands to install code-server, a web-based version of VS Code:
1
2!curl -fsSL https://code-server.dev/install.sh | sh !mkdir -p ~/.config/code-server/ -
Next, set a password for accessing the code server:
1
2with open("~/.config/code-server/config.yaml", "w") as f: f.write("bind-addr: 0.0.0.0:8080\nauth: password\npassword: 'your_password'\n")Replace
'your_password'with a secure password you will use to access the code server.
-
- Start code-server:
-
Start the code-server with the following command:
1
!code-server --config ~/.config/code-server/config.yaml &
-
- Create a Public URL to Access code-server:
-
Colab allows you to use
ngrokorlocaltunnelto create a public URL for accessing code-server. Uselocaltunnelas it’s simpler to set up:1
2!npm install -g localtunnel !lt --port 8080 -
The output will display a URL like
https://<unique-subdomain>.loca.lt. This is your access point to the code-server.
-
- Access code-server in Your Browser:
- Open the URL from the
localtunneloutput in your browser. You should see the VS Code interface. - Enter the password you set in step 2 to log in.
- Open the URL from the
- Use VS Code in Your Browser:
- You can now use VS Code directly in your browser, running on Colab’s backend. This setup will give you access to Colab’s CPU/GPU/TPU resources while working with a familiar VS Code interface.
- Saving Your Work:
- Remember to use Google Drive or other cloud storage to save your work, as Colab instances are ephemeral.
This method provides a practical workaround to use VS Code with Google Colab’s resources without needing SSH.
Method A
To use Visual Studio Code (VS Code) as the front end while running Python code on Google Colab (using Colab’s resources), you can set up a remote connection to Colab from VS Code by forwarding Colab’s runtime. Here’s a step-by-step guide:
Prerequisites
- Make sure you have Python installed locally.
- Install the Remote - SSH extension in VS Code.
Steps
- Set Up SSH on Colab: (NOT working! Google Colab disallow SSH connection!)
-
Open your Colab notebook and run the following code to set up an SSH connection. Replace
your_passwordwith a strong password:1
2
3
4
5
6
7
8# Install colab_ssh !pip install colab_ssh --upgrade # Import colab_ssh from colab_ssh import launch_ssh, init_git # Enable SSH launch_ssh("your_email@example.com", "your_password") -
You’ll see output with connection details. For example:
1
2SSH: ssh -L 8888:localhost:8888 root@0.tcp.ngrok.io -pXXXXX Password: your_password
-
- Forward Colab’s SSH Port Locally:
-
On your local machine, open a terminal and enter the SSH command given by Colab. For example:
1
ssh -L 8888:localhost:8888 root@0.tcp.ngrok.io -pXXXXXEnter your password when prompted.
-
- Configure VS Code:
- In VS Code, open the Command Palette (Ctrl+Shift+P) and select Remote-SSH: Connect to Host.
- Enter the SSH command (
root@0.tcp.ngrok.io -pXXXXX) that Colab provided, and connect. - After connecting, VS Code will treat Colab as a remote server.
- Set Up Your Workspace:
- You can open a folder and start writing code in VS Code as if you were working locally. VS Code will send your code to Colab for execution, leveraging Colab’s resources (like GPU or TPU).
- Run Jupyter Notebooks (Optional):
- If you’re working with Jupyter notebooks, you can access them from your browser by going to
localhost:8888in your local machine, where the Jupyter server is forwarded.
- If you’re working with Jupyter notebooks, you can access them from your browser by going to
- Using Python Scripts:
- Create Python scripts or notebooks as you would normally in VS Code. When you execute them, they will run on Colab’s backend.
Important Tips
- Be mindful of idle timeouts on Colab. If you leave the connection idle for too long, Colab might terminate the session.
- File persistence: Files saved in the Colab environment will disappear when the session ends. Use Google Drive or other cloud storage if you need persistence.
This setup allows you to enjoy the rich interface of VS Code while leveraging Google Colab’s powerful backend.
Vs Code Python and Colab Jupyter Notebook have their own advantages.
Vs Code Python:
- very good debugging environment including breakpoint, watch function.
- use local GPU card to save money
Jupyter
- Python file compatible
- Use remote GPU with better performance
- Interactive debugging without recompile
Here’s a comparison table summarizing the features of VS Code and Jupyter for Python development:
| Feature | VS Code | Jupyter |
|---|---|---|
| Debugging Environment | Very good debugging with breakpoints and watch functions | Interactive debugging without recompilation |
| GPU Utilization | Uses local GPU card to save money | Uses remote GPU with better performance |
| File Compatibility | Supports Python files | Compatible with Python files |
| Interactivity | Less interactive, primarily code-based | Highly interactive with notebooks |
| File comparison | Easy for TEXT file, diff or BeyongCompare | JSON file, diffcult to compare |
How to Avoid Two Copy of Files?
Apparently we don’t really want to keep track two copies of files, VS Code python and Colab Jupyter Notebook (ipynb). However, there are two different file formats.
A good way is to make the python code to be function only for importing. The test code can be either using if __name__ == "__main__": code block, or use test folder. This is a standard procedure so that I won’t explain it.
On the other hand, the Colab Jupyter notebook needs some work.
Mount Local Folder
1 | |
Change Directory for Import
1 | |
Import related python and packages
The first line is the most important part!
1 | |
Command Line Argement
1 | |
Visualize transformer tensor shape and validate
A very good tools is summary
1 | |