Skip to content

Commit 9093df8

Browse files
committed
DOC: Styler documentation changes
- Update docstring on output shapes matching - Change build process API: Enforce output shape requirements on Styler.apply
1 parent f752886 commit 9093df8

File tree

6 files changed

+253
-17975
lines changed

6 files changed

+253
-17975
lines changed

doc/make.py

+88-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
"""
1919
from __future__ import print_function
2020

21-
import glob
21+
import io
22+
import glob # noqa
2223
import os
2324
import shutil
2425
import sys
25-
import sphinx
26+
from contextlib import contextmanager
27+
28+
import sphinx # noqa
2629
import argparse
27-
import jinja2
30+
import jinja2 # noqa
2831

2932
os.environ['PYTHONPATH'] = '..'
3033

@@ -102,10 +105,90 @@ def clean():
102105
shutil.rmtree('source/generated')
103106

104107

108+
@contextmanager
109+
def cleanup_nb(nb):
110+
try:
111+
yield
112+
finally:
113+
try:
114+
os.remove(nb + '.executed')
115+
except OSError:
116+
pass
117+
118+
119+
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''):
120+
"""
121+
Execute notebook in `src` and write the output to `dst`
122+
123+
Parameters
124+
----------
125+
src, dst: str
126+
path to notebook
127+
allow_errors: bool
128+
timeout: int
129+
kernel_name: str
130+
defualts to value set in notebook metadata
131+
132+
Returns
133+
-------
134+
dst: str
135+
"""
136+
import nbformat
137+
from nbconvert.preprocessors import ExecutePreprocessor
138+
139+
with io.open(src, encoding='utf-8') as f:
140+
nb = nbformat.read(f, as_version=4)
141+
142+
ep = ExecutePreprocessor(allow_errors=allow_errors,
143+
timeout=timeout,
144+
kernel_name=kernel_name)
145+
ep.preprocess(nb, resources={})
146+
147+
with io.open(dst, 'wt', encoding='utf-8') as f:
148+
nbformat.write(nb, f)
149+
return dst
150+
151+
152+
def convert_nb(src, dst, to='html', template_file='basic'):
153+
"""
154+
Convert a notebook `src`.
155+
156+
Parameters
157+
----------
158+
src, dst: str
159+
filepaths
160+
to: {'rst', 'html'}
161+
format to export to
162+
template_file: str
163+
name of template file to use. Default 'basic'
164+
"""
165+
from nbconvert import HTMLExporter, RSTExporter
166+
167+
dispatch = {'rst': RSTExporter, 'html': HTMLExporter}
168+
exporter = dispatch[to.lower()](template_file=template_file)
169+
170+
(body, resources) = exporter.from_filename(src)
171+
with io.open(dst, 'wt', encoding='utf-8') as f:
172+
f.write(body)
173+
return dst
174+
175+
105176
def html():
106177
check_build()
107-
os.system('jupyter nbconvert --to=html --template=basic '
108-
'--output=source/html-styling.html source/html-styling.ipynb')
178+
179+
notebooks = [
180+
'source/html-styling.ipynb',
181+
]
182+
183+
for nb in notebooks:
184+
with cleanup_nb(nb):
185+
try:
186+
print("Converting %s" % nb)
187+
executed = execute_nb(nb, nb + '.executed', allow_errors=True)
188+
convert_nb(executed, nb.rstrip('.ipynb') + '.html')
189+
except ImportError:
190+
pass
191+
109192
if os.system('sphinx-build -P -b html -d build/doctrees '
110193
'source build/html'):
111194
raise SystemExit("Building HTML failed.")

doc/source/contributing.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ code.
360360

361361
It is easiest to :ref:`create a development environment <contributing.dev_env>`, then install::
362362

363-
conda install -n pandas_dev sphinx ipython
363+
conda install -n pandas_dev sphinx ipython nbconvert nbformat
364364

365365
Furthermore, it is recommended to have all `optional dependencies
366366
<http://pandas.pydata.org/pandas-docs/dev/install.html#optional-dependencies>`_
@@ -369,6 +369,8 @@ messages when building the docs. This happens because all the code in the docume
369369
is executed during the doc build, and so code examples using optional dependencies
370370
will generate errors. Run ``pd.show_versions()`` to get an overview of the installed
371371
version of all dependencies.
372+
`nbconvert <https://nbconvert.readthedocs.io/en/latest/>`_ and `nbformat <http://nbformat.readthedocs.io/en/latest/>`_ are required to build the Jupyter notebooks
373+
included in the documentation.
372374

373375
.. warning::
374376

0 commit comments

Comments
 (0)