def nb_to_html(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]

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

    # 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)])

    header_lines = filter(lambda x: not any([s in x for s in filter_strings]),
                          header.split('\n'))
    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)
Exemple #2
0
def nb_to_html(nb_path):
    """convert notebook to html"""
    exporter = html.HTMLExporter(template_file='basic')
    output, resources = exporter.from_filename(nb_path)
    lines = ['<div class="ipynotebook" id="notebook-container">']
    lines.append(output)
    lines.append('</div>')
    return '\n'.join(lines)
Exemple #3
0
def nb_to_html(nb_path):
    """convert notebook to html

    This html will get embedded in another html page, so we need to strip out
    stuff that would interfere with the parent page's formatting.
    """
    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]

    # http://imgur.com/eR9bMRH
    header = header.replace('<style', '<style scoped="scoped"')
    header = header.replace('body{background-color:#ffffff;}\n', '')
    header = header.replace('body{background-color:white;position:absolute;'
                            'left:0px;right:0px;top:0px;bottom:0px;'
                            'overflow:visible;}\n', '')
    header = header.replace('body{margin:0;'
                            'font-family:"Helvetica Neue",Helvetica,Arial,'
                            'sans-serif;font-size:13px;line-height:20px;'
                            'color:#000000;background-color:#ffffff;}', '')
    header = header.replace('\na{color:#0088cc;text-decoration:none;}', '')
    header = header.replace(
        'a:focus{color:#005580;text-decoration:underline;}', '')
    header = header.replace(
        '\nh1,h2,h3,h4,h5,h6{margin:10px 0;font-family:inherit;font-weight:bold;'
        'line-height:20px;color:inherit;text-rendering:optimizelegibility;}'
        'h1 small,h2 small,h3 small,h4 small,h5 small,'
        'h6 small{font-weight:normal;line-height:1;color:#999999;}'
        '\nh1,h2,h3{line-height:40px;}\nh1{font-size:35.75px;}'
        '\nh2{font-size:29.25px;}\nh3{font-size:22.75px;}'
        '\nh4{font-size:16.25px;}\nh5{font-size:13px;}'
        '\nh6{font-size:11.049999999999999px;}\nh1 small{font-size:22.75px;}'
        '\nh2 small{font-size:16.25px;}\nh3 small{font-size:13px;}'
        '\nh4 small{font-size:13px;}', '')
    header = header.replace('background-color:#ffffff;', '', 1)
    # Remove mathjax configuration
    header = mathjax_config.sub('', header)
    # concatenate raw html lines
    lines = ['<div class="ipynotebook">']
    lines.append(header)
    lines.append(body)
    lines.append('</div>')
    return '\n'.join(lines)
def nb_to_html(nb_path):
    """convert notebook to html"""
    c = Config({'ExtractOutputPreprocessor': {'enabled': True}})

    exporter = html.HTMLExporter(template_file='full', config=c)
    notebook = nbformat.read(open(nb_path), 'json')
    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("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
Exemple #5
0
def nb_to_html(nb_path, preprocessors=[]):
    """convert notebook to html"""
    exporter = html.HTMLExporter(template_file='basic',
                                 preprocessors=preprocessors)
    output, resources = exporter.from_filename(nb_path)
    return output