From e2076b774caa7b4f784532e3ed086419dd5ea81e Mon Sep 17 00:00:00 2001 From: jonathanagustin <5193877+jonathanagustin@users.noreply.github.com> Date: Fri, 28 Jun 2024 09:05:28 -0400 Subject: [PATCH] Update README.md Signed-off-by: jonathanagustin <5193877+jonathanagustin@users.noreply.github.com> --- README.md | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 1aa7e1a..bc36392 100644 --- a/README.md +++ b/README.md @@ -4,30 +4,27 @@ ```python def colab2pdf(): - # Colab2PDF by Drengskapur (https://github.com/drengskapur/colab2pdf) - # @title Convert Colab Notebook to PDF {display-mode:'form'} - # VERSION 1.5 - # LICENSE: GPL-3.0-or-later - !apt-get install -yqq --no-install-recommends librsvg2-bin>/dev/null - import contextlib, datetime, google, io, IPython, ipywidgets, json, locale, nbformat, os, pathlib, requests, urllib, warnings, werkzeug, yaml; locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') - def convert(b): - try: - s.value = '⚙️ Converting...'; b.disabled = True; get_ipython().events.register('post_execute', lambda: IPython.display.display(IPython.display.Javascript('document.querySelectorAll("#output-footer").forEach(footer=>footer.remove());'))) - n = pathlib.Path(werkzeug.utils.secure_filename(urllib.parse.unquote(requests.get(f'http://{os.environ["COLAB_JUPYTER_IP"]}:{os.environ["KMP_TARGET_PORT"]}/api/sessions').json()[0]['name']))) - p = pathlib.Path('/content/pdfs') / f'{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}_{n.stem}'; p.mkdir(parents=True, exist_ok=True); warnings.filterwarnings('ignore', category=nbformat.validator.MissingIDFieldWarning) - nb = [cell for cell in nbformat.reads(json.dumps(google.colab._message.blocking_request('get_ipynb', timeout_sec=600)['ipynb']), as_version=4).cells if '--Colab2PDF' not in cell.source] - nb = nbformat.v4.new_notebook(cells=nb or [nbformat.v4.new_code_cell('#')]); nbformat.validator.normalize(nb) - nbformat.write(nb, (p / f'{n.stem}.ipynb').open('w', encoding='utf-8')) - with (p / 'config.yml').open('w', encoding='utf-8') as f: yaml.dump({'include-in-header': [{'text': r'\usepackage{fvextra}\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines,breakanywhere,commandchars=\\\{\}}'}], 'include-before-body': [{'text': r'\DefineVerbatimEnvironment{verbatim}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines}'}]}, f) - !quarto render {p}/{n.stem}.ipynb --metadata-file={p}/config.yml --to pdf -M latex-auto-install -M margin-top=1in -M margin-bottom=1in -M margin-left=1in -M margin-right=1in --quiet - google.colab.files.download(str(p / f'{n.stem}.pdf')); s.value = f'🎉 Downloaded {n.stem}.pdf' - except Exception as e: - s.value = f'⚠️ ERROR {str(e)}' - finally: - b.disabled = False - if not pathlib.Path('/usr/local/bin/quarto').exists(): - !wget -q 'https://quarto.org/download/latest/quarto-linux-amd64.deb' && dpkg -i quarto-linux-amd64.deb>/dev/null && quarto install tinytex --update-path --quiet && rm quarto-linux-amd64.deb - b = ipywidgets.widgets.Button(description='⬇️ Download PDF'); s = ipywidgets.widgets.Label(); b.on_click(lambda b: convert(b)); IPython.display.display(ipywidgets.widgets.HBox([b, s])) - IPython.display.display(IPython.display.Javascript('document.currentScript.parentElement.closest(".output_subarea").querySelector("#output-footer > input").remove();')) -colab2pdf() + # @title Download Notebook in PDF Format{display-mode:'form'} + !apt-get install -yqq --no-install-recommends librsvg2-bin>/dev/null; + import contextlib,datetime,google,io,IPython,ipywidgets,json,locale,nbformat,os,pathlib,requests,urllib,warnings,werkzeug,yaml,re;locale.setlocale(locale.LC_ALL,'en_US.UTF-8');warnings.filterwarnings('ignore',category=nbformat.validator.MissingIDFieldWarning); + %matplotlib inline + def convert(b): + try: + s.value='🔄 Converting';b.disabled=True + n=pathlib.Path(werkzeug.utils.secure_filename(urllib.parse.unquote(requests.get(f'http://{os.environ["COLAB_JUPYTER_IP"]}:{os.environ["KMP_TARGET_PORT"]}/api/sessions').json()[0]['name']))) + p=pathlib.Path('/content/pdfs')/f'{datetime.datetime.utcnow().strftime("%Y%m%d_%H%M%S")}_{n.stem}';p.mkdir(parents=True,exist_ok=True);nb=nbformat.reads(json.dumps(google.colab._message.blocking_request('get_ipynb',timeout_sec=600)['ipynb']),as_version=4) + u=[u for c in nb.cells if c.get('cell_type')=='markdown' for u in re.findall(r'!\[.*?\]\((https?://.*?)\)',c['source']) if requests.head(u,timeout=5).status_code!=200] + if u:raise Exception(f"Bad Image URLs: {','.join(u)}") + nb.cells=[cell for cell in nb.cells if '--Colab2PDF' not in cell.source] + nb=nbformat.v4.new_notebook(cells=nb.cells or [nbformat.v4.new_code_cell('#')]);nbformat.validator.normalize(nb) + nbformat.write(nb,(p/f'{n.stem}.ipynb').open('w',encoding='utf-8')) + with (p/'config.yml').open('w', encoding='utf-8') as f: yaml.dump({'include-in-header':[{'text':r'\usepackage{fvextra}\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines,breakanywhere,commandchars=\\\{\}}'}],'include-before-body':[{'text':r'\DefineVerbatimEnvironment{verbatim}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines}'}]},f) + !quarto render {p}/{n.stem}.ipynb --metadata-file={p}/config.yml --to pdf -M latex-auto-install -M margin-top=1in -M margin-bottom=1in -M margin-left=1in -M margin-right=1in --quiet + google.colab.files.download(str(p/f'{n.stem}.pdf'));s.value=f'✅ Downloaded: {n.stem}.pdf' + except Exception as e:s.value=f'❌ {str(e)}' + finally:b.disabled=False + if not pathlib.Path('/usr/local/bin/quarto').exists(): + !wget -q 'https://quarto.org/download/latest/quarto-linux-amd64.deb' && dpkg -i quarto-linux-amd64.deb>/dev/null && quarto install tinytex --update-path --quiet && rm quarto-linux-amd64.deb + b=ipywidgets.widgets.Button(description='⬇️ Download');s=ipywidgets.widgets.Label();b.on_click(lambda b:convert(b));IPython.display.display(ipywidgets.widgets.HBox([b,s])) +colab2pdf() # | Colab2PDF v1.6 | https://github.com/drengskapur/colab2pdf | GPL-3.0-or-later | ```