Skip to content

Instantly share code, notes, and snippets.

@bradleyboehmke
Created December 3, 2025 19:48
Show Gist options
  • Select an option

  • Save bradleyboehmke/6984d0c7b6fefa770bcf1cba62fc9785 to your computer and use it in GitHub Desktop.

Select an option

Save bradleyboehmke/6984d0c7b6fefa770bcf1cba62fc9785 to your computer and use it in GitHub Desktop.
Save Colab notebook as an HTML file
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)
# Update notebook metadata to handle widgets
notebook_content['metadata'].setdefault('widgets', {}).setdefault(
'application/vnd.jupyter.widget-state+json', {}).setdefault('state', {})
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment