How to export colab notebooks to pdf file - python

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.

Related

Recovering unsaved jupyter notebook

I just got two days worth of work vanished because jupyter notebook was not able to save (_xsrf argument missing).
I browse the internet for solutions:
looking at the running directory in jupyter
looking at the saved version in jupyter (there are none in my case)
I am starting to look at the chrome logs to at least see everything I typed but they are binary. Would you know where I could find logs of everything that was typed on chrome ? Or any other solution to retrieve that code ? You would be my savior !
I had the same issue and was able to recover all of my code from the 'history.sqlite' file in the following directory: ~/.ipython/profile_default/
(on Windows it's C:/Users/USERNAME/.ipython/profile_default/history.sqlite).
This is a file that saves all commands that were issued to the IPython kernel. At the end of the file (opened in a text editor) I found my most recent commands from the Jupyter notebook. This will only work if you did actually run your code though, not if it was just written.
Some more information is on the website where I found this solution: https://medium.com/flatiron-engineering/recovering-from-a-jupyter-disaster-27401677aeeb

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 work with .ipynb files without launching the Jupyter Notebook server?

I'm starting to work more with Jupyter notebooks, and am really starting to like it. However, I find it difficult to use it with my particular setup.
I have a workstation for running all the notebooks, but for a large part of my day I'm on-the-go with a space-constrained laptop with no power outlets. I'd like to be able to edit (but not run) these notebooks without installing and running the full Jupyter server backend, which I imagine would suck up a lot of power.
My question is: Is it possible for me to edit (and not run) notebooks without running the Jupyter server?
You could use one of the following options
1. ipynb-py-convert
With this module you can do a conversion from .py to .ipynb and vice-versa:
ipynb-py-convert ~/name_of_notebook.ipynb ~/name_of_notebook.py
where according to the documentation the cells are left as they are. To get back a jupyter notebook
ipynb-py-convert ~/name_of_notebook.py ~/name_of_notebook.ipynb
2. Ipython
However, you could also do a conversion to .py when you want to work it with an editor like VS Code or Sublime Text after you have download your .ipynb file with ipython:
ipython nbconvert --to python name_of_your_notebook.ipynb
As I was asking this question, I had opened the notebook locally in Visual Studio Code, but the preview was just the raw text representation of the notebook, so I had assumed that it needed the backend to run.
However, I was about to press submit on the question when I checked back in on it, and the notebook showed up just fine. So one solution is to open it in VS Code and wait a little bit.

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.

How to transfer en bloc all the cells of a Jupyter Notebook?

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.

Categories

Resources