コード例 #1
0
ファイル: utils.py プロジェクト: jasonyzhang/gsExport
def export_notebook(nb, name, templating="test.tplx", debug=False):
    shutil.copyfile(pkg_resources.resource_filename(__name__, templating),
                    "test.tplx")
    pdf_exporter = PDFExporter()
    pdf_exporter.template_file = "test.tplx"
    print("Attempting to compile LaTeX")
    try:
        pdf_output = pdf_exporter.from_notebook_node(nb)
        with open("%s.pdf" % name, "wb") as output_file:
            output_file.write(pdf_output[0])
            print("Finished generating PDF")
    except nbconvert.pdf.LatexFailed as error:
        print("There was an error generating your LaTeX")
        output = error.output
        if debug:
            print("Showing full error message from PDFTex")

        else:
            print("Showing concise error message")
            output = "\n".join(error.output.split("\n")[-15:])
        print("=" * 30)
        print(output)
        print("=" * 30)
        return None
    return "%s.pdf" % name
コード例 #2
0
    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()
コード例 #3
0
ファイル: to_pdf.py プロジェクト: azmatsiddique/jassign
def export_notebook(nb, pdf_path, template="test.tplx", debug=False):
    """Write notebook as PDF to pdf_path. Return True on success or False on error."""
    shutil.copyfile(pkg_resources.resource_filename(__name__, template), "test.tplx")
    pdf_exporter = PDFExporter()
    pdf_exporter.template_file = "test.tplx"
    try:
        pdf_output = pdf_exporter.from_notebook_node(nb)
        with open(pdf_path, "wb") as out:
            out.write(pdf_output[0])
            print("Saved", pdf_path)
        return True
    except nbconvert.pdf.LatexFailed as error:
        print("There was an error generating your LaTeX")
        output = error.output
        if not debug:
            print("To see the full error message, run with debug=True")
            output = "\n".join(error.output.split("\n")[-15:])
        print("=" * 30)
        print(output)
        print("=" * 30)
        return False