-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
43 lines (40 loc) · 1.47 KB
/
test.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
import pathlib
import nbformat
import nbclient
import nbconvert
import argparse
import os
root = pathlib.Path(__file__).absolute().parent
print(root)
parser = argparse.ArgumentParser()
parser.add_argument("--destination", "-d", default="docs")
parser.add_argument("--notebooks", "-n",
default=root/"notebooks/")
args = parser.parse_args()
path_destination = pathlib.Path(args.destination).absolute()
path_doc = pathlib.Path(args.notebooks).absolute()
if path_doc.is_dir():
docs = path_doc.glob("*")
else:
docs = [path_doc]
for f in docs:
if f.suffix == ".ipynb":
print(f.name)
nb = nbformat.read(f, as_version=4)
os.chdir(f.parent)
nbclient.client.NotebookClient(nb).execute()
for i, cell in enumerate(nb.cells):
if "assert" in cell.source:
del nb.cells[i]
body, _ = nbconvert.HTMLExporter().from_notebook_node(nb)
body = body.replace("<title>Notebook</title>",
"<title>py3d.{}</title>".format(f.stem))
open(path_destination/(f.stem+".html"), "w").write(body)
if f.name == "index.ipynb":
for i, cell in enumerate(nb.cells):
if hasattr(cell, "outputs"):
setattr(cell, "outputs", [])
if "<script>" in cell.source:
del nb.cells[i]
body, _ = nbconvert.MarkdownExporter().from_notebook_node(nb)
open(root/"README.md", "w").write(body)