Skip to content

Commit

Permalink
This seems to do the basic thing now.
Browse files Browse the repository at this point in the history
Now we just need to clean things up in the rst => ipynb conversion.
  • Loading branch information
arokem committed Mar 15, 2019
1 parent 3a41036 commit 940ee97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
10 changes: 7 additions & 3 deletions doc/Makefile
Expand Up @@ -27,7 +27,7 @@ help:
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"

clean: api-clean examples-clean
clean: api-clean examples-clean notebooks-clean
-rm -rf _build/*
-rm *-stamp

Expand All @@ -41,6 +41,10 @@ api:
$(PYTHON) tools/docgen_cmd.py dipy reference_cmd
@echo "Build API docs...done."


notebooks-clean:
-cd jupyter_notebooks && rm -rf *.ipynb

examples-clean:
-cd examples_built && rm -rf *.py *.rst *.png fig

Expand Down Expand Up @@ -125,8 +129,8 @@ rstexamples-stamp:
touch $@

ipynbexamples:
$(PYTHON) ../tools/make_notebooks.py
@echo "Notebooks can be found in doc/ipython_notebooks"
../tools/make_notebooks.py
@echo "Notebooks can be found in doc/jupyter_notebooks"

pdf: pdf-stamp
pdf-stamp: latex
Expand Down
33 changes: 18 additions & 15 deletions tools/make_notebooks.py
Expand Up @@ -33,16 +33,16 @@ def clean_string(test_str):


def make_notebook(example):
"""Generate Ipython notebook of the given example
"""Generate jupyter notebook of the given example
Paramters
---------
example : str
example : str
The raw text of the python example
Returns
-------
notebook : str
Ipython notebook in the form of a raw text which can be
jupyter notebook in the form of a raw text which can be
written to a file.
"""
Expand Down Expand Up @@ -72,15 +72,15 @@ def make_notebook(example):


def read_example(fname, directory="../doc/examples/"):
"""Read the example python file to convert to Ipython notebook
"""Read the example python file to convert to jupyter notebook
Parameters
----------
fname : str
Filename of the python example
directory : str
Directory in which the .py examples are located. This has
to specified and changed based on the folder from which we
to specified and changed based on the folder from which we
call the make_notebook function
Default to ../doc/examples/
Expand Down Expand Up @@ -113,10 +113,11 @@ def write_notebook(notebook, fname, directory):
-------
Returns 1 if conversion isn't successful
"""
if not os.path.isdir("ipython_notebooks"):
os.mkdir("ipython_notebooks")
if not os.path.isdir("jupyter_notebooks"):
os.mkdir("jupyter_notebooks")

nbname = codecs.open("ipython_notebooks/" + str(fname) + ".ipynb",
nbname = codecs.open(pjoin('jupyter_notebooks',
str(fname) + ".ipynb"),
encoding='utf-8', mode='w')

nbf.write(notebook, nbname, 4)
Expand All @@ -137,7 +138,7 @@ def valid_examples():
validated_examples = [line.strip() for line in validated_examples]
# Remove blank lines
validated_examples = filter(None, validated_examples)

out_examples = []
for example in validated_examples:
fullpath = pjoin(EG_SRC_DIR, example)
if not example.endswith(".py"):
Expand All @@ -153,10 +154,12 @@ def valid_examples():
msg = "Example, %s, not in index file %s."
msg = msg % (example, EG_INDEX_FNAME)
print(msg)
return validated_examples
out_examples.append(example)
return out_examples


# if __name__ == "__main__":
validated_examples = valid_examples()
for fname in validated_examples:
notebook = make_notebook(read_example(fname))
write_notebook(notebook, fname.split(".")[0], "examples_built")
if __name__ == "__main__":
validated_examples = valid_examples()
for fname in validated_examples:
notebook = make_notebook(read_example(fname))
write_notebook(notebook, fname.split(".")[0], "examples_built")

0 comments on commit 940ee97

Please sign in to comment.