Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit and save text files (.py) in Google Colab?

I cloned a github repo using !git clone https://github.com/llSourcell/Pokemon_GAN.git. I wanted to modify a .py file inside Colab. So i used %load filename.py as suggested here (How to load/edit/run/save text files (.py) into an IPython notebook cell?). But whenever i run this command, i get disconnected after some time. I was wondering if there is some other way to edit .py file without undergoing the hassle of downloading it to pc,editing and then re uploading it. Thanks in advance.

like image 925
randomName Avatar asked Feb 08 '18 13:02

randomName


People also ask

How do I edit a .PY file in Google Colab?

In the files section, there is an option to upload files or you can double click on the file, make changes and ctrl+s to save those changes.

How do I save code in Colab?

Path to save in drive can be found easily by clicking files on left side, navigating to folder and right click to choose Copy path . This will save the image to folder named Delete in Google Drive.


2 Answers

You can use Ipython magic commands. Use below command

%pycat code.py

A pop up will appear displaying the code. You can copy it and edit it locally.
Remove the file using below command

!rm code.py

Copy the edited code to a cell in notebook and add below command at the top of the cell

%%writefile code.py

Run the cell. A file will be created with the contents present in the cell.

Updates: Now there are lot more easy and convenient options.

  1. In the files section, there is an option to upload files or you can double click on the file, make changes and ctrl+s to save those changes.
  2. You can also use https://github.com/abhishekkrthakur/colabcode to edit using visual studio code server.
like image 81
rahul Avatar answered Sep 28 '22 20:09

rahul


Colab includes a text editor you can use to create, open, and delete .py files directly.

All is done in the Files view (see below).

  • To create or delete a file, right click and choose "New file" or "Delete file".
  • To edit a file, double click on it. It appears on the right portion of your screen. Make any changes you wish. Changes are saved automatically.

enter image description here

like image 24
Bob Smith Avatar answered Sep 28 '22 20:09

Bob Smith