Exemple #1
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config(
        {
            "CSSHTMLHeaderTransformer": {
                "enabled": True,
                "highlight_class": ".highlight-ipynb",
            },
        }
    )

    config.HTMLExporter.preprocessors = [HtmlLinksPreprocessor]
    config.HTMLExporter.exclude_input = True
    config.HTMLExporter.exclude_output_prompt = True
    config.HtmlLinksPreprocessor["enabled"] = True

    path = os.path.dirname(os.path.realpath(__file__))
    exporter = HTMLExporter(
        config=config,
        template_file="no_code",
        template_path=[".", path + "/../../../scripts/"],
        filters={"highlight2html": custom_highlighter},
    )
    content, info = exporter.from_filename(filepath)

    return content, info
Exemple #2
0
def get_html_from_filepath(
    filepath, start=0, end=None, preprocessors=[], template=None
):
    """Return the HTML from a Jupyter Notebook
    """
    template_file = "basic"
    extra_loaders = []
    if template:
        extra_loaders.append(jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    config = get_config()
    config.update(
        {
            "CSSHTMLHeaderTransformer": {
                "enabled": True,
                "highlight_class": ".highlight-ipynb",
            },
            "SubCell": {"enabled": True, "start": start, "end": end},
        }
    )
    exporter = HTMLExporter(
        config=config,
        template_file=template_file,
        extra_loaders=extra_loaders,
        filters={"highlight2html": custom_highlighter},
        preprocessors=[SubCell] + preprocessors,
    )

    config.CSSHTMLHeaderPreprocessor.highlight_class = " .highlight pre "
    content, info = exporter.from_filename(filepath)

    return content, info
def get_html_from_filepath(filepath, start=0, end=None):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file='basic',
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell])
    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup.decode(formatter="minimal")

    return content, info
Exemple #4
0
    def convert_ipynb_to_html(notebook_file, html_file):
        """
        Convert the given Jupyter notebook file (``.ipynb``)
        to HTML and write it out as the given ``.html`` file.

        Parameters
        ----------
        notebook_file : str
            Path to input Jupyter notebook file.
        html_file : str
            Path to output HTML file.

        Note
        ----
        This function is also exposed as the
        :ref:`render_notebook <render_notebook>` command-line utility.
        """

        # set a high timeout for datasets with a large number of features
        report_config = Config({
            'ExecutePreprocessor': {
                'enabled': True,
                'timeout': 3600
            },
            'HTMLExporter': {
                'template_path': [template_path],
                'template_file': 'report.tpl'
            }
        })

        exportHtml = HTMLExporter(config=report_config)
        output, _ = exportHtml.from_filename(notebook_file)
        open(html_file, mode='w', encoding='utf-8').write(output)
Exemple #5
0
def get_html_from_filepath(filepath,
                           start=0,
                           end=None,
                           preprocessors=[],
                           template=None):
    """Return the HTML from a Jupyter Notebook
    """
    template_file = 'basic'
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    config = Config({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file=template_file,
                            extra_loaders=extra_loaders,
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell] + preprocessors)

    config.CSSHTMLHeaderPreprocessor.highlight_class = " .highlight pre "
    content, info = exporter.from_filename(filepath)

    return content, info
Exemple #6
0
def get_html_from_filepath(filepath, start=0, end=None, preprocessors=[], template=None):
    """Return the HTML from a Jupyter Notebook
    """
    template_file = 'basic'
    extra_loaders = []
    if template:
        extra_loaders.append(jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    config = get_config()
    config.update({'CSSHTMLHeaderTransformer': {
                        'enabled': True,
                        'highlight_class': '.highlight-ipynb'},
                     'SubCell': {
                        'enabled':True,
                        'start':start,
                        'end':end}})
    exporter = HTMLExporter(config=config,
                            template_file=template_file,
                            extra_loaders=extra_loaders,
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell] + preprocessors)

    config.CSSHTMLHeaderPreprocessor.highlight_class = " .highlight pre "
    content, info = exporter.from_filename(filepath)

    return content, info
Exemple #7
0
def get_html_from_filepath(filepath, start=0, end=None):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({
        "CSSHTMLHeaderTransformer": {
            "enabled": True,
            "highlight_class": ".highlight-ipynb",
        },
        "SubCell": {
            "enabled": True,
            "start": start,
            "end": end
        },
    })
    exporter = HTMLExporter(
        config=config,
        template_file="basic",
        filters={"highlight2html": custom_highlighter},
        preprocessors=[SubCell],
    )
    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, "html.parser")
        for i in soup.findAll("div", {"class": "input"}):
            if i.findChildren()[1].find(text="#ignore") is not None:
                i.extract()
        content = soup.decode(formatter=None)

    return content, info
 def to_html(self):
     cb_name = self.nb_buffer.name.split('/')[-1]
     notebook_name = cb_name.split('.')[0]
     self.write_buffer()
     html_exporter = HTMLExporter(config=self.c)
     html_exporter.template_file = 'full'
     body, resources = html_exporter.from_filename(cb_name)
     self.writer.write(body, resources, notebook_name = notebook_name)
def get_html_from_filepath(filepath,
                           start=0,
                           end=None,
                           template=None):
    """Return the HTML from a Jupyter Notebook
    """
    preprocessors_ = [SubCell]

    template_file = "basic"
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    # Load the user's nbconvert configuration
    app = NbConvertApp()
    app.load_config_file()

    app.config.update({
        # This Preprocessor changes the pygments css prefixes
        # from .highlight to .highlight-ipynb
        "CSSHTMLHeaderPreprocessor": {
            "enabled": True,
            "highlight_class": ".highlight-ipynb",
        },
        "SubCell": {
            "enabled": True,
            "start": start,
            "end": end
        },
    })

    # Overwrite Custom jinja filters
    # This is broken right now so needs fix from below
    # https://github.com/jupyter/nbconvert/pull/877
    filters = {
        "highlight_code": custom_highlight_code
    }

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        extra_loaders=extra_loaders,
        filters=filters,
        preprocessors=preprocessors_,
    )
    content, info = exporter.from_filename(filepath)

    # Fix for nbconvert bug
    # content = content.replace("<pre>", '<pre class="highlight highlight-ipynb">')
    # end-fix

    # Since we make a Markdown file we need to remove empty lines and strip
    content = "\n".join([line.rstrip() for line in content.split("\n") if line.rstrip()])

    return content, info
Exemple #10
0
def get_html_from_filepath(filepath,
                           start=0,
                           end=None,
                           template=None,
                           execute=False):
    """Return the HTML from a Jupyter Notebook
    """
    preprocessors_ = [SubCell]

    template_file = "basic"
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    # Load the user's nbconvert configuration
    app = NbConvertApp()
    app.load_config_file()

    app.config.update({
        # This Preprocessor changes the pygments css prefixes
        # from .highlight to .highlight-ipynb
        "CSSHTMLHeaderPreprocessor": {
            "enabled": True,
            "highlight_class": ".highlight-ipynb",
        },
        "SubCell": {
            "enabled": True,
            "start": start,
            "end": end
        },
        # "ExecutePreprocessor": {
        #     "enabled": execute,
        #     "store_widget_state": True
        # }
    })

    # Overwrite Custom jinja filters
    # This is broken right now so needs fix from below
    # https://github.com/jupyter/nbconvert/pull/877
    # TODO: is this fixed and released?
    filters = {
        "highlight_code": custom_highlight_code,
    }

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        preprocessors=preprocessors_,
        extra_loaders=extra_loaders,
        filters=filters,
    )
    content, info = exporter.from_filename(filepath)

    return content, info
    def render_notebook(self, filename, site=None, data=None, lang=None, post=None):
        c = Config(self.site.config['IPYNB_CONFIG'])
        export_html = HTMLExporter(config=c)
        (notebook_raw, _) = export_html.from_filename(filename)

        # The raw HTML contains garbage (scripts and styles). Extract only div id=notebook-container and children
        notebook_html = lxml.html.fromstring(notebook_raw)
        notebook_code = lxml.html.tostring(notebook_html.xpath('//*[@id="notebook-container"]')[0], encoding='unicode')

        return notebook_code, [filename]
Exemple #12
0
def nb2html(nb_path, start=0, end=None, execute=False, kernel_name=""):
    """Convert a notebook and return html"""

    logger.info(f"Convert notebook {nb_path}")

    # Load the user's nbconvert configuration
    app = NbConvertApp()
    app.load_config_file()

    app.config.update(
        {
            # This Preprocessor changes the pygments css prefixes
            # from .highlight to .highlight-ipynb
            "CSSHTMLHeaderPreprocessor": {
                "enabled": True,
                "highlight_class": ".highlight-ipynb",
            },
            "SubCell": {"enabled": True, "start": start, "end": end},
            "ExecutePreprocessor": {
                "enabled": execute,
                "store_widget_state": True,
                "kernel_name": kernel_name,
            },
        }
    )

    preprocessors_ = [SubCell]

    filters = {
        "highlight_code": custom_highlight_code,
    }

    template_file = "mkdocs_html/notebook.html.j2"
    # template_file = "lab/index.html.j2"

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        # uncomment this line when new nbconvert is released
        # https://github.com/jupyter/nbconvert/pull/1429
        extra_template_paths=[os.path.join(THIS_DIR, "templates")],
        preprocessors=preprocessors_,
        filters=filters,
    )
    # Delete this block when nbconvert is released
    # https://github.com/jupyter/nbconvert/pull/1429
    # exporter.template_paths.append(os.path.join(THIS_DIR, "templates"))
    # print(exporter.template_paths)
    # End block

    html, info = exporter.from_filename(nb_path)

    # HTML and CSS fixes
    # html = html_fixes(html, info, fix_css=True, ignore_css=False)
    return GENERATED_MD.format(html=html)
def get_html_from_filepath(filepath, start=0, end=None, template=None):
    """Return the HTML from a Jupyter Notebook
    """
    preprocessors_ = [SubCell]

    template_file = "basic"
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    # Load the user's nbconvert configuration
    app = NbConvertApp()
    app.load_config_file()

    app.config.update({
        # This Preprocessor changes the pygments css prefixes
        # from .highlight to .highlight-ipynb
        "CSSHTMLHeaderPreprocessor": {
            "enabled": True,
            "highlight_class": ".highlight-ipynb",
        },
        "SubCell": {
            "enabled": True,
            "start": start,
            "end": end
        },
    })

    # Overwrite Custom jinja filters
    # This is broken right now so needs fix from below
    # https://github.com/jupyter/nbconvert/pull/877
    filters = {"highlight_code": custom_highlight_code}

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        extra_loaders=extra_loaders,
        filters=filters,
        preprocessors=preprocessors_,
    )
    content, info = exporter.from_filename(filepath)

    # Fix for nbconvert bug
    content = content.replace("<pre>",
                              '<pre class="highlight highlight-ipynb">')
    # end-fix

    # Since we make a Markdown file we need to remove empty lines and strip
    content = "\n".join(
        [line.strip() for line in content.split("\n") if line.strip()])

    return content, info
Exemple #14
0
def trace_get_func(job, func):
    if func == "custom.css":
        return ""

    from nbconvert.exporters import HTMLExporter
    exporter = HTMLExporter()
   
    fn = os.path.join(tempfile.gettempdir(), job, func+"_output.ipynb")

    output, resources = exporter.from_filename(fn)

    return output
Exemple #15
0
def convert_ipynb_to_html(notebook_file, html_file):
    """
    Convert the given `notebook_file` to HTML and
    write it to `html_file`.
    """

    # set a high timeout for datasets with a large number of features
    report_config = Config({'ExecutePreprocessor': {'enabled': True,
                                                    'timeout': 600},
                            'HTMLExporter': {'template_path': [template_path],
                                             'template_file': 'report.tpl'}})

    exportHtml = HTMLExporter(config=report_config)
    output, resources = exportHtml.from_filename(notebook_file)
    open(html_file, mode='w', encoding='utf-8').write(output)
Exemple #16
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({"CSSHTMLHeaderTransformer": {"enabled": True, "highlight_class": ".highlight-ipynb"}})
    exporter = HTMLExporter(config=config, template_file="basic", filters={"highlight2html": custom_highlighter})
    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, "html.parser")
        for i in soup.findAll("div", {"class": "input"}):
            if i.findChildren()[1].find(text="#ignore") is not None:
                i.extract()
        content = soup.decode(formatter=None)

    return content, info
Exemple #17
0
    def render_notebook(self,
                        filename,
                        site=None,
                        data=None,
                        lang=None,
                        post=None):
        c = Config(self.site.config['IPYNB_CONFIG'])
        export_html = HTMLExporter(config=c)
        (notebook_raw, _) = export_html.from_filename(filename)

        # The raw HTML contains garbage (scripts and styles). Extract only div id=notebook-container and children
        notebook_html = lxml.html.fromstring(notebook_raw)
        notebook_code = lxml.html.tostring(
            notebook_html.xpath('//*[@id="notebook-container"]')[0],
            encoding='unicode')

        return notebook_code, [filename]
Exemple #18
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                                                  'highlight_class': '.highlight-ipynb'}})

    config.HTMLExporter.preprocessors = [HtmlLinksPreprocessor]
    config.HtmlLinksPreprocessor['enabled'] = True

    path = os.path.dirname(os.path.realpath(__file__))
    exporter = HTMLExporter(config=config, template_file='no_code',
                            template_path=['.', path + '/../../../scripts/'],
                            filters={'highlight2html': custom_highlighter})
    content, info = exporter.from_filename(filepath)

    return content, info
Exemple #19
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                                                  'highlight_class': '.highlight-ipynb'}})

    config.HTMLExporter.preprocessors = [HtmlLinksPreprocessor]
    config.HtmlLinksPreprocessor['enabled'] = True

    path = os.path.dirname(os.path.realpath(__file__))
    exporter = HTMLExporter(config=config, template_file='no_code',
                            template_path=['.', path + '/../../../scripts/'],
                            filters={'highlight2html': custom_highlighter})
    content, info = exporter.from_filename(filepath)

    return content, info
Exemple #20
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                     'highlight_class': '.highlight-ipynb'}})
    exporter = HTMLExporter(config=config, template_file='basic')
    exporter.register_filter('markdown2html', filters.markdown2html_pandoc)

    content, info = exporter.from_filename(filepath)

    soup = BeautifulSoup(content, "html.parser")
    for i in soup.findAll("div", {"class": "input"}):
        if i.findChildren()[1].find(text='#ignore') is not None:
            i.extract()
    content = soup.decode(formatter=None)

    return content, info
def nb_to_html(nb_path):
    """convert notebook to html"""
    exporter = 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', '')
    header = header.replace("code,pre{", "code{")

    header = header.replace('\n', '')
    comp = re.compile('<style.*?</style>', re.MULTILINE)
    header = comp.sub('', header)

    # 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_strings = [
        'pre{',
        'p{margin'
        ]

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

    header = '\n'.join(header_lines)

    # fix wrong static links to headers
    body = body.replace('class="anchor-link"', 'class="headerlink"')

    # concatenate raw html lines
    lines = ['<div class="ipynotebook">', header, body, '</div>']
    return '\n'.join(lines)
Exemple #22
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                     'highlight_class': '.highlight-ipynb'}})
    exporter = HTMLExporter(config=config, template_file='basic',
                            filters={'highlight2html': custom_highlighter})

    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content)
        for i in soup.findAll("div", {"class" : "input"}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup

    return content, info
Exemple #23
0
def get_html_from_filepath(filepath,
                           template=None,
                           start=0,
                           end=None,
                           no_prompts=False):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    if template is None:
        template = 'basic'

    config = Config({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file=template,
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell])

    # Remove all the input and output prompts from the exported HTML, this extends the width of
    # all cells,, not just the input and output cells.
    if no_prompts:
        exporter.exclude_input_prompt = True
        exporter.exclude_output_prompt = True

    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup.decode(formatter=None)

    return content, info
Exemple #24
0
    def convert_ipynb_to_html(notebook_file, html_file):
        """
        Convert the given Jupyter notebook file (``.ipynb``)
        to HTML and write it out as the given ``.html`` file.

        Parameters
        ----------
        notebook_file : str
            Path to input Jupyter notebook file.
        html_file : str
            Path to output HTML file.

        Note
        ----
        This function is also exposed as the
        :ref:`render_notebook <render_notebook>` command-line utility.
        """

        # `nbconvert` uses `asyncio` which uses an entirely default
        # implemention of the event loop on Windows for Cpython 3.8
        # which breaks the report generation unless we include the
        # following workaround
        if (sys.version_info[0] == 3 and sys.version_info[1] >= 8
                and sys.platform.startswith('win')):
            asyncio.set_event_loop_policy(
                asyncio.WindowsSelectorEventLoopPolicy())

        # set a high timeout for datasets with a large number of features
        report_config = Config({
            'ExecutePreprocessor': {
                'enabled': True,
                'timeout': 3600
            },
            'HTMLExporter': {
                "template_name": "classic",
                "template_file": join(template_path, 'report.tpl')
            }
        })

        exportHtml = HTMLExporter(config=report_config)
        output, _ = exportHtml.from_filename(notebook_file)
        open(html_file, mode='w', encoding='utf-8').write(output)
Exemple #25
0
def nb2html(nb_path, start=0, end=None, execute=False, kernel_name=""):
    """Convert a notebook
    Returns
    -------
        HTML content
    """
    logger.info(f"Converting notebook: {nb_path}")

    app = get_nbconvert_app(start=start,
                            end=end,
                            execute=execute,
                            kernel_name=kernel_name)

    # Use the templates included in this package
    template_file = "mkdocs_html/notebook.html.j2"
    extra_template_paths = [os.path.join(THIS_DIR, "templates")]

    # Customize NBConvert App
    preprocessors_ = [SubCell]
    filters = {
        "highlight_code": custom_highlight_code,
        "markdown2html": custom_markdown2html,
    }

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        extra_template_paths=extra_template_paths,
        preprocessors=preprocessors_,
        filters=filters,
    )

    _, extension = os.path.splitext(nb_path)

    if extension == ".py":
        nb = jupytext.read(nb_path)
        nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb"))
        html, info = exporter.from_file(nb_file)
    else:
        html, info = exporter.from_filename(nb_path)

    return html
def get_html_from_filepath(filepath, start=0, end=None):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                     'highlight_class': '.highlight-ipynb'},
                     'SubCell': {'enabled':True, 'start':start, 'end':end}})
    exporter = HTMLExporter(config=config, template_file='basic',
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell])
    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup.decode(formatter=None)

    return content, info
Exemple #27
0
def convert_ipynb_to_html(notebook_file, html_file):
    """
    Convert the given `notebook_file` to HTML and
    write it to `html_file`.
    """

    # set a high timeout for datasets with a large number of features
    report_config = Config({
        'ExecutePreprocessor': {
            'enabled': True,
            'timeout': 600
        },
        'HTMLExporter': {
            'template_path': [template_path],
            'template_file': 'report.tpl'
        }
    })

    exportHtml = HTMLExporter(config=report_config)
    output, resources = exportHtml.from_filename(notebook_file)
    open(html_file, mode='w', encoding='utf-8').write(output)
Exemple #28
0
def get_html_from_filepath(filepath):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        }
    })

    # Remove prompts from rendered notebook (requires nbconvert>=5.1.0)
    from jinja2 import DictLoader
    loader = DictLoader({
        'noprompt':
        """
         {%- extends 'basic.tpl' -%}

         {% block in_prompt -%}
         {%- endblock in_prompt %}

         {% block output_area_prompt %}
         {%- endblock output_area_prompt %}
                         """
    })
    exporter = HTMLExporter(config=config,
                            template_file='noprompt',
                            extra_loaders=[loader],
                            filters={'highlight2html': custom_highlighter})

    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup.decode(formatter=None)

    return content, info
Exemple #29
0
    def convert(self):
        """Convert the executed notebook to a static HTML file.

        Parameters
        ----------
        """

        self.execute()

        if os.path.exists(self.html_path) and not self.overwrite:
            logger.debug("Rendered notebook page already exists at "
                         f"{self.html_path}. Use overwrite=True to "
                         "overwrite.")
            return self.html_path

        # Initialize the resources dict:
        resources = {}
        resources['config_dir'] = ''  # we don't need to specify config
        resources['unique_key'] = self.filename

        # path to store extra files, like plots generated
        resources['output_files_dir'] = 'nboutput'

        # Exports the notebook to HTML
        logger.debug('Exporting notebook to HTML...')
        exporter = HTMLExporter(**self.convert_kwargs)
        output, resources = exporter.from_filename(self.exec_path,
                                                   resources=resources)

        # Write the output HTML file
        writer = FilesWriter(build_directory=os.path.dirname(self.html_path))
        output_file_path = writer.write(output,
                                        resources,
                                        notebook_name=self.basename)

        return output_file_path
Exemple #30
0
def get_html_from_filepath(filepath, start=0, end=None):
    """Convert ipython notebook to html
    Return: html content of the converted notebook
    """
    config = Config({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file='basic',
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell])
    _, info = exporter.from_filename(filepath)

    # Get content from `jupyter nbconvert` result instead.
    call("jupyter nbconvert --to html --template basic {}".format(filepath),
         shell=True)
    html_filepath = os.path.splitext(filepath)[0] + '.html'
    with open(html_filepath) as file:
        content = file.read()

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()
        content = soup.decode(formatter="minimal")

    return content, info
Exemple #31
0
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)

        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)['metadata']

            # Fix metadata to pelican standards
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
        metadata['ipython'] = True

        # Convert ipython notebook to html
        config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                         'highlight_class': '.highlight-ipynb'}})
        exporter = HTMLExporter(config=config, template_file='basic',
                                filters={'highlight2html': custom_highlighter})

        content, info = exporter.from_filename(filepath)

        if BeautifulSoup:
            soup = BeautifulSoup(content)
            for i in soup.findAll("div", {"class" : "input"}):
                if i.findChildren()[1].find(text='#ignore') is not None:
                    i.extract()
        else:
            soup = content

        # Process using Pelican HTMLReader
        content = '<body>{0}</body>'.format(soup)  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        if ('IPYNB_USE_META_SUMMARY' in self.settings.keys() and \
          self.settings['IPYNB_USE_META_SUMMARY'] == False) or \
          'IPYNB_USE_META_SUMMARY' not in self.settings.keys():
            metadata['summary'] = parser.summary

        def filter_css(style_text):
            '''
            HACK: IPython returns a lot of CSS including its own bootstrap.
            Get only the IPython Notebook CSS styles.
            '''
            index = style_text.find('/*!\n*\n* IPython notebook\n*\n*/')
            if index > 0:
                style_text = style_text[index:]
            index = style_text.find('/*!\n*\n* IPython notebook webapp\n*\n*/')
            if index > 0:
                style_text = style_text[:index]

            style_text = re.sub(r'color\:\#0+(;)?', '', style_text)
            style_text = re.sub(r'\.rendered_html[a-z0-9,._ ]*\{[a-z0-9:;%.#\-\s\n]+\}', '', style_text)

            return '<style type=\"text/css\">{0}</style>'.format(style_text)

        ipython_css = '\n'.join(filter_css(css_style) for css_style in info['inlining']['css'])
        body = ipython_css + body + LATEX_CUSTOM_SCRIPT

        return body, metadata
Exemple #32
0
def get_html_from_filepath(filepath,
                           start=0,
                           end=None,
                           preprocessors=[],
                           template=None):
    """Return the HTML from a Jupyter Notebook
    """
    template_file = 'basic'
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    config = get_config()
    config.update({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file=template_file,
                            extra_loaders=extra_loaders,
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell] + preprocessors)

    config.CSSHTMLHeaderPreprocessor.highlight_class = " .highlight pre "
    content, info = exporter.from_filename(filepath)

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'input'}):
            if i.findChildren()[1].find(text='#ignore') is not None:
                i.extract()

            # transform code block to block-quote for pretty rendering
            elif i.findChildren()[1].find(text='#blockquote') is not None:
                pre = i.find('pre')
                parent_div = pre.parent  # div to replace code with blockquote

                # get raw text after block-quote keyword
                raw_text = ''
                for e in pre.contents[2:]:
                    try:
                        raw_text += e.text
                    except AttributeError:
                        if e.replace(' ', ' ').replace(' ', ' ') == ' ':
                            raw_text += e
                # delete <pre> tag from parent div
                pre.extract()

                # insert <blockquote> tag into parent div
                p = soup.new_tag('p')
                p.append(raw_text)
                block_quote = soup.new_tag('blockquote')
                block_quote.append(p)
                parent_div.append(block_quote)

        # remove input and output prompt
        for prompt in soup.findAll('div', {'class': 'prompt'}):
            prompt.extract()

        # add classes for tables to apply bootstrap style
        # for t in soup.findAll('table', {'class': 'dataframe'}):
        for t in soup.findAll('table'):
            t['class'] = t.get(
                'class', []) + ['table', 'table-striped', 'table-responsive']

        # generate html for templated markdown cells
        mp4_options = 'loop autoplay muted playsinline'  # mp4 video default options
        for i in soup.findAll('div', {'class': 'text_cell_render'}):
            class_str = ""
            style_str = ""

            texts = i.findChildren()
            template_settings = {
                '!article':
                ['article_title', 'article_link', 'image_filename'],
                '!quote': ['quote_text', 'author_into'],
                '!mp4': ['mp4_file', 'image_file', 'description'],
                '!image':
                ['image_file', 'description', 'source_link', 'source_name'],
                '!youtube': ['video_id', 'description', 'start', 'end'],
            }
            # get settings for template keywords
            values = []
            if texts and texts[0].text in template_settings:
                keyword = texts[0].text
                for e in i.findAll('li'):
                    text = e.text
                    if text == 'dark':
                        # 參照 pelican-jupyter-notebook/themes/Hola10/static/css/darkmode.css
                        style_str += "mix-blend-mode: initial;"
                    elif text.startswith("style:"):
                        text = text.replace("style:", "")
                        style_str += text

                    # special keyword for mp4 video options
                    elif keyword == '!mp4' and 'options:' in text:
                        if 'no-loop' in text.replace('_', '-'):
                            mp4_options = mp4_options.replace("loop ", "")
                        if 'no-autoplay' in text.replace('_', '-'):
                            mp4_options = mp4_options.replace("autoplay ", "")
                        if 'controls' in text:
                            mp4_options += " controls"
                    else:
                        values.append(text)

                style_str = f'style="{style_str}"' if style_str else ''
                class_str = f'class="{class_str}"' if class_str else ''

            if not values:
                continue
            html_str = ''
            # article covers for digest
            if keyword == '!article':
                title, link, image_filename = values
                anchor_link = title.replace(' ', ' ').replace(' ',
                                                              ' ').replace(
                                                                  ' ', '-')
                html_str = """
                <h2 id="{anchor_link}">
                    <a href="{link}" target="_blank">{title}</a><a class="anchor-link" href="#{anchor_link}">¶</a>
                </h2>
                <center>
                    <a href="{link}" target="_blank">
                        <img src="{{static}}images/digests/{image_filename}" {style_str}>
                    </a>
                    <br>
                </center>
                """.format(static='static',
                           anchor_link=anchor_link,
                           link=link,
                           title=title,
                           image_filename=image_filename,
                           style_str=style_str)
            elif keyword == '!quote':
                author_intro = ''
                if len(values) > 1:
                    author_intro = """
                    <span style="float:right;margin-right: 1.5rem">─ {author_intro}</span>
                    """.format(author_intro=values[1])
                html_str = """
                <blockquote>
                    <p>
                        {quote}
                        <br>
                        {author_intro}
                        <br>
                    </p>
                </blockquote>
                """.format(quote=values[0], author_intro=author_intro)
            elif keyword == '!mp4':
                mp4_file, description, image_file, source_str, source_link = [
                    ''
                ] * 5
                if len(values) == 1:
                    mp4_file = values[0]
                elif len(values) == 2:
                    for v in values:
                        v = v.lower()
                        if '.mp4' in v:
                            mp4_file = v
                        elif '.jpg' in v or '.png' in v or '.svg' in v or '.jpeg' in v:
                            image_file = v
                        else:
                            description = v
                elif len(values) == 3:
                    mp4_file, image_file, description = values
                    assert '.mp4' in mp4_file, '沒有對應的 mp4 檔案!'
                elif len(values) == 4:
                    mp4_file, image_file, description, source_link = values
                    assert '.mp4' in mp4_file, '沒有對應的 mp4 檔案!'
                    assert 'http' in source_link, '沒有對應的網址!'

                if description:
                    if source_link:
                        source_str = """
                        (<a href="{source_link}" target="_blank">圖片來源</a>)
                        """.format(source_link=source_link)

                    description = """
                    <center>
                        {description}{source_str}
                        <br/>
                        <br/>
                    </center>
                    """.format(description=description, source_str=source_str)
                else:
                    description = '<br>'

                html_str = """
                <video {mp4_options} poster="{{static}}{image_file}" {class_str} {style_str}> 
                  <source src="{{static}}{mp4_file}" type="video/mp4">
                    您的瀏覽器不支援影片標籤,請留言通知我:S
                </video>
                {description}
                """.format(mp4_options=mp4_options,
                           static='static',
                           mp4_file=mp4_file,
                           image_file=image_file,
                           class_str=class_str,
                           style_str=style_str,
                           description=description)
            elif keyword == '!image':
                if len(values) == 1:
                    html_str = """
                    <img {class_str} {style_str} src="{{static}}images/{image_file}"/>
                    <br>
                    """.format(class_str=class_str,
                               style_str=style_str,
                               static='static',
                               image_file=values[0])
                else:
                    description = values[1]
                    source_str = ''
                    if len(values) == 3:
                        source_link = values[2]
                        source_str = """
                        (<a href="{source_link}" target="_blank">圖片來源</a>)
                        """.format(source_link=source_link)
                    elif len(values) == 4:
                        source_link, source_name = values[2:]
                        source_str = """
                        (圖片來源:<a href="{source_link}" target="_blank">{source_name}</a>)
                        """.format(source_link=source_link,
                                   source_name=source_name)

                    html_str = """
                    <center>
                        <img {class_str} {style_str} src="{{static}}/images/{image_file}">
                    </center>
                    <center>
                        {description}{source_str}
                        <br>
                        <br>
                    </center>
                    """.format(class_str=class_str,
                               style_str=style_str,
                               static='static',
                               image_file=values[0],
                               description=description,
                               source_str=source_str)
            elif keyword == '!youtube':
                period_str, description = '', ''
                if len(values) == 1:
                    video_id = values[0]
                elif len(values) == 2:
                    video_id, description = values
                elif len(values) == 3:
                    video_id, description, start = values
                    period_str = f'?start={start}'
                elif len(values) == 4:
                    video_id, description, start, end = values
                    period_str = f'?start={start}&end={end}'

                if description:
                    description = """
                    <center>
                        {description}
                        <br/>
                        <br/>
                    </center>
                    """.format(description=description, )
                else:
                    description = '<br>'

                html_str = """
                <div class="resp-container">
                    <iframe class="resp-iframe" 
                            src="https://www.youtube-nocookie.com/embed/{video_id}{period_str}" 
                            frameborder="0" 
                            allow="accelerometer; 
                            autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
                    </iframe>
                </div>
                {description}
                """.format(video_id=video_id,
                           period_str=period_str,
                           description=description)

            # delete existing template setting
            p = i.find('p')
            ul = i.find('ul')
            p.extract()
            ul.extract()

            # insert generated html string
            i.append(BeautifulSoup(html_str, 'html.parser'))

        content = soup.decode(formatter="minimal")

    return content, info
Exemple #33
0
def get_html_from_filepath(filepath,
                           start=0,
                           end=None,
                           preprocessors=[],
                           template=None):
    """Return the HTML from a Jupyter Notebook
    """
    template_file = 'basic'
    extra_loaders = []
    if template:
        extra_loaders.append(
            jinja2.FileSystemLoader([os.path.dirname(template)]))
        template_file = os.path.basename(template)

    config = get_config()
    config.update({
        'CSSHTMLHeaderTransformer': {
            'enabled': True,
            'highlight_class': '.highlight-ipynb'
        },
        'SubCell': {
            'enabled': True,
            'start': start,
            'end': end
        }
    })
    exporter = HTMLExporter(config=config,
                            template_file=template_file,
                            extra_loaders=extra_loaders,
                            filters={'highlight2html': custom_highlighter},
                            preprocessors=[SubCell] + preprocessors)

    config.CSSHTMLHeaderPreprocessor.highlight_class = " .highlight pre "
    content, info = exporter.from_filename(filepath)
    from bs4 import NavigableString

    if BeautifulSoup:
        soup = BeautifulSoup(content, 'html.parser')
        for i in soup.findAll('div', {'class': 'prompt input_prompt'}):
            i.decompose()
        for i in soup.findAll('div', {'class': 'prompt output_prompt'}):
            i.decompose()
        for i in soup.findAll('div', {'class': 'prompt'}):
            i.decompose()
        for i in soup.findAll('a', {'class': 'anchor-link'}):
            i.decompose()

        for i in soup.findAll('code'):
            i.attrs['class'] = 'code-class'
        content = soup.decode(formatter=None)
        #     url = 'http://localhost:8800/list-comprehension.html'
        #     page = requests.get(url)
        #     soup = BeautifulSoup(page.content, 'html.parser')

        soup = BeautifulSoup(content, 'html.parser')
        pre_tags = soup.find_all('div', {'class': 'input_area'})
        input_areas = [i for i in pre_tags if i['class'][0] == 'input_area']
        output = '\r\n'.join([i.get_text() for i in input_areas])
        new_div = soup.new_tag('textarea')
        new_div['text'] = output
        new_div['id'] = "myInput"
        new_div['type'] = "text"
        new_div['class'] = "codecopy"
        new_div.insert(0, NavigableString(output))

        soup.insert(0, new_div)
        content = soup.decode(formatter=None)
    return content, info
Exemple #34
0
from nbconvert.exporters import HTMLExporter
html_exporter = HTMLExporter()

## Create an HTML report from a Jupyter notebook
contents = html_exporter.from_filename('Exercise2pt5.ipynb')[0]

from pathlib import Path

Path('Exercise2pt5.html').write_text(contents)
Exemple #35
0
def nb2html(nb_path,
            execute=False,
            kernel_name="",
            theme=None,
            start=0,
            end=None):
    """
    Convert a notebook to HTML

    Arguments
    ---------
        nb_path: str
            Path to the notebook
        execute: bool
            Whether to execute the notebook
        kernel_name: str
            Name of the kernel to use
        theme: str
            Name of the theme to use (default: light) (options: light or dark)
        start: int
            Start cell number
        end: int
            End cell number

    Returns
    -------
        HTML content
    """
    logger.info(f"Converting notebook (execute={execute}): {nb_path}")

    global cell_id, kernel_lang
    cell_id = 0  # Reset the cell id

    app = get_nbconvert_app(execute=execute,
                            kernel_name=kernel_name,
                            start=start,
                            end=end)

    # Use the templates included in this package
    template_file = "mkdocs_html/notebook.html.j2"
    extra_template_paths = [settings.templates_dir]

    # Customize NBConvert App
    preprocessors_ = [SubCell]
    filters = {
        "highlight_code": custom_highlight_code,
        "markdown2html": custom_markdown2html,
    }

    exporter = HTMLExporter(
        config=app.config,
        template_file=template_file,
        extra_template_paths=extra_template_paths,
        preprocessors=preprocessors_,
        filters=filters,
        theme=theme,
    )

    _, extension = os.path.splitext(nb_path)

    if extension == ".py":
        nb = jupytext.read(nb_path)
        nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb"))
        content, resources = exporter.from_file(nb_file)
    else:
        try:
            with open(nb_path, "r", encoding="utf-8") as f:
                nb_json = json.load(f)
                kernel_lang = nb_json["metadata"]["kernelspec"]["language"]
        except KeyError:
            pass
        content, resources = exporter.from_filename(nb_path)

    return content
Exemple #36
0
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)
        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)['metadata']

            # Fix metadata to pelican standards
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
        metadata['ipython'] = True
        # Convert ipython notebook to html
        config = Config({
            'CSSHTMLHeaderTransformer': {
                'enabled': True,
                'highlight_class': '.highlight-ipynb'
            }
        })
        exporter = HTMLExporter(config=config,
                                template_file='basic',
                                filters={'highlight2html': custom_highlighter})

        content, info = exporter.from_filename(filepath)

        if BeautifulSoup:
            soup = BeautifulSoup(content)
            for i in soup.findAll("div", {"class": "input"}):
                if i.findChildren()[1].find(text='#ignore') is not None:
                    i.extract()
        else:
            soup = content

        # Process using Pelican HTMLReader
        content = '<body>{0}</body>'.format(
            soup)  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        if ('IPYNB_USE_META_SUMMARY' in self.settings.keys() and \
          self.settings['IPYNB_USE_META_SUMMARY'] == False) or \
          'IPYNB_USE_META_SUMMARY' not in self.settings.keys():
            metadata['summary'] = parser.summary

        def filter_css(style_text):
            '''
            HACK: IPython returns a lot of CSS including its own bootstrap.
            Get only the IPython Notebook CSS styles.
            '''
            index = style_text.find('/*!\n*\n* IPython notebook\n*\n*/')
            if index > 0:
                style_text = style_text[index:]
            index = style_text.find('/*!\n*\n* IPython notebook webapp\n*\n*/')
            if index > 0:
                style_text = style_text[:index]

            style_text = re.sub(r'color\:\#0+(;)?', '', style_text)
            style_text = re.sub(
                r'\.rendered_html[a-z0-9,._ ]*\{[a-z0-9:;%.#\-\s\n]+\}', '',
                style_text)

            return '<style type=\"text/css\">{0}</style>'.format(style_text)

        ipython_css = '\n'.join(
            filter_css(css_style) for css_style in info['inlining']['css'])
        body = ipython_css + body + LATEX_CUSTOM_SCRIPT

        return body, metadata
Exemple #37
0
# c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell",)
# c.TagRemovePreprocessor.remove_all_outputs_tags = ('remove_output',)
c.TagRemovePreprocessor.remove_input_tags = ('imports','css-html', 'comments')

c.ExecutePrepocessor.enabled = True
c.HTMLExporter.exclude_input_prompt = True #False

c.HTMLExporter.theme = 'dark'

# Configure and run out exporter
# c.preprocessors = ["TagRemovePreprocessor"]

exporter = HTMLExporter(config=c)
# exporter.register_preprocessor(TagRemovePreprocessor(config=c),True)


(body, resources) = exporter.from_filename("halo_centric/delta_stack.ipynb")

with open('halo_centric/delta_stack.html', 'w') as output_html:
    output_html.write(body)

(body, resources) = exporter.from_filename("misc/accretion_rate_hist.ipynb")

with open('misc/accretion_rate_hist.html', 'w') as output_html:
    output_html.write(body)

(body, resources) = exporter.from_filename("halo_centric/velocity_stack.ipynb")

with open('halo_centric/velocity_stack.html', 'w') as output_html:
    output_html.write(body)