From f4dce6fcc3dd6352795ba3e2007a127e2e6e333e Mon Sep 17 00:00:00 2001 From: luck Date: Sat, 7 Oct 2017 12:04:48 +0200 Subject: [PATCH 1/3] use of nbconvert TemplateExporter Added nbsphinx options --- nbsphinx.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/nbsphinx.py b/nbsphinx.py index 44715d09..a690398e 100644 --- a/nbsphinx.py +++ b/nbsphinx.py @@ -46,6 +46,8 @@ _ipynbversion = 4 +global nbsphinx_args_output_prompt + # See nbconvert/exporters/html.py: DISPLAY_DATA_PRIORITY_HTML = ( 'application/javascript', @@ -532,7 +534,11 @@ class Exporter(nbconvert.RSTExporter): """ def __init__(self, execute='auto', kernel_name='', execute_arguments=[], - allow_errors=False, timeout=30, codecell_lexer='none'): + allow_errors=False, timeout=30, codecell_lexer='none', + exclude_output = False, exclude_input = False, + exclude_input_prompt = False, exclude_output_prompt = False, + exclude_markdown = False, exclude_code_cell = False): + """Initialize the Exporter.""" self._execute = execute self._kernel_name = kernel_name @@ -541,10 +547,24 @@ def __init__(self, execute='auto', kernel_name='', execute_arguments=[], self._timeout = timeout self._codecell_lexer = codecell_lexer loader = jinja2.DictLoader({'nbsphinx-rst.tpl': RST_TEMPLATE}) + self._exclude_output = exclude_output + self._exclude_input = exclude_input + self._exclude_input_prompt = exclude_input_prompt + self._exclude_output_prompt = exclude_output_prompt + self._exclude_markdown = exclude_markdown + self._exclude_code_cell = exclude_code_cell + super(Exporter, self).__init__( template_file='nbsphinx-rst.tpl', extra_loaders=[loader], config=traitlets.config.Config( - {'HighlightMagicsPreprocessor': {'enabled': True}}), + {'HighlightMagicsPreprocessor': {'enabled': True}, + 'TemplateExporter':{ + "exclude_output": exclude_output, + "exclude_input": exclude_input, + "exclude_input_prompt": exclude_input_prompt, + "exclude_output_prompt": exclude_output_prompt, + "exclude_markdown": exclude_markdown, + "exclude_code_cell": exclude_code_cell}}), filters={ 'convert_pandoc': convert_pandoc, 'markdown2rst': markdown2rst, @@ -630,6 +650,9 @@ def parse(self, inputstring, document): resources['output_files_dir'] = os.path.relpath(auxdir, srcdir) resources['unique_key'] = re.sub('[/ ]', '_', env.docname) + global nbsphinx_args_output_prompt + nbsphinx_args_output_prompt = env.config.nbsphinx_exclude_output_prompt + exporter = Exporter( execute=env.config.nbsphinx_execute, kernel_name=env.config.nbsphinx_kernel_name, @@ -637,6 +660,13 @@ def parse(self, inputstring, document): allow_errors=env.config.nbsphinx_allow_errors, timeout=env.config.nbsphinx_timeout, codecell_lexer=env.config.nbsphinx_codecell_lexer, + + exclude_output = env.config.nbsphinx_exclude_output, + exclude_input = env.config.nbsphinx_exclude_input, + exclude_input_prompt = env.config.nbsphinx_exclude_input_prompt, + exclude_output_prompt = env.config.nbsphinx_exclude_output_prompt, + exclude_markdown = env.config.nbsphinx_exclude_markdown, + exclude_code_cell = env.config.nbsphinx_exclude_code_cell ) try: @@ -756,7 +786,11 @@ def run(self): # Optional output prompt if execution_count: - text = 'Out[{}]:'.format(execution_count) + global nbsphinx_args_output_prompt + if nbsphinx_args_output_prompt: + text = '' + else: + text = 'Out[{}]:'.format(execution_count) container += CodeNode.create(text, classes=['prompt']) latex_prompt = text + ' ' else: @@ -1330,6 +1364,14 @@ def setup(app): app.add_config_value('nbsphinx_allow_errors', False, rebuild='') app.add_config_value('nbsphinx_timeout', 30, rebuild='') app.add_config_value('nbsphinx_codecell_lexer', 'none', rebuild='env') + #hide or display input, output,.... + app.add_config_value('nbsphinx_exclude_output', False, rebuild='env') + app.add_config_value('nbsphinx_exclude_input', False, rebuild='env') + app.add_config_value('nbsphinx_exclude_input_prompt', False, rebuild='env') + app.add_config_value('nbsphinx_exclude_output_prompt', False, rebuild='env') + app.add_config_value('nbsphinx_exclude_markdown', False, rebuild='env') + app.add_config_value('nbsphinx_exclude_code_cell', False, rebuild='env') + # Default value is set in builder_inited(): app.add_config_value('nbsphinx_prompt_width', None, rebuild='html') app.add_config_value('nbsphinx_responsive_width', '540px', rebuild='html') From d31a0465d4185b8bbf5b59022bbd56eddb2a6d65 Mon Sep 17 00:00:00 2001 From: luck Date: Sat, 14 Oct 2017 16:30:11 +0200 Subject: [PATCH 2/3] use of nbconvert TemplateExporter "self.state.document.settings.env.config.nbsphinx_exclude_output_prompt" instead of a extra global variable --- nbsphinx.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nbsphinx.py b/nbsphinx.py index a690398e..acc26cbe 100644 --- a/nbsphinx.py +++ b/nbsphinx.py @@ -46,7 +46,6 @@ _ipynbversion = 4 -global nbsphinx_args_output_prompt # See nbconvert/exporters/html.py: DISPLAY_DATA_PRIORITY_HTML = ( @@ -650,9 +649,6 @@ def parse(self, inputstring, document): resources['output_files_dir'] = os.path.relpath(auxdir, srcdir) resources['unique_key'] = re.sub('[/ ]', '_', env.docname) - global nbsphinx_args_output_prompt - nbsphinx_args_output_prompt = env.config.nbsphinx_exclude_output_prompt - exporter = Exporter( execute=env.config.nbsphinx_execute, kernel_name=env.config.nbsphinx_kernel_name, @@ -786,8 +782,7 @@ def run(self): # Optional output prompt if execution_count: - global nbsphinx_args_output_prompt - if nbsphinx_args_output_prompt: + if self.state.document.settings.env.config.nbsphinx_exclude_output_prompt: text = '' else: text = 'Out[{}]:'.format(execution_count) From 5e2ce66143a46c5ba72a51e44ee8b78b68271750 Mon Sep 17 00:00:00 2001 From: David Powell Date: Tue, 8 May 2018 11:29:28 +1000 Subject: [PATCH 3/3] Remove the exclude_output, exclude_markdown and exclude_code_cell options, since no clear use case has been shown to date. --- src/nbsphinx.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/nbsphinx.py b/src/nbsphinx.py index 347aeb7f..a9b58527 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -46,7 +46,6 @@ _ipynbversion = 4 - # See nbconvert/exporters/html.py: DISPLAY_DATA_PRIORITY_HTML = ( 'application/javascript', @@ -522,9 +521,8 @@ class Exporter(nbconvert.RSTExporter): def __init__(self, execute='auto', kernel_name='', execute_arguments=[], allow_errors=False, timeout=30, codecell_lexer='none', - exclude_output = False, exclude_input = False, - exclude_input_prompt = False, exclude_output_prompt = False, - exclude_markdown = False, exclude_code_cell = False): + exclude_input = False, exclude_input_prompt = False, + exclude_output_prompt = False): """Initialize the Exporter.""" self._execute = execute @@ -534,24 +532,18 @@ def __init__(self, execute='auto', kernel_name='', execute_arguments=[], self._timeout = timeout self._codecell_lexer = codecell_lexer loader = jinja2.DictLoader({'nbsphinx-rst.tpl': RST_TEMPLATE}) - self._exclude_output = exclude_output self._exclude_input = exclude_input self._exclude_input_prompt = exclude_input_prompt self._exclude_output_prompt = exclude_output_prompt - self._exclude_markdown = exclude_markdown - self._exclude_code_cell = exclude_code_cell super(Exporter, self).__init__( template_file='nbsphinx-rst.tpl', extra_loaders=[loader], config=traitlets.config.Config( {'HighlightMagicsPreprocessor': {'enabled': True}, 'TemplateExporter':{ - "exclude_output": exclude_output, "exclude_input": exclude_input, "exclude_input_prompt": exclude_input_prompt, - "exclude_output_prompt": exclude_output_prompt, - "exclude_markdown": exclude_markdown, - "exclude_code_cell": exclude_code_cell}}), + "exclude_output_prompt": exclude_output_prompt}}), filters={ 'convert_pandoc': convert_pandoc, 'markdown2rst': markdown2rst, @@ -667,12 +659,9 @@ def parse(self, inputstring, document): timeout=env.config.nbsphinx_timeout, codecell_lexer=env.config.nbsphinx_codecell_lexer, - exclude_output = env.config.nbsphinx_exclude_output, exclude_input = env.config.nbsphinx_exclude_input, exclude_input_prompt = env.config.nbsphinx_exclude_input_prompt, exclude_output_prompt = env.config.nbsphinx_exclude_output_prompt, - exclude_markdown = env.config.nbsphinx_exclude_markdown, - exclude_code_cell = env.config.nbsphinx_exclude_code_cell ) try: @@ -1471,12 +1460,9 @@ def setup(app): app.add_config_value('nbsphinx_timeout', 30, rebuild='') app.add_config_value('nbsphinx_codecell_lexer', 'none', rebuild='env') #hide or display input, output,.... - app.add_config_value('nbsphinx_exclude_output', False, rebuild='env') app.add_config_value('nbsphinx_exclude_input', False, rebuild='env') app.add_config_value('nbsphinx_exclude_input_prompt', False, rebuild='env') app.add_config_value('nbsphinx_exclude_output_prompt', False, rebuild='env') - app.add_config_value('nbsphinx_exclude_markdown', False, rebuild='env') - app.add_config_value('nbsphinx_exclude_code_cell', False, rebuild='env') # Default value is set in builder_inited(): app.add_config_value('nbsphinx_prompt_width', None, rebuild='html')