Skip to content

Commit

Permalink
DOC: Use nbsphinx
Browse files Browse the repository at this point in the history
Update header levels for nbsphinx

Link to nb, nicer default table
  • Loading branch information
TomAugspurger committed Mar 5, 2017
1 parent 5067708 commit 16b50ab
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 141 deletions.
4 changes: 4 additions & 0 deletions ci/install_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ if [ "$COVERAGE" ]; then
pip install coverage pytest-cov
fi

if [ "$DOC_BUILD" ]; then
pip install nbsphinx
fi

if [ "$BUILD_TEST" ]; then

# build testing
Expand Down
4 changes: 3 additions & 1 deletion doc/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ have ``sphinx`` and ``ipython`` installed. `numpydoc
<https://github.com/numpy/numpydoc>`_ is used to parse the docstrings that
follow the Numpy Docstring Standard (see above), but you don't need to install
this because a local copy of ``numpydoc`` is included in the pandas source
code.
code. `nbsphinx <https://nbsphinx.readthedocs.io/>`_ is used to convert
Jupyter notebooks. You will need to install it if you intend to modify any of
the notebooks included in the documentation.

Furthermore, it is recommended to have all `optional dependencies
<http://pandas.pydata.org/pandas-docs/dev/install.html#optional-dependencies>`_
Expand Down
79 changes: 0 additions & 79 deletions doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,93 +116,14 @@ def cleanup_nb(nb):
pass


def get_kernel():
"""Find the kernel name for your python version"""
return 'python%s' % sys.version_info.major


def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''):
"""
Execute notebook in `src` and write the output to `dst`
Parameters
----------
src, dst: str
path to notebook
allow_errors: bool
timeout: int
kernel_name: str
defualts to value set in notebook metadata
Returns
-------
dst: str
"""
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor

with io.open(src, encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)

ep = ExecutePreprocessor(allow_errors=allow_errors,
timeout=timeout,
kernel_name=kernel_name)
ep.preprocess(nb, resources={})

with io.open(dst, 'wt', encoding='utf-8') as f:
nbformat.write(nb, f)
return dst


def convert_nb(src, dst, to='html', template_file='basic'):
"""
Convert a notebook `src`.
Parameters
----------
src, dst: str
filepaths
to: {'rst', 'html'}
format to export to
template_file: str
name of template file to use. Default 'basic'
"""
from nbconvert import HTMLExporter, RSTExporter

dispatch = {'rst': RSTExporter, 'html': HTMLExporter}
exporter = dispatch[to.lower()](template_file=template_file)

(body, resources) = exporter.from_filename(src)
with io.open(dst, 'wt', encoding='utf-8') as f:
f.write(body)
return dst


def html():
check_build()

notebooks = [
'source/html-styling.ipynb',
]

for nb in notebooks:
with cleanup_nb(nb):
try:
print("Converting %s" % nb)
kernel_name = get_kernel()
executed = execute_nb(nb, nb + '.executed', allow_errors=True,
kernel_name=kernel_name)
convert_nb(executed, nb.rstrip('.ipynb') + '.html')
except (ImportError, IndexError) as e:
print(e)
print("Failed to convert %s" % nb)

if os.system('sphinx-build -P -b html -d build/doctrees '
'source build/html'):
raise SystemExit("Building HTML failed.")
try:
# remove stale file
os.remove('source/html-styling.html')
os.remove('build/html/pandas.zip')
except:
pass
Expand Down
1 change: 1 addition & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'sphinx.ext.pngmath',
'sphinx.ext.ifconfig',
'sphinx.ext.linkcode',
'nbsphinx',
]


Expand Down
Loading

0 comments on commit 16b50ab

Please sign in to comment.