From 2ac0ca8b5f0c2e67c5dc2cfaba6038fdc4750828 Mon Sep 17 00:00:00 2001 From: Sepand Haghighi Date: Thu, 8 Aug 2024 21:41:08 +0330 Subject: [PATCH] Notebook to HTML script (#543) * doc : notebook_to_html.py script added * fix : minor edit in notebook_to_html.py * fix : minor edit in notebook_to_html.py * fix : config added to notebook_to_html.py * fix : minor edit in notebook_to_html.py config * fix : TocExporter added to notebook_to_html.py * del : doc_to_html.bat removed * doc : RELEASE.md updated * fix : doc-requirements.txt added * doc : RELEASE.md updated * fix : doc-requirements.txt updated * doc : RELEASE.md updated * fix : doc-requirements.txt updated --- Otherfiles/RELEASE.md | 6 +- Otherfiles/doc-requirements.txt | 8 +++ Otherfiles/doc_to_html.bat | 29 --------- Otherfiles/notebook_to_html.py | 103 ++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 Otherfiles/doc-requirements.txt delete mode 100644 Otherfiles/doc_to_html.bat create mode 100644 Otherfiles/notebook_to_html.py diff --git a/Otherfiles/RELEASE.md b/Otherfiles/RELEASE.md index 1589ab29..f94d34de 100644 --- a/Otherfiles/RELEASE.md +++ b/Otherfiles/RELEASE.md @@ -1,7 +1,7 @@ # PyCM Release Instructions -**Last Update: 2024-07-19** +**Last Update: 2024-08-06** 1. Create the `release` branch under `dev` 2. Update all version tags @@ -18,7 +18,7 @@ 4. Update `.github/ISSUE_TEMPLATE/bug_report.yml` 1. Add new version tag to `PyCM version` dropbox options 5. Update Document - 1. Run `Otherfiles/notebook_run.py` + 1. Run `Otherfiles/notebook_run.py` (requirements: `Otherfiles/doc-requirements.txt`, Python <= 3.9) 6. Create a PR from `release` to `dev` 1. Title: `Version x.x` (Example: `Version 0.1`) 2. Tag all related issues @@ -43,7 +43,7 @@ 11. Close milestone 12. Close project 13. Generate HTML files - 1. Run `Otherfiles/doc_to_html.bat` + 1. Run `Otherfiles/notebook_to_html.py` (requirements: `Otherfiles/doc-requirements.txt`, Python <= 3.9) 2. Copy `doc` folder for the next steps 14. Update website 1. `git checkout gh-pages` diff --git a/Otherfiles/doc-requirements.txt b/Otherfiles/doc-requirements.txt new file mode 100644 index 00000000..a80dcd5b --- /dev/null +++ b/Otherfiles/doc-requirements.txt @@ -0,0 +1,8 @@ +art==6.2 +notebook==5.7.4 +nbformat==5.0.8 +traitlets==4.3.3 +nbconvert==5.6.1 +jupyter_contrib_nbextensions==0.5.1 +MarkupSafe==2.0.1 +jinja2==2.10.1 \ No newline at end of file diff --git a/Otherfiles/doc_to_html.bat b/Otherfiles/doc_to_html.bat deleted file mode 100644 index e17892bb..00000000 --- a/Otherfiles/doc_to_html.bat +++ /dev/null @@ -1,29 +0,0 @@ -@echo off - -call python -m art text "Doc 2 HTML" -call python setup.py install -if not exist "doc" mkdir doc -for %%f in (doc\*) do (del %%f) -copy Document\*.ipynb doc -cd doc -echo -------------------------- -echo Document.ipynb is running! -call python -m nbconvert --to html_toc --execute Document.ipynb --no-prompt --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags={\"html_hide\"} --log-level=ERROR -move Document.html index.html -if ERRORLEVEL 1 (echo Document.ipynb Failed! & cd .. & exit /b 0) else (echo Document.ipynb Done!) -echo -------------------------- -del Document.ipynb -echo Distance.ipynb is running! -call python -m nbconvert --to html_toc --execute Distance.ipynb --no-prompt --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags={\"html_hide\"} --log-level=ERROR -if ERRORLEVEL 1 (echo Distance.ipynb Failed! & cd .. & exit /b 0) else (echo Distance.ipynb Done!) -echo -------------------------- -del Distance.ipynb -for %%f in (*.ipynb) do ( -echo %%f is running! -call python -m nbconvert --to html --execute %%f --no-prompt --log-level=ERROR -if ERRORLEVEL 1 (echo %%f Failed! & cd .. & exit /b 0) else (echo %%f Done!) -echo -------------------------- -del %%f -) -cd .. -call python Otherfiles\version_check.py diff --git a/Otherfiles/notebook_to_html.py b/Otherfiles/notebook_to_html.py new file mode 100644 index 00000000..aaaa1aa1 --- /dev/null +++ b/Otherfiles/notebook_to_html.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +"""Notebook-to-HTML script.""" +import os +import time +import shutil +import pycm +import nbformat +from traitlets.config import Config +from nbconvert.preprocessors import ExecutePreprocessor +from nbconvert import HTMLExporter +from jupyter_contrib_nbextensions.nbconvert_support import TocExporter +from art import tprint + + +EXAMPLES_LIST = ["Example1", + "Example2", + "Example3", + "Example4", + "Example5", + "Example6", + "Example7", + "Example8"] + +MAIN_DOCS_LIST = ["Distance", + "Document"] + +NOTEBOOK_EXTENSION = ".ipynb" + +HTML_EXTENSION = ".html" + +OUTPUT_FOLDER_PATH = "doc" + +DOCUMENTS_FOLDER_PATH = "Document" + +if __name__ == "__main__": + tprint("PYCM", "bulbhead") + tprint("v{0}".format(pycm.__version__), "bulbhead") + tprint("Notebook Convert", "amc3line") + if OUTPUT_FOLDER_PATH in os.listdir(): + shutil.rmtree(OUTPUT_FOLDER_PATH) + time.sleep(5) + os.mkdir(OUTPUT_FOLDER_PATH) + + print("Documents:") + print("Processing ...") + c = Config() + c.TagRemovePreprocessor.remove_cell_tags = ("html_hide",) + c.TagRemovePreprocessor.enabled = True + c.TemplateExporter.exclude_input_prompt = True + c.HTMLExporter.preprocessors = ["nbconvert.preprocessors.TagRemovePreprocessor"] + for index, notebook in enumerate(sorted(MAIN_DOCS_LIST)): + notebook_path = os.path.join( + DOCUMENTS_FOLDER_PATH, notebook + NOTEBOOK_EXTENSION) + notebook_copy_path = os.path.join( + OUTPUT_FOLDER_PATH, notebook + NOTEBOOK_EXTENSION) + html_file_path = os.path.join( + OUTPUT_FOLDER_PATH, notebook + HTML_EXTENSION) + shutil.copy(notebook_path, notebook_copy_path) + ep = ExecutePreprocessor(timeout=6000, kernel_name='python3') + with open(notebook_copy_path, "r", encoding="utf-8") as f: + nb = nbformat.read(f, as_version=4) + ep.preprocess( + nb, { + 'metadata': { + 'path': OUTPUT_FOLDER_PATH}}) + with open(notebook_copy_path, 'w', encoding='utf-8') as f: + nbformat.write(nb, f) + exporter = TocExporter(config=c) + output_notebook = nbformat.read(notebook_copy_path, as_version=4) + output, resources = exporter.from_notebook_node( + output_notebook, {'metadata': {'name': notebook}}) + with open(html_file_path, "w", encoding="utf-8") as html_file: + html_file.write(output) + os.remove(notebook_copy_path) + print("\t{0}.{1} [OK]".format(index + 1, notebook)) + + print("\nExamples:") + print("Processing ...") + for index, notebook in enumerate(sorted(EXAMPLES_LIST)): + notebook_path = os.path.join( + DOCUMENTS_FOLDER_PATH, notebook + NOTEBOOK_EXTENSION) + notebook_copy_path = os.path.join( + OUTPUT_FOLDER_PATH, notebook + NOTEBOOK_EXTENSION) + html_file_path = os.path.join( + OUTPUT_FOLDER_PATH, notebook + HTML_EXTENSION) + shutil.copy(notebook_path, notebook_copy_path) + ep = ExecutePreprocessor(timeout=6000, kernel_name='python3') + with open(notebook_copy_path, "r", encoding="utf-8") as f: + nb = nbformat.read(f, as_version=4) + ep.preprocess( + nb, { + 'metadata': { + 'path': OUTPUT_FOLDER_PATH}}) + with open(notebook_copy_path, 'w', encoding='utf-8') as f: + nbformat.write(nb, f) + exporter = HTMLExporter(config=c) + output_notebook = nbformat.read(notebook_copy_path, as_version=4) + output, resources = exporter.from_notebook_node( + output_notebook, {'metadata': {'name': notebook}}) + with open(html_file_path, "w", encoding="utf-8") as html_file: + html_file.write(output) + os.remove(notebook_copy_path) + print("\t{0}.{1} [OK]".format(index + 1, notebook))