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.
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.
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.
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.
So I found this great visualization of Newton's unconstrained optimization on a Jupyter Notebook within Louis Tiao's public account, and I want to run it on my laptop.
With other platforms, I'd be able to just copy and paste (including the annotations), and get it ready to "play". But with Notebook, I have to deal with multiple cells, and copy and paste each one separately, and in order.
Is there a more expeditious way of transferring the code?
A jupyter notebook is stored in a file with a .ipynb extension. The internals of the file a specially formatted text called json (or similar to it).
Right click the link and save as name.ipynb (it defaults to this in windows on chrome) and choose a location to save it. The best location is one where you have all your notebooks by default.
Then run jupyter and open the file.