Last active
April 14, 2023 17:55
-
-
Save cjgunnar/f1e05a586cd3b8858b077bb9ef4897f0 to your computer and use it in GitHub Desktop.
A Colab cell that will generate a nicely formatted PDF of a Colab notebook in your Google Drive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"cellView": "form", | |
"id": "srzJGrHNVHEC" | |
}, | |
"outputs": [], | |
"source": [ | |
"#EXPORT_EXCLUDE#\n", | |
"\n", | |
"#@title # Create a PDF\n", | |
"\n", | |
"#@markdown ## Click through any prompts that appear.\n", | |
"\n", | |
"\"\"\"\n", | |
"Simply using File -> Print often gives poor formatting or cutoff graphs and code blocks.\n", | |
"This cell will download a nicely formatted version instead.\n", | |
"\"\"\"\n", | |
"\n", | |
"from google.colab import drive\n", | |
"from pathlib import Path\n", | |
"\n", | |
"# only necessary if plotly graphs used\n", | |
"import plotly.io as pio\n", | |
"pio.renderers.default='notebook'\n", | |
"\n", | |
"# write changes then mount\n", | |
"drive.flush_and_unmount()\n", | |
"drive.mount(\"/content/drive\")\n", | |
"\n", | |
"#@markdown 1. Paste the path to the notebook you want a PDf of. You can use the Files menu on the left to copy the path (run this cell in order to mount). \n", | |
"\n", | |
"path = \"/content/drive/MyDrive/Colab Notebooks/colab_print_demo.ipynb\" #@param {type: \"string\"}\n", | |
"\n", | |
"#@markdown 2. Do you want to include code cells in the pdf?\n", | |
"include_code = True #@param {type: \"boolean\"}\n", | |
"\n", | |
"#@markdown 3. Do you want to download the PDF to your computer when done? The PDF will also be placed next to the converted notebook file.\n", | |
"download = True #@param {type: \"boolean\"}\n", | |
"\n", | |
"#@markdown 4. Click Run to generate a PDF. (this might take a while)\n", | |
"\n", | |
"path = Path(path)\n", | |
"\n", | |
"if not path.exists():\n", | |
" print(\"No file found at \", path)\n", | |
" raise FileNotFoundError()\n", | |
"\n", | |
"print(\"installing dependencies...\")\n", | |
"\n", | |
"# install dependencies... this will take a while\n", | |
"!sudo apt -q install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget &> /dev/null\n", | |
"!python -m pip -qqq install -U notebook-as-pdf &> /dev/null\n", | |
"!pip install -qqq nbconvert[webpdf] -U &> /dev/null\n", | |
"!pyppeteer-install &> /dev/null\n", | |
"!sudo apt -q install chromium-chromedriver &> /dev/null\n", | |
"\n", | |
"# credit to https://stackoverflow.com/questions/57217924/pyppeteer-errors-browsererror-browser-closed-unexpectedly\n", | |
"if include_code:\n", | |
" !jupyter nbconvert --disable-chromium-sandbox --WebPDFExporter.disable_sandbox=True --RegexRemovePreprocessor.patterns=\"^#EXPORT_EXCLUDE#.*\" --to webpdf \"{path}\"\n", | |
"else:\n", | |
" !jupyter nbconvert --disable-chromium-sandbox --WebPDFExporter.disable_sandbox=True --RegexRemovePreprocessor.patterns=\"^#EXPORT_EXCLUDE#.*\" --no-input --to webpdf \"{path}\"\n", | |
"\n", | |
"if download:\n", | |
" from google.colab import files\n", | |
" output = path.with_suffix('.pdf')\n", | |
" files.download(output)" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment