spaghetticode
spaghetticode

Reputation: 595

Convert ipynb notebook to HTML in Google Colab

I have a Google Colaboratory Notebook for Data Analysis that I want to output as a HTML file as currently not everything loads within the Colab environment such as large Folium Heatmaps. Is it possible to export the notebook as a html file as opposed to the ipynb and py options?

Upvotes: 57

Views: 155029

Answers (7)

gndps
gndps

Reputation: 821

Snippet to run within a colab cell to download html that works without hardcode passing the current filename:

from IPython.display import display, Javascript
from google.colab import drive, files
import os
import glob
import time
import nbformat
from nbconvert import HTMLExporter
import uuid
import subprocess

drive.mount('/content/drive')

def get_most_recent_ipynb(path):
    notebooks = glob.glob(os.path.join(path, '*.ipynb'))
    if not notebooks:
        return None
    most_recent_file = max(notebooks, key=os.path.getmtime)
    return most_recent_file

def export_notebook_as_html(notebook_path, checkpoint_name='', export_path='/content/html_exports'):
    with open(notebook_path, 'r') as f:
        notebook_content = nbformat.read(f, as_version=4)
    
    html_exporter = HTMLExporter()
    html_content, _ = html_exporter.from_notebook_node(notebook_content)

    base_name = os.path.splitext(notebook_path)[0]
    html_output_path = os.path.join(export_path, f"{os.path.basename(base_name)}{checkpoint_name}_{uuid.uuid4().hex[:4]}.html")
    
    os.makedirs(export_path, exist_ok=True)  # Create directory if it doesn't exist
    
    with open(html_output_path, 'w') as f:
        f.write(html_content)
    files.download(html_output_path)

def download(checkpoint_name=""):
  if checkpoint_name and not checkpoint_name.startswith('_') and not checkpoint_name.startswith('-'):
    checkpoint_name = '_' + checkpoint_name
  recent_ipynb = get_most_recent_ipynb('/content/drive/My Drive/Colab Notebooks/')
  if recent_ipynb:
      export_notebook_as_html(recent_ipynb, checkpoint_name)

download("test")

Caveat: Must press cmd+s to save before running download in order make the current file most recently saved file

Upvotes: 0

Julie
Julie

Reputation: 1

I did all the steps mentioned above to get the file path but then ran this code (each line in it's own cell).

!pip install nbconvert

!jupyter nbconvert -- to html /ADD YOUR FILE PATH HERE.ipynb

from google.colab import files files.download('ADD YOUR FILE PATH HERE.html')

Upvotes: 0

zabop
zabop

Reputation: 7832

Method using Google Colab only

  1. Download your .ipynb file

You can actually do it using only Google Colab. File -> Download .ipynb


  1. Reupload it so Colab can see it

Click on the Files icon on the far left:

enter image description here

Then Upload to session storage:

enter image description here

Select & upload your .ipynb file you just downloaded.


  1. Get your file's path

then obtain its path (you might need to hit the Refresh button before your file shows up):

enter image description here


  1. Conversion using %%shell

Then, just as in Julio's answer, execute in a Colab cell:

%%shell
jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb

The %%shell lets the interpreter know that the following script is interpreted as shell. Don't write anything before %%shell, use a distinct cell for this.

The form of /PATH/TO/YOUR/NOTEBOOKFILE.ipynb will be something like /content/lightaberration3.ipynb.


  1. Your file is ready

Might need to click Refresh again, but your notebook.html will appear in the files, so you can download it:

enter image description here


The great thing about this is that nothing python-related has to be installed on your computer, not conda, not pip, only a browser.

Upvotes: 70

Sanjay S
Sanjay S

Reputation: 63

  1. Download your notebook and upload it back to the colab
  2. Replace PATH_TO_THE_NOTEBOOK_IN_COLAB in the below command to the location of the reuploaded notebook.
  3. Use this command to download the notebook as html => !jupyter nbconvert --to html PATH_TO_THE_NOTEBOOK_IN_COLAB.ipynb.

Upvotes: 0

Riphath
Riphath

Reputation: 1

to continue with "Method using only Google Colab" " %%shell jupyter nbconvert --to html /PATH/TO/YOUR/NOTEBOOKFILE.ipynb" - as given

the following worked for me - type the following in Google Colab !pip install nbconvert

%shell jupyter nbconvert --to html /content/testfile.ipynb (instead of using %%shell in Google Colab, use %shell - this way, it worked for me)

Upvotes: 0

Analyst
Analyst

Reputation: 74

I tried the approach above but couldn't get it to work - for small jobs I log onto https://jupyter.org/try , upload the downloaded ipynb file from Google Colab and then I opened the file, from here you will have the functionality in jupyter.org to download as html or whatever other format you require. It took the pain out of trying to get it to work.

Upvotes: -1

Julio Cezar Silva
Julio Cezar Silva

Reputation: 2456

Google Colab doesn't currently have such a feature as a built-in.

Your best route is to first download it through File > Download .ipynb and then use the standard tool for Jupyter Notebook conversion, nbconvert:

jupyter nbconvert --to html notebook.ipynb

If you use an Anaconda Python distribution, nbconvert is most likely already installed. If not, refer to what is described in their install instructions to be able to convert:

pip install nbconvert
# OR
conda install nbconvert

Upvotes: 42

Related Questions