3

I would like to know if there is a way in Google Colab that can collate outputs nicely, just like Markdown in R and how IPython Notebook can be converted to pdf and html format?

My output consists of multiple tables, graphs etc. I would like to preferably pretty print them into one file, of which some part are presentable enough to be used in a report.If there's no such method, what is the best alternative?

4
  • Do you mean creating a "text" cell? Commented Mar 18, 2020 at 23:28
  • @norok2 Hi I would like to output all my Google Colab results collectively in a nice way. Commented Mar 18, 2020 at 23:43
  • Isn't what you use in Google Colab a Jupyter Notebook? Commented Mar 18, 2020 at 23:47
  • @AMC It appears to me Google Colab doesn't have some functions in Jupyter. Commented Mar 19, 2020 at 0:05

3 Answers 3

5

You can also create a pdf in colab itself using nbconvert.

!apt update
!apt install texlive-xetex texlive-fonts-recommended texlive-generic-recommended

import re, pathlib, shutil

# Get a list of all your Notebooks
notebooks = [x for x in pathlib.Path("/content/drive/My Drive/Colab Notebooks").iterdir() if 
             re.search(r"\.ipynb", x.name, flags = re.I)]

for i, n in enumerate(notebooks):
    print(f"\nProcessing  [{i+1:{len(str(len(notebooks)))}d}/{len(notebooks)}]  {n.name}\n")

    # Optionally copy your notebooks from gdrive to your vm
    shutil.copy(n, n.name)
    n = pathlib.Path(n.name)

    !jupyter nbconvert "{n.as_posix()}" --to pdf --output "{n.stem.replace(" ", "_")}"

Instead of using a magic to run nbconvert you could also use subprocess

s = subprocess.Popen(shlex.split(
    f'jupyter nbconvert "{n.as_posix()}" --to pdf --output "{n.stem.replace(" ", "_")}"'
    ), shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
s.wait()
s.stdout.read()

There are also more packages available regarding xetex in case you use a very sophisticated template.

sudo apt install pandoc nbconvert texlive texlive-latex-extra texlive-generic-extra
Sign up to request clarification or add additional context in comments.

Comments

3

You can save / export an IPython notebook (menu: File / Download .ipynb) and then use Jupyter to save to PDF.

2 Comments

Hi, thanks. Do you mean that there's no possible way in Google Colab, and I have to output via IPython Notebook anyway?
I see no other simple way of doing what you are after.
0

In Colab, an easy alternative will be to use the browser's print functionality.

  1. click on file on the toolbar & select print.
  2. select Save as PDF - this will download the notebook to your PC as pdf.

1 Comment

The browser functionality usually gives incomplete code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.