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

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.

In the early days of Colab you could have used 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.
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.
You can also use https://github.com/abhishekkrthakur/colabcode to edit using visual studio code server.

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.

Unfortunately, it seems, colab do not support %load line magic (yet), and yet, you can see the file content using !cat your_file.py and then manually, copy the output contents, write them to a new cell and write %%writefile your_new_file_name.py at the top of the new cell to save this back to the instance. Note that, this will not be saved to your google drive yet.
Example:
!ls
output: colabData/
%%writefile something.py
print("everything's fine.")
!ls
output: colabData/ something.py
%run something.py
output: everything's fine.

you can edit it like this:
click on the triple bar (≡ on the left side of your windows)
click on files (the folder icon on the left)
click on Mount Drive and mount your drive
find your .py file and double click on it
edit it
press ctrl+s to save
edit:
these steps were after cloning your code into your drive
you should first mount your drive and clone your repo into your drive

Not a perfect solution but can be useful for someone.
You can use
!cat file_name.py to access file_name.py contents, copy the contents in the next cell and now you can run it or edit it.

I found it easier to edit the file locally.
You can download it from the left panel.
Right click on any file and download it.
Next, edit the file.
Next, upload the file.
use mv to move the file to the right location.

Solution:
p = """
Yadda yadda
whatever you want just don't use triple quotes.
"""
c = """text_file = open("text.text", "w+");text_file.write(p);text_file.close()"""
exec(c)

Easiest solution
just double click on the file you want to edit.The file opens and edit the file,save it and that's that.You are done

There is an app called Python Compiler Editor that you can connect to your Google Drive account, edit files and save them back.

The easiest way is:
1- Go to where you want the file to be with:
%cd WhereYouWantItToBe
2- Then write using:
%%writefile NameOfFile.txt
Hey there here is the start of the text
and also here
here is the end
3- Now run this cell and the file is going to be saved where you decided in step one.

While I don't have a way of editing in the notebook, I will share my pipeline.
Quite obvious really:
fork the repo or create a new one(for a new project)
create a branch just for uploading
make changes and push
evaluate
make changes
Hope that helps.

With the addition of the terminal (the icon is in the lower left corner), now we can edit files through vim.

You can open the file using the file explorer programatically, like this:
from google.colab import files
files.view('your_file.py')
It will open your file in a separate panel and you can then edit and save it there directly.

Related

How to export colab notebooks to pdf file

this is my first time asking a question on this forum, so, any tip or suggestion is highly appreciated!
As for the question itself, I have already seen many discussions on how to export a Colab notebook as a pdf, however I would like to ask more specifically if there is any way of doing it that can preserve the output of executed code (e.g.: I would like tables made from dataframe in pandas to be exported as they were printed on the notebook and not like a bunch of strings).
I think the easier method is you can use browser print functionality.
for most browser it's shortcut should be ctrl + p
and the the harder method is that you can download ipynb file to your machine and then use jupyter notebook to do this
for this to work you should install notebook-as-pdf pip package and then you need to use this command in your command-line or terminal
pyppeteer-install
after that you are all set, so now you can open your ipynb with jupyter notebook and you should find "PDF via HTML(pdf)" option in "download as" section of file menu
in other word it should be here:
file > download as > PDF via HTML(pdf)
if you want more details on this use this and this.

Is there a way to visualize a Jupyter Notebook without executing it?

I want to view the jupyter notebook in without having to execute, I mean no to open it and iteratively execute commands, just to see it as if it were a pdf or something like that.
I ask this because every time a want to see some code in the notebook I have to go to the specific directory and run the comand jupyter notebook and wait until it starts to be able to open the proper file.
If you are using Macos, give this a shot
Found in this thread:
A way to quick preview .ipynb files
You can view it nicely in PyCharm as well.
You can convert your notebook to html (or other formats) via
File -> Download as -> html
You can see them dropping the file on VSCode too.

How to get raw code from a Jupyter notebook?

It would be really handy to have a button to copy raw code from a Jupyter notebook to the clipboard so it can be dumped into a text file, .py/.R script, or straight into the terminal (a button like github has to copy to clipboard)
Does a button (or even a chrome add on) exist?
Obviously you could copy/paste sections manually into a txt file, but that's laborious for long notebooks/markdowns
Yes,there is a button:
File > Download as > Python (.py)
There is a more handy way for Mac OS users.
By installing an Automator script converting the ipynb file to py first, then automatically copy the content to the clipboard with one right-click and select the script in the Quick Action part.
Github link
You can use the magic command %%writefile to save as py file
%%writefile "directory/to/file/filename.py"
However, you should place this command at the top of the cell, otherwise it will throw an error.

Download data from a jupyter server

I'm using ipython notebook by connecting to a server
I don't know how to download a thing (data frame, .csv file,... for example) programatically to my local computer. Because I can't specific declare the path like C://user//... It will be downloaded to their machine not mine
Run this in separate cell in one of the notebooks:
!tar cvfz zipname.tar.gz *
To cover more folders up the tree, write ../ before the * for every step up the directory.
tar cvfz zipname.tar.gz ../../*
The file zipname.tar.gz will be saved in the same folder as your notebook.
Also if files size is too large execute the following in same notebook block
!split -b 200m allfiles.tar.gz allfiles.tar.gz.part
Alternatively you can use this extension https://github.com/data-8/nbzip
If you are using Jupyter notebook, you can go to the "File" tab on the top left part of the notebook and click "Open". It shows you the content of the current directory. You can select your data file with different format (CSV, text, etc) and then you can download it in your local computer.
Open tab in Jupyter notebook
Download your desired file
Based on another answer,
the following function will export a pandas data frame to a csv file and it will provide you with a link to download the csv file in your browser:
def csv_download_link(df, csv_file_name, delete_prompt=True):
"""Display a download link to load a data frame as csv from within a Jupyter notebook"""
df.to_csv(csv_file_name, index=False)
from IPython.display import FileLink
display(FileLink(csv_file_name))
if delete_prompt:
a = input('Press enter to delete the file after you have downloaded it.')
import os
os.remove(csv_file_name)
To get a link to a csv file, enter or import the above function and use the code below in a jupyter notebook cell :
csv_download_link(df, 'df.csv')
By default the argument delete_prompt=True makes sure that once you have downloaded the csv file, it gets deleted so the file doesn't pollute the git repository where you naturally archive your notebooks (converted in markdown format with jupytext for meaningful diffs).
The download option did not appear for me.
The solution was to open the file (which could not be correctly read as it was a binary file), and to download it from the notebook's notepad.
You can also download the files directly from the Jupyter dashboard. To open it, either click on the Jupyter icon in the top left corner, or alternatively select Open... from the File menu:
After the dashboard has opened, select the file you want to download by checking the check box to the left of it. A Download button will appear in the actions bar above:

Default working directory for Python IDLE?

Is there a configuration file where I can set its default working directory? It currently defaults to my home directory, but I want to set it to another directory when it starts. I know I can do "import os" followed by "os.chdir("")" but that's kind of troublesome. It'd be great if there is a conf file that I can edit and change that setting, but I am unable to find it.
In particular, I've looked into my OS (Ubuntu)'s desktop entry '/usr/share/applications/idle-python3.2.desktop', which doesn't contain a conf file, but points to '/usr/lib/python3.2/idlelib/PyShell.py', which points to config-*.def conf files under the same folder, with 'config-main.def' being the most likely candidate. However I am unable to find where the default path is specified or how it can be changed.
It seems that the path is hard-coded in PyShell.py, though I could be wrong with my limited knowledge on Python. I will keep looking, but would appreciate it if somebody knows the answer on top of his or her head. Thanks in advance.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
This is also my answer here: Default save path for Python IDLE? Let me know if this works!
I've found a solution after looking into PyShell.py:
Create a python file under a preferred directory, in my case '~/.idlerc/init.py', and copy/paste the following lines:
import os
os.chdir('<your preferred directory>')
Pass "-r '~/.idlerc/init.py' " argument to the IDLE startup command, like the following (your exec location and name may vary depending on OS, etc):
/usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py
Just use a shell script such as:
#!/bin/bash
cd /Users/pu/Projects/L-Python
/usr/bin/idle
and run that instead of stock idle. The example is on OS X, adapt to your system.
I'm new to python and learning from 'Dive into Python' by mark Pilgrim (can be found online free)
the answer is in chapter 2.4 - hope he doesn't mind me pasting it here as its also plugging his book and is in the GPL
Before you go any further, I want to briefly mention the library
search path. Python looks in several places when you try to import a
module. Specifically, it looks in all the directories defined in
sys.path. This is just a list, and you can easily view it or modify it
with standard list methods. (You'll learn more about lists later in
this chapter.)
Example 2.4. Import Search Path
import sys
sys.path
sys.path.append('/my/new/path')
It's a good book I am a programmer - usually I find learning from books sends me quickly to sleep - not the case here ....
All I had to do here (Linux Mint 18.2 Xfce) ...
Just add path in line "working directory" = "Arbeitsverzeichnis"
It can change depending on where you installed Python. Open up IDLE, import os, then call os.getcwd() and that should tell you exactly where your IDLE is working on.
One default path is specified in idlelib.IOBinding.IOBinding.dirname or idlelib.IOBinding.IOBinding.filename
Ubuntu
So my idle-python3.desktop
file in /usr/share/applications looks like this:
[Desktop Entry]
Name=IDLE (using Python-3)
Comment=Integrated Development Environment for Python (using Python-3)
Exec=python3 -c "import idlelib.IOBinding, os; idlelib.IOBinding.IOBinding.dirname='/DEFAULT/DIRECTORY';import idlelib.idle"
Icon=/usr/share/pixmaps/python3.xpm
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true
To use it you need to set /DEFAULT/DIRECTORY to your desired directory, copy it with root rights into /usr/share/applications. You can also use it for Python 2 but then you need to replace the 3s with 2s.
ConfigFiles
There are also extensions that can be loaded. These must be modules and you specify them by module name. The config files for IDLE are located in HOME/.idlerc and parsed with a configparser. I did not get further with this.
Here's a way to reset IDLE's default working directory for MacOS if you launch Idle as an application by double-clicking it. You need a different solution if you launch Idle from a command line in Terminal. This solution is a permanent fix. You don't have to rechange the directory everytime you launch IDLE. I wish it were easier.
The idea is to edit a resource file inside of the IDLE package in Applications.
Start by finding the the file. In Finder, go to IDLE in Applications (in the Python folder) as if you wanted to open it. Right click and select "show package contents". Open Contents, then open Resources. In Resources, you'll see a file called idlemain.py. This file executes when you launch idle and sets, among other things, the working directory. We're going to edit that.
But before you can edit it, you need to give yourself permission to write to it. To do that, right click on the idlemain.py and select get info. Scroll to the bottom of the getinfo window and you'll see the Sharing & Permissions section. On the bottom right there's a lock icom. Click the lock and follow the prompts to unlock it. Once it's unlocked, look to the left for the + (under the list of users with permissions). Click it. That will bring up a window with a list of users you can add. Select yourself (probably the name of your computer or your user account) and click Select. You'll see yourself added to the list of names with permissions. Click where is says "Read only" next to your name and change it to "Read & Write". Be careful not to change anything else. When you're done, click the lock again to lock the changes.
Now go back to idlemain.py and open it with any text editor (you could use Idle, TextEdit, or anything. Right under the import statement at the top is the code to change the default working directory. Read the comment if you like, then replace the single line of code under the comment with
os.chdir('path of your desired working directory')
Mine looks like this:
os.chdir('/Users/MyName/Documents/Python')
Save your changes (which should work because you gave yourself permission). Next time you start Idle, you should be in your desired working directory. You can check with the following commands:
import os
os.getcwd()
This ought to be the number one answer. I have been playing around this for an hour or more and nothing worked. Paul explains this perfectly. It's just like the PATH statement in Windows. I successfully imported a module by appending my personal "PythonModules" path/dir on my Mac (starting at "/users/etc") using a simple
import xxxx command in Idle.

Categories

Resources