Skip to content

Commit f963a2a

Browse files
committed
Enable nbconvert config as a sphinx option.
This enables users to give nbconvert configuration, such as preprocessor settings, as part of their sphinx config.
1 parent 895b8fb commit f963a2a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/nbsphinx.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,11 @@ class Exporter(nbconvert.RSTExporter):
760760
761761
"""
762762

763+
# TODO: define default preprocessors to include our one to write out notebook just after execution
764+
763765
def __init__(self, execute='auto', kernel_name='', execute_arguments=[],
764-
allow_errors=False, timeout=None, codecell_lexer='none'):
766+
allow_errors=False, timeout=None, codecell_lexer='none',
767+
nbconvert_config=None):
765768
"""Initialize the Exporter."""
766769

767770
# NB: The following stateful Jinja filters are a hack until
@@ -795,13 +798,16 @@ def replace_attachments(text):
795798
self._timeout = timeout
796799
self._codecell_lexer = codecell_lexer
797800
loader = jinja2.DictLoader({'nbsphinx-rst.tpl': RST_TEMPLATE})
798-
super(Exporter, self).__init__(
799-
template_file='nbsphinx-rst.tpl', extra_loaders=[loader],
800-
config=traitlets.config.Config({
801+
if nbconvert_config is None:
802+
nbconvert_config = {
801803
'HighlightMagicsPreprocessor': {'enabled': True},
802804
# Work around https://github.com/jupyter/nbconvert/issues/720:
803805
'RegexRemovePreprocessor': {'enabled': False},
804-
}),
806+
}
807+
808+
super(Exporter, self).__init__(
809+
template_file='nbsphinx-rst.tpl', extra_loaders=[loader],
810+
config=traitlets.config.Config(nbconvert_config),
805811
filters={
806812
'convert_pandoc': convert_pandoc,
807813
'markdown2rst': markdown2rst,
@@ -846,13 +852,17 @@ def from_notebook_node(self, nb, resources=None, **kw):
846852
allow_errors = nbsphinx_metadata.get(
847853
'allow_errors', self._allow_errors)
848854
timeout = nbsphinx_metadata.get('timeout', self._timeout)
855+
# TODO: Here, just add the execute preprocessor to the exporter list of preprocessors
856+
# rather than executing directly. We can still pass appropriate config values in
857+
# and that way the tag remove preprocessor is run *before* execution rather than after.
849858
pp = nbconvert.preprocessors.ExecutePreprocessor(
850859
kernel_name=self._kernel_name,
851860
extra_arguments=self._execute_arguments,
852861
allow_errors=allow_errors, timeout=timeout)
853862
nb, resources = pp.preprocess(nb, resources)
854863

855864
if 'nbsphinx_save_notebook' in resources:
865+
# TODO: maybe we write our *own* preprocessor to hook into this stage, right after the execute preprocessor, to save the notebook
856866
# Save *executed* notebook *before* the Exporter can change it:
857867
nbformat.write(nb, resources['nbsphinx_save_notebook'])
858868

@@ -1037,6 +1047,7 @@ def parse(self, inputstring, document):
10371047
allow_errors=env.config.nbsphinx_allow_errors,
10381048
timeout=env.config.nbsphinx_timeout,
10391049
codecell_lexer=env.config.nbsphinx_codecell_lexer,
1050+
nbconvert_config=env.config.nbsphinx_nbconvert_config
10401051
)
10411052

10421053
try:
@@ -2316,6 +2327,7 @@ def setup(app):
23162327
app.add_config_value('nbsphinx_execute', 'auto', rebuild='env')
23172328
app.add_config_value('nbsphinx_kernel_name', '', rebuild='env')
23182329
app.add_config_value('nbsphinx_execute_arguments', [], rebuild='env')
2330+
app.add_config_value('nbsphinx_nbconvert_config', None, rebuild='env')
23192331
app.add_config_value('nbsphinx_allow_errors', False, rebuild='')
23202332
app.add_config_value('nbsphinx_timeout', None, rebuild='')
23212333
app.add_config_value('nbsphinx_codecell_lexer', 'none', rebuild='env')

0 commit comments

Comments
 (0)