Skip to content

Use of nbconvert TemplateExporter to exclude input/output and/or the related in/out prompts #131

@keluc

Description

@keluc

In order to use this for every notebook at once I'm still trying some things out (see #15) for my earlier comments)

As suggested there, I'v added some nbsphinx options and used the global setting in conf.py

def setup(app):
  ...
    #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')

which is used then in the config setting of Exporter(nbconvert.RSTExporter)

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):
       ...
        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}}),
            filters={
                'convert_pandoc': convert_pandoc,
                'markdown2rst': markdown2rst,
                'get_empty_lines': _get_empty_lines,
                'extract_toctree': _extract_toctree,
                'get_output_type': _get_output_type,
                'json_dumps': json.dumps,
            })

This works fine but not for every output prompt.
I tried to solve it but apparently in class NbOutput(rst.Directive) I don't seem to be able to get hold of the "exclude_output_prompt" setting.
With a global variable I can tackle this but still trying to figure out how to do it correctly without using this 'dirty hack':

class NbOutput(rst.Directive):
 ...
    def run(self):
           ....
            global nbsphinx_args_output_prompt
            if nbsphinx_args_output_prompt:
                text = ''
            else:
                text = 'Out [{}]:'.format(execution_count)

See: changes
Any Idea how I can pass env.config.nbsphinx_exclude_output_prompt to the run method of the class NbOutput?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions