Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebook to HTML script #543

Merged
merged 13 commits into from
Aug 8, 2024
Merged
6 changes: 3 additions & 3 deletions Otherfiles/RELEASE.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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`
Expand Down
8 changes: 8 additions & 0 deletions Otherfiles/doc-requirements.txt
Original file line number Diff line number Diff line change
@@ -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
29 changes: 0 additions & 29 deletions Otherfiles/doc_to_html.bat

This file was deleted.

103 changes: 103 additions & 0 deletions Otherfiles/notebook_to_html.py
Original file line number Diff line number Diff line change
@@ -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))
Loading