-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.py
81 lines (73 loc) · 3.5 KB
/
convert.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
'''
Converts the Jupyter notebook to a python script, markdown, and restructured text.
'''
import os
import logging
import click
from shutil import copyfile
from ...modules.base import setup_logger, get_project_root, NOTEBOOKS_DIR, RST_DIR, DOCS_DIR, SCRIPTS_DIR
import nbformat as nbf
import nbconvert as nbc
# Set up logger
setup_logger()
@click.command()
@click.option('--script', is_flag=True, default=False, help='Convert the notebook to a python script.')
@click.option('--rst', is_flag=True, default=False, help='Convert the notebooks to a reStructuredText output.')
@click.option('--markdown', is_flag=True, default=False, help='Convert the notebooks to a reStructuredText output.')
# @click.option('--all', is_flag=True, default=False, help='Convert the notebooks to a reStructuredText output.')
def convert(script, rst, markdown):
"""
Launch Jupyter Notebook (using Docker).
"""
if not script and not rst and not markdown:
all = True
else:
all = False
project_root = get_project_root()
notebooks = os.path.join(project_root, NOTEBOOKS_DIR)
if os.path.isdir(notebooks):
for dirpath, _, filenames in os.walk(notebooks):
for filename in filenames:
if filename.endswith(".ipynb") and os.path.basename(dirpath) != '.ipynb_checkpoints':
parentpath = os.path.relpath(dirpath, notebooks)
nb = nbf.read(
open(os.path.join(dirpath, filename), 'r'), as_version=4)
if rst or all:
# EXPORT RST
exporter = nbc.RSTExporter()
dir = os.path.join(project_root, RST_DIR, parentpath)
logging.info("Converting the "+filename +
" notebook to an RST format.")
_export(nb, exporter, project_root, dir,
parentpath, filename, '.rst')
if markdown or all:
# EXPORT Markdown
exporter = nbc.MarkdownExporter()
dir = os.path.join(project_root, DOCS_DIR, parentpath)
logging.info("Converting the "+filename +
" notebook to an Markdown format.")
_export(nb, exporter, project_root, dir,
parentpath, filename, '.md')
if script or all:
# EXPORT SCRIPT
exporter = nbc.ScriptExporter()
dir = os.path.join(
project_root, SCRIPTS_DIR, parentpath)
logging.info("Converting the "+filename +
" notebook to a python script.")
_export(nb, exporter, project_root, dir,
parentpath, filename, '.py')
elif filename.endswith(".Rmd"):
# TBD add processing for R notebooks. s
print("R", filename)
def _export(nb, exporter, project_root, outdir, parentpath, filename, extension):
# EXPORT RST
(body, _) = exporter.from_notebook_node(nb)
# output the rst file
print("parent path", parentpath)
print("outputdir", outdir)
filename_base = os.path.splitext(filename)[0]
if not os.path.exists(outdir):
os.makedirs(outdir)
with open(os.path.join(outdir, filename_base+extension), "w") as text_file:
text_file.write(body)