示例#1
0
def main(input_file, output_file):
    """Execute a Jupyter notebook and save the results as HTML.

    Args:
        input_file: Location of the notebook to execute.
        output_file: Location where to write the output log file.
    """
    ep = ExecutePreprocessor(timeout=60 * 60 * 24 * 7)  # one week

    with open(input_file) as f:
        nb = nbformat.read(f, nbformat.NO_CONVERT)

    notebook_path = Path(__file__).resolve().parent.parent.joinpath("notebooks/").as_posix()

    try:
        nb, _ = ep.preprocess(nb, {"metadata": {"path": notebook_path}})
    except CellExecutionError:
        print(f"Error executing notebook '{input_file}'; see '{output_file}' for traceback.")
        raise
    finally:
        exporter = html.HTMLExporter(template_file="full")
        output, _ = exporter.from_notebook_node(nb)
        with open(output_file, mode="wt") as fout:
            fout.write(output)
示例#2
0
    def run(self):
        # check if raw html is supported
        if not self.state.document.settings.raw_enabled:
            raise self.warning('"%s" directive disabled.' % self.name)

        # set up encoding
        attributes = {'format': 'html'}
        encoding = self.options.get(
            'encoding', self.state.document.settings.input_encoding)
        e_handler = self.state.document.settings.input_encoding_error_handler

        # get path to notebook
        source_dir = os.path.dirname(
            os.path.abspath(self.state.document.current_source))
        nb_path = os.path.normpath(os.path.join(source_dir, self.arguments[0]))
        nb_path = utils.relative_path(None, nb_path)

        # convert notebook to html
        exporter = html.HTMLExporter(template_file='full')
        output, resources = exporter.from_filename(nb_path)
        header = output.split('<head>', 1)[1].split('</head>', 1)[0]
        body = output.split('<body>', 1)[1].split('</body>', 1)[0]

        # add HTML5 scoped attribute to header style tags
        header = header.replace('<style', '<style scoped="scoped"')
        header = header.replace(
            'body {\n  overflow: visible;\n  padding: 8px;\n}\n', '')
        header = header.replace("code,pre{", "code{")

        # Filter out styles that conflict with the sphinx theme.
        filter_strings = [
            'navbar',
            'body{',
            'alert{',
            'uneditable-input{',
            'collapse{',
        ]

        filter_strings.extend(['h%s{' % (i + 1) for i in range(6)])

        line_begin = ['pre{', 'p{margin']

        filterfunc = lambda x: not any([s in x for s in filter_strings])
        header_lines = filter(filterfunc, header.split('\n'))

        filterfunc = lambda x: not any([x.startswith(s) for s in line_begin])
        header_lines = filter(filterfunc, header_lines)

        header = '\n'.join(header_lines)

        # concatenate raw html lines
        lines = ['<div class="ipynotebook">']
        lines.append(header)
        lines.append(body)
        lines.append('</div>')
        text = '\n'.join(lines)

        # add dependency
        self.state.document.settings.record_dependencies.add(nb_path)
        attributes['source'] = nb_path

        # create notebook node
        nb_node = notebook('', text, **attributes)
        (nb_node.source, nb_node.line) = \
            self.state_machine.get_source_and_line(self.lineno)

        return [nb_node]
示例#3
0
def nb_to_html(nb_path, skip_exceptions):
    """convert notebook to html"""

    nbconvert_config = Config({
        'ExtractOutputPreprocessor': {'enabled': True},
        'ExecutePreprocessor': {
            'enabled': True,
            # make this configurable?
            'timeout': 3600,
        }
    })

    if skip_exceptions is False:
        nbconvert_config['ExecutePreprocessor']['allow_errors'] = True

    exporter = html.HTMLExporter(template_file='full', config=nbconvert_config)
    notebook = nbformat.read(nb_path, nbformat.NO_CONVERT)
    output, resources = exporter.from_notebook_node(notebook)
    header = output.split('<head>', 1)[1].split('</head>', 1)[0]
    body = output.split('<body>', 1)[1].split('</body>', 1)[0]

    # http://imgur.com/eR9bMRH
    header = header.replace('<style', '<style scoped="scoped"')
    header = header.replace(
        'body {\n  overflow: visible;\n  padding: 8px;\n}\n', '')
    header = header.replace(
        'div#notebook {\n  overflow: visible;',
        '.container {\n  width: 100%;\n}\ndiv#notebook {\n  overflow: visible;\n  width: 100%;')
    header = header.replace(
        '#notebook-container {\n    padding: 15px;',
        '#notebook-container {\n    padding: 0px;',)
    header = header.replace("code,pre{", "code{")

    # Filter out styles that conflict with the sphinx theme.
    filter_strings = [
        'navbar',
        'body{',
        'alert{',
        'uneditable-input{',
        'collapse{',
    ]

    #filter_strings.extend(['h%s{' % (i+1) for i in range(6)])

    line_begin = [
        'pre{',
        'p{margin'
    ]

    filterfunc = lambda x: not any([s in x for s in filter_strings])
    header_lines = filter(filterfunc, header.split('\n'))

    filterfunc = lambda x: not any([x.startswith(s) for s in line_begin])
    header_lines = filter(filterfunc, header_lines)

    header = '\n'.join(header_lines)

    # concatenate raw html lines
    lines = ['<div class="ipynotebook">']
    lines.append(header)
    lines.append(body)
    lines.append('</div>')
    return '\n'.join(lines), resources, notebook
def nb_to_html(nb_path, skip_exceptions, evaluate=True):
    """convert notebook to html"""

    nbconvert_config_html = Config({
        'ExtractOutputPreprocessor': {
            'enabled': True
        },
    })

    nbconvert_config_nb = Config({
        'ExecutePreprocessor': {
            'enabled': evaluate,
            # make this configurable?
            'timeout': 3600,
        },
    })

    if skip_exceptions is False:
        nbconvert_config_nb['ExecutePreprocessor']['allow_errors'] = True

    template = setup.config.run_notebook_export_template

    hexporter = html.HTMLExporter(template=template,
                                  config=nbconvert_config_html)
    nexporter = notebook_exporter.NotebookExporter(config=nbconvert_config_nb)
    notebook = nbformat.read(nb_path, nbformat.NO_CONVERT)
    noutput, _ = nexporter.from_notebook_node(notebook)
    eval_notebook = nbformat.reads(noutput, nbformat.NO_CONVERT)
    houtput, hresources = hexporter.from_notebook_node(eval_notebook)

    if template == 'basic':
        header_lines = []
        body = houtput
    else:
        header = houtput.split('<head>', 1)[1].split('</head>', 1)[0]
        # There may be classes and other modifications to this tag, and the
        # specific class names may change.
        body_open_start = houtput.find('<body')
        body_open_end = houtput[body_open_start:].find('>')
        body = houtput[body_open_start + body_open_end + 1:].split(
            '</body>', 1)[0]

        # http://imgur.com/eR9bMRH
        header = header.replace('<style', '<style scoped="scoped"')
        header = header.replace(
            'body {\n  overflow: visible;\n  padding: 8px;\n}\n', '')
        header = header.replace("code,pre{", "code{")

        # Filter out styles that conflict with the sphinx theme.
        filter_strings = [
            'navbar',
            'body{',
            'alert{',
            'uneditable-input{',
            'collapse{',
        ]

        line_begin = ['pre{', 'p{margin']

        filterfunc = lambda x: not any([s in x for s in filter_strings])
        header_lines = filter(filterfunc, header.split('\n'))

        filterfunc = lambda x: not any([x.startswith(s) for s in line_begin])
        header_lines = filter(filterfunc, header_lines)

    header = '\n'.join(header_lines)

    # concatenate raw html lines
    lines = ['<div class="ipynotebook">', header, body, '</div>']
    return '\n'.join(lines), hresources, noutput