-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_utils.py
94 lines (77 loc) · 3.16 KB
/
convert_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import time
import os
from pathlib import Path
from nbconvert import HTMLExporter, WebPDFExporter
import nbformat
dir_in = Path("notebooks")
dir_out = Path("out")
template_file = "exam/index.html.j2"
template_paths = [os.path.join(os.path.dirname(__file__), "Templates")]
def extract_outputs(notebook, include_markdown=True):
extracted_cells = []
for cell in notebook.cells:
if cell.cell_type == "code" and cell.outputs:
for output in cell.outputs:
if (
output.output_type == "execute_result"
or output.output_type == "display_data"
):
# Create a markdown cell with the formatted output
markdown_cell = nbformat.v4.new_markdown_cell(
output["data"].get("text/markdown", False)
or output["data"].get("text/latex", False)
or "![image](data:image/png;base64,"
+ output["data"].get("image/png", "")
+ ")"
)
extracted_cells.append(markdown_cell)
elif include_markdown and cell.cell_type == "markdown":
extracted_cells.append(cell)
return extracted_cells
def open_notebook(fn):
# Load the notebook
with open(fn, "r", encoding="utf-8") as f:
notebook = nbformat.read(f, as_version=4)
notebook.cells = extract_outputs(notebook)
return notebook
def export_pdf(notebook, resources, name):
print(f"Exporting PDF {name}")
pdf_exporter = WebPDFExporter(template_file=template_file)
pdf_exporter.template_paths += template_paths
body_pdf, _ = pdf_exporter.from_notebook_node(notebook, resources)
fn = dir_out / f"{name}.pdf"
with open(fn, "wb") as f:
f.write(body_pdf)
return fn
def export_html(notebook, resources, name):
html_exporter = HTMLExporter(template_file=template_file)
html_exporter.template_paths += template_paths
body_html, _ = html_exporter.from_notebook_node(notebook, resources)
fn = dir_out / f"{name}.html"
with open(fn, "w", encoding="utf-8") as f:
f.write(body_html)
return fn
def export(notebook, name, resources={}, pdf=True, html=False):
print(f"Exporting {name} to pdf: {pdf} and html: {html}")
resources = {"outputs": {}}
image_dir = "notebooks/temp/"
for image_name in os.listdir(image_dir):
image_path = os.path.join(image_dir, image_name)
with open(image_path, "rb") as f:
resources["outputs"][image_name] = f.read()
print(resources.keys())
if pdf:
export_pdf(notebook, resources, name)
if html:
export_html(notebook, resources, name)
def detect_file_changes(path, pdf=True, html=False, interval=1):
last_modified = os.path.getmtime(path)
while True:
current_modified = os.path.getmtime(path)
if current_modified != last_modified:
print(f"Detecting changes in {path}")
notebook = open_notebook(path)
export(notebook, path.stem, {}, pdf, html)
print(f"Done exporting.")
last_modified = current_modified
time.sleep(interval)