Python nbconvert.HTMLExporter() Examples

The following are 18 code examples of nbconvert.HTMLExporter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module nbconvert , or try the search function .
Example #1
Source File: notebook_sphinxext.py    From msmexplorer with MIT License 6 votes vote down vote up
def export_html(nb, f):
    config = {
        'Exporter': {'template_file': 'embed',
                     'template_path': ['./sphinxext/']},
        'ExtractOutputPreprocessor': {'enabled': True},
        'CSSHTMLHeaderPreprocessor': {'enabled': True}
    }

    exporter = HTMLExporter(config)
    body, resources = exporter.from_notebook_node(
        nb, resources={'output_files_dir': f['nbname']})

    for fn, data in resources['outputs'].items():
        bfn = os.path.basename(fn)
        with open("{destdir}/{fn}".format(fn=bfn, **f), 'wb') as res_f:
            res_f.write(data)

    return body 
Example #2
Source File: notebook_sphinxext.py    From mdentropy with MIT License 6 votes vote down vote up
def export_html(nb, f):
    config = {
        'Exporter': {'template_file': 'embed',
                     'template_path': ['./sphinxext/']},
        'ExtractOutputPreprocessor': {'enabled': True},
        'CSSHTMLHeaderPreprocessor': {'enabled': True}
    }

    exporter = HTMLExporter(config)
    body, resources = exporter.from_notebook_node(
        nb, resources={'output_files_dir': f['nbname']})

    for fn, data in resources['outputs'].items():
        bfn = os.path.basename(fn)
        with open("{destdir}/{fn}".format(fn=bfn, **f), 'wb') as res_f:
            res_f.write(data)

    return body 
Example #3
Source File: notebook.py    From pynb with MIT License 6 votes vote down vote up
def export_html(self, pathname):
        """
        Export notebook to .html file
        :param pathname: output filename
        :return:
        """

        html_exporter = HTMLExporter()

        (body, resources) = html_exporter.from_notebook_node(self.nb)

        if pathname == '-':
            sys.__stdout__.write(body)
        else:
            with open(pathname, 'w') as f:
                f.write(body)

        logging.info("HTML notebook exported to '{}'".format(pathname)) 
Example #4
Source File: app.py    From dagster with Apache License 2.0 6 votes vote down vote up
def notebook_view(request_args):
    check.dict_param(request_args, 'request_args')

    # This currently provides open access to your file system - the very least we can
    # do is limit it to notebook files until we create a more permanent solution.
    path = request_args['path']
    if not path.endswith('.ipynb'):
        return 'Invalid Path', 400

    with open(os.path.abspath(path)) as f:
        read_data = f.read()
        notebook = nbformat.reads(read_data, as_version=4)
        html_exporter = HTMLExporter()
        html_exporter.template_file = 'basic'
        (body, resources) = html_exporter.from_notebook_node(notebook)
        return '<style>' + resources['inlining']['css'][0] + '</style>' + body, 200 
Example #5
Source File: exporter.py    From pipelines with Apache License 2.0 6 votes vote down vote up
def generate_html_from_notebook(self, nb: NotebookNode) -> Text:
        """Converts a provided NotebookNode to HTML.

        Args:
            nb: NotebookNode that should be converted to HTML.

        Returns:
            HTML from converted NotebookNode as a string.

        """
        # HTML generator and exporter object
        html_exporter = HTMLExporter()
        template_file = "templates/{}.tpl".format(self.template_type.value)
        html_exporter.template_file = str(Path.cwd() / template_file)
        # Output generator
        self.ep.preprocess(nb, {"metadata": {"path": Path.cwd()}}, self.km)
        # Export all html and outputs
        body, _ = html_exporter.from_notebook_node(nb, resources={})
        return body 
Example #6
Source File: views.py    From MPContribs with MIT License 6 votes vote down vote up
def export_notebook(nb, cid):
    nb = nbformat.from_dict(nb)
    html_exporter = HTMLExporter()
    html_exporter.template_file = "basic"
    body = html_exporter.from_notebook_node(nb)[0]
    soup = BeautifulSoup(body, "html.parser")
    # mark cells with special name for toggling, and
    # TODO make element id's unique by appending cid (for ingester)
    for div in soup.find_all("div", "output_wrapper"):
        script = div.find("script")
        if script:
            script = script.contents[0]
            if script.startswith("render_json"):
                div["name"] = "HData"
            elif script.startswith("render_table"):
                div["name"] = "Tables"
            elif script.startswith("render_plot"):
                div["name"] = "Graphs"
        else:
            pre = div.find("pre")
            if pre and pre.contents[0].startswith("Structure"):
                div["name"] = "Structures"
    # name divs for toggling code_cells
    for div in soup.find_all("div", "input"):
        div["name"] = "Code"
    # separate script
    script = []
    for s in soup.find_all("script"):
        script.append(s.string)
        s.extract()  # remove javascript
    return soup.prettify(), "\n".join(script) 
Example #7
Source File: convert.py    From sphinxcontrib-jupyter with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, builderSelf):
        
        self.htmldir = builderSelf.outdir + "/html" #html directory 

        for path in [self.htmldir]:
            ensuredir(path)
        self.html_exporter = HTMLExporter()
        
        templateFolder = builderSelf.config['jupyter_template_path']

        if os.path.exists(templateFolder):
            pass
        else:
            builderSelf.logger.warning("template directory not found")
            exit()

        self.html_exporter.template_file = templateFolder + "/" + builderSelf.config["jupyter_html_template"] 
Example #8
Source File: archive.py    From holoviews with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _generate_html(self, node, substitutions):  # pragma: no cover
        exporter = HTMLExporter()
        exporter.register_preprocessor(Substitute(self.nbversion,
                                                  substitutions))
        html,_ = exporter.from_notebook_node(node)
        return html 
Example #9
Source File: ipynb_to_html.py    From blogger-cli with MIT License 5 votes vote down vote up
def gen_exporter():
    config = TraitletsConfig()
    config.htmlexporter.preprocessors = [
        "nbconvert.preprocessors.extractoutputpreprocessor"
    ]
    html_exporter = HTMLExporter(config=config)
    html_exporter.template_file = "basic"
    return html_exporter 
Example #10
Source File: jupyter_utils.py    From jupyter-edx-viewer-xblock with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def convert_to_html(nb):
    """Converts notebook dict to HTML with included CSS"""
    exporter = HTMLExporter()
    body, resources = exporter.from_notebook_node(nb)

    return body, resources 
Example #11
Source File: iota_run_nb.py    From aws-iot-analytics-notebook-containers with Apache License 2.0 5 votes vote down vote up
def _write_output_to_s3(context):
    ipynb_json = nbformat.writes(context.nb)
    _write_to_s3(context.output_ipynb_s3_uri, ipynb_json, context.s3)
    basic_html, _ = HTMLExporter(template_file='basic').from_notebook_node(context.nb)
    _write_to_s3(context.output_html_s3_uri, basic_html, context.s3) 
Example #12
Source File: converters.py    From jupyter-docx-bundler with MIT License 5 votes vote down vote up
def notebook_to_html(content, htmlfile):
    """ Convert notebook to html file.

    Parameters
    ----------

    content : nbformat.NotebookNode
        A dict-like node of the notebook with attribute-access
    htmlfile : str
        Filename for the notebook exported as html
    """
    # prepare html exporter, anchor_link_text=' ' suppress anchors being shown
    html_exporter = HTMLExporter(
        anchor_link_text=' ', exclude_input_prompt=True, exclude_output_prompt=True
    )

    # save metadata for possible title removement
    metadata = content['metadata']

    # export to html
    content, _ = html_exporter.from_notebook_node(content)

    # check if export path exists
    if os.path.dirname(htmlfile) != '' and not os.path.isdir(os.path.dirname(htmlfile)):
        raise FileNotFoundError(f'Path to html-file does not exist: {os.path.dirname(htmlfile)}')

    # Remove title from htmlfile if none is set to prevent pandoc from writing one
    if 'title' not in metadata:
        content = remove_html_title(content)

    # write content to html file
    with open(htmlfile, 'w', encoding='utf-8') as file:
        file.write(content) 
Example #13
Source File: notebook_sphinxext.py    From deepchem with MIT License 5 votes vote down vote up
def export_html(nb, f):
  config = {
      'Exporter': {
          'template_file': 'basic',
          'template_path': ['./sphinxext/']
      },
      'ExtractOutputPreprocessor': {
          'enabled': True
      },
      'CSSHTMLHeaderPreprocessor': {
          'enabled': True
      }
  }

  exporter = HTMLExporter(config)
  body, resources = exporter.from_notebook_node(
      nb, resources={'output_files_dir': f['nbname']})

  for fn, data in resources['outputs'].items():
    bfn = os.path.basename(fn)
    with open("{destdir}/{fn}".format(fn=bfn, **f), 'wb') as res_f:
      res_f.write(data)

  return body 
Example #14
Source File: convert.py    From Jike-Metro with MIT License 5 votes vote down vote up
def convert():
    exporter = HTMLExporter()
    exporter.template_path = [os.path.join('docs', 'templates')]
    exporter.template_file = 'full'
    for source_ipynb_path in gen_notebook_path():
        arrange_notebook_execution_order(source_ipynb_path)
        _, filename = os.path.split(source_ipynb_path)

        body, _ = exporter.from_filename(source_ipynb_path)
        write_path = os.path.join('docs', filename.replace('ipynb', 'html'))
        with open(write_path, 'wt', encoding='utf-8') as f:
            f.write(body)
        print('{} write success.'.format(write_path)) 
Example #15
Source File: evaluation.py    From imageatm with Apache License 2.0 4 votes vote down vote up
def _create_report(self, report_kernel_name:str, report_export_html:bool, report_export_pdf:bool):
        """Creates report from notebook-template and stores it in different formats all figures.

            - Jupyter Notebook
            - HTML
            - PDF
        """
        assert not self.mode_ipython, 'Create report is only possible when not in ipython mode'

        filepath_template = dirname(imageatm.notebooks.__file__) + '/evaluation_template.ipynb'
        filepath_notebook = self.evaluation_dir / 'evaluation_report.ipynb'
        filepath_html = self.evaluation_dir / 'evaluation_report.html'
        filepath_pdf = self.evaluation_dir / 'evaluation_report.pdf'

        pm.execute_notebook(
            str(filepath_template),
            str(filepath_notebook),
            parameters=dict(
                image_dir=str(self.image_dir),
                job_dir=str(self.job_dir)
            ),
            kernel_name=report_kernel_name
        )

        with open(filepath_notebook) as f:
            nb = nbformat.read(f, as_version=4)

        if report_export_html:
            self.logger.info('\n****** Create HTML ******\n')
            with open(filepath_notebook) as f:
                nb = nbformat.read(f, as_version=4)

            html_exporter = HTMLExporter()
            html_data, resources = html_exporter.from_notebook_node(nb)

            with open(filepath_html, 'w') as f:
                f.write(html_data)
                f.close()

        if report_export_pdf:
            self.logger.info('\n****** Create PDF ******\n')

            pdf_exporter = PDFExporter()
            pdf_exporter.template_file = dirname(imageatm.notebooks.__file__) + '/tex_templates/evaluation_report.tplx'
            pdf_data, resources = pdf_exporter.from_notebook_node(nb, resources={
                'metadata': {'name': 'Evaluation Report'}
            })

            with open(filepath_pdf, 'wb') as f:
                f.write(pdf_data)
                f.close()

    # TO-DO: Enforce string or integer but not both at the same time 
Example #16
Source File: parse_tutorials.py    From captum with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def gen_tutorials(repo_dir: str) -> None:
    """Generate HTML tutorials for captum Docusaurus site from Jupyter notebooks.

    Also create ipynb and py versions of tutorial in Docusaurus site for
    download.
    """
    with open(os.path.join(repo_dir, "website", "tutorials.json"), "r") as infile:
        tutorial_config = json.loads(infile.read())

    tutorial_ids = {x["id"] for v in tutorial_config.values() for x in v}

    for tid in tutorial_ids:
        print("Generating {} tutorial".format(tid))

        # convert notebook to HTML
        ipynb_in_path = os.path.join(repo_dir, "tutorials", "{}.ipynb".format(tid))
        with open(ipynb_in_path, "r") as infile:
            nb_str = infile.read()
            nb = nbformat.reads(nb_str, nbformat.NO_CONVERT)

        # displayname is absent from notebook metadata
        nb["metadata"]["kernelspec"]["display_name"] = "python3"

        exporter = HTMLExporter()
        html, meta = exporter.from_notebook_node(nb)

        # pull out html div for notebook
        soup = BeautifulSoup(html, "html.parser")
        nb_meat = soup.find("div", {"id": "notebook-container"})
        del nb_meat.attrs["id"]
        nb_meat.attrs["class"] = ["notebook"]
        html_out = JS_SCRIPTS + str(nb_meat)

        # generate html file
        html_out_path = os.path.join(
            repo_dir, "website", "_tutorials", "{}.html".format(tid)
        )
        with open(html_out_path, "w") as html_outfile:
            html_outfile.write(html_out)

        # generate JS file
        script = TEMPLATE.format(tid)
        js_out_path = os.path.join(
            repo_dir, "website", "pages", "tutorials", "{}.js".format(tid)
        )
        with open(js_out_path, "w") as js_outfile:
            js_outfile.write(script)

        # output tutorial in both ipynb & py form
        ipynb_out_path = os.path.join(
            repo_dir, "website", "static", "files", "{}.ipynb".format(tid)
        )
        with open(ipynb_out_path, "w") as ipynb_outfile:
            ipynb_outfile.write(nb_str)
        exporter = ScriptExporter()
        script, meta = exporter.from_notebook_node(nb)
        py_out_path = os.path.join(
            repo_dir, "website", "static", "files", "{}.py".format(tid)
        )
        with open(py_out_path, "w") as py_outfile:
            py_outfile.write(script) 
Example #17
Source File: parse_tutorials.py    From ClassyVision with MIT License 4 votes vote down vote up
def gen_tutorials(repo_dir: str) -> None:
    """Generate HTML tutorials for captum Docusaurus site from Jupyter notebooks.

    Also create ipynb and py versions of tutorial in Docusaurus site for
    download.
    """
    with open(os.path.join(repo_dir, "website", "tutorials.json"), "r") as infile:
        tutorial_config = json.loads(infile.read())

    tutorial_ids = {x["id"] for v in tutorial_config.values() for x in v}

    for tid in tutorial_ids:
        print("Generating {} tutorial".format(tid))

        # convert notebook to HTML
        ipynb_in_path = os.path.join(repo_dir, "tutorials", "{}.ipynb".format(tid))
        with open(ipynb_in_path, "r") as infile:
            nb_str = infile.read()
            nb = nbformat.reads(nb_str, nbformat.NO_CONVERT)

        # displayname is absent from notebook metadata
        nb["metadata"]["kernelspec"]["display_name"] = "python3"

        exporter = HTMLExporter()
        html, meta = exporter.from_notebook_node(nb)

        # pull out html div for notebook
        soup = BeautifulSoup(html, "html.parser")
        nb_meat = soup.find("div", {"id": "notebook-container"})
        del nb_meat.attrs["id"]
        nb_meat.attrs["class"] = ["notebook"]
        html_out = JS_SCRIPTS + str(nb_meat)

        # generate html file
        html_out_path = os.path.join(
            repo_dir, "website", "_tutorials", "{}.html".format(tid)
        )
        with open(html_out_path, "w") as html_outfile:
            html_outfile.write(html_out)

        # generate JS file
        script = TEMPLATE.format(tid)
        js_out_path = os.path.join(
            repo_dir, "website", "pages", "tutorials", "{}.js".format(tid)
        )
        with open(js_out_path, "w") as js_outfile:
            js_outfile.write(script)

        # output tutorial in both ipynb & py form
        ipynb_out_path = os.path.join(
            repo_dir, "website", "static", "files", "{}.ipynb".format(tid)
        )
        with open(ipynb_out_path, "w") as ipynb_outfile:
            ipynb_outfile.write(nb_str)
        exporter = ScriptExporter()
        script, meta = exporter.from_notebook_node(nb)
        py_out_path = os.path.join(
            repo_dir, "website", "static", "files", "{}.py".format(tid)
        )
        with open(py_out_path, "w") as py_outfile:
            py_outfile.write(script) 
Example #18
Source File: files.py    From SlicerJupyter with MIT License 4 votes vote down vote up
def notebookExportToHtml(outputFilePath=None):
    """Export current notebook to HTML.
    If outputFilePath is not specified then filename will be generated from the notebook filename
    with current timestamp appended.
    It returns full path of the saved html file.
    It requires nbformat and nbconvert packages. You can use this command to install them:
        pip_install("nbformat nbconvert")
    """
    try:
      import nbformat
      from nbconvert import HTMLExporter
    except ModuleNotFoundError:
      import logging
      logging.error("notebookExportToHtml requires nbformat and nbconvert. They can be installed by running this command:\n\n    pip_install('nbformat nbconvert')\n")

    import datetime, json, os

    notebook_path = notebookPath()

    # Generate output file path from notebook name and timestamp (if not specified)
    if not outputFilePath:
      this_notebook_name = os.path.splitext(os.path.basename(notebook_path))[0]
      save_timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
      save_file_name = this_notebook_name + "_" + save_timestamp + ".html"
      notebooks_save_path = os.path.dirname(notebook_path)
      outputFilePath = os.path.join(notebooks_save_path, save_file_name)

    with open(notebook_path, mode="r") as f:
        file_json = json.load(f)
        
    notebook_content = nbformat.reads(json.dumps(file_json), as_version=4)

    html_exporter = HTMLExporter()
    (body, resources) = html_exporter.from_notebook_node(notebook_content)

    f = open(outputFilePath, 'wb')
    f.write(body.encode())
    f.close()

    return outputFilePath