def export_pdf(nbpath, template=None):
    from nbconvert.nbconvertapp import NbConvertApp
    if template is None:
        template = os.environ.get(ENV_VARS['export_pdf'],
                                  'hide_code_cells_pdf')
    NbConvertApp.launch_instance(
        [nbpath, '--template', template, '--to', 'pdf'])
Esempio n. 2
0
def get_nbconvert_app(execute=False,
                      kernel_name="",
                      start=0,
                      end=None) -> NbConvertApp:
    """Create"""

    # 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,
            "allow_errors": True,
        },
    })

    return app
Esempio n. 3
0
    def test_notebook(self):
        src = os.path.join(os.pardir, 'examples',
                           'pythreejs test scenes.ipynb')
        fname = get_fname('pythreejs_test_scenes.html')

        NbConvertApp.launch_instance(
            argv=['--execute', '--to', 'html', '--output', fname, src])
def export_pdf(nbpath, template=None):
    from nbconvert.nbconvertapp import NbConvertApp

    if template is None:
        template = os.environ.get(ENV_VARS["export_pdf"],
                                  "hide_code_cells_pdf")
    NbConvertApp.launch_instance(
        [nbpath, "--template", template, "--to", "pdf"])
Esempio n. 5
0
def _get_config(default=None):
    """Load and return the user's nbconvert configuration
    """
    config = Config(default) if default is not None else Config()
    app = NbConvertApp()
    app.load_config_file()
    _update_config(config, app.config)
    return config
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
Esempio n. 7
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 notebook(request):
    """
    Example on how Jupyter Notebook can be converted to HTML and returned
    from a Django view
    """
    exporter = HTMLExporter(exclude_input=True, exclude_output_prompt=True)
    writer = StringWriter()
    app = NbConvertApp(writer=writer, exporter=exporter)
    app.convert_single_notebook('./notebooks/sample.ipynb')
    return HttpResponse(writer.content)
Esempio n. 9
0
def export_notebook(notebook_path: Path, config: NbAutoexportConfig):
    """Export a given notebook file given configuration.

    Args:
        notebook_path (Path): path to notebook to export with nbconvert
        config (NbAutoexportConfig): configuration
    """
    logger.info(f"nbautoexport | Exporting {notebook_path} ...")
    logger.debug(
        f"nbautoexport | Using export configuration:\n{config.json(indent=2)}")
    with cleared_argv():
        converter = NbConvertApp()
        converter.log.handlers = logger.handlers
        converter.log.setLevel(logger.level)

        for export_format in config.export_formats:
            if config.organize_by == "notebook":
                subfolder = notebook_path.stem
            elif config.organize_by == "extension":
                subfolder = export_format.value

            converter.postprocessor = CopyToSubfolderPostProcessor(
                subfolder=subfolder, export_format=export_format)
            converter.export_format = export_format.value
            converter.initialize()
            converter.notebooks = [str(notebook_path)]
            converter.convert_notebooks()
Esempio n. 10
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)
Esempio n. 11
0
def notebooks_to_rst(app):
    from glob import glob

    # post "big-split", nbconvert is a separate namespace
    from nbconvert.nbconvertapp import NbConvertApp
    from nbconvert.writers import FilesWriter

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        os.chdir(os.path.join(srcdir, 'stginga', 'notebooks'))
        nbs = glob('*.ipynb')

        app = NbConvertApp()
        app.initialize(argv=[])
        app.writer = OrphanizerWriter()

        app.export_format = 'rst'
        app.notebooks = nbs

        app.start()
    except:
        pass
    finally:
        os.chdir(olddir)
Esempio n. 12
0
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
Esempio n. 13
0
def convert_tour_nb_to_document():
    app = NbConvertApp()
    app.initialize()
    app.notebooks = ['../Skills-ML Tour.ipynb']
    app.export_format = 'markdown'
    app.output_base = 'skills_ml_tour.md'
    app.writer.build_directory = 'sources/'
    app.convert_notebooks()
Esempio n. 14
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
    except ImportError:
        from IPython.nbconvert.nbconvertapp import NbConvertApp
        from IPython.nbconvert.writers import FilesWriter

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        os.chdir(os.path.join(srcdir, 'astroimtools', 'notebooks'))
        nbs = glob('*.ipynb')

        app = NbConvertApp()
        app.initialize(argv=[])
        app.writer = OrphanizerWriter()

        app.export_format = 'rst'
        app.notebooks = nbs

        app.start()
    except:
        pass
    finally:
        os.chdir(olddir)
Esempio n. 15
0
def ipynb2html(message: dict) -> dict:
    """convert jupyter notebook

    TODO: make this work from stdin

    Args:
        content: the notebook contents to convert

    Returns:
        html: str: the html representation for the requested jupyter notebook file.

    Note:
        this function requires nbconvert

    """
    from nbconvert.nbconvertapp import NbConvertApp
    from nbconvert.exporters.html import HTMLExporter

    # create an NbConvertApp:
    app = NbConvertApp.instance()
    # initialize the app with the arguments
    app.initialize(["--template=basic"])
    # create an exporter
    app.exporter = HTMLExporter(config=app.config)
    # get html output
    message["content"], _ = app.export_single_notebook(
        notebook_filename=None,
        resources=None,
        input_buffer=io.StringIO(message["content"]),
    )
    message["encoding"] = "html"
    message = filter_urls(message)
    return message
Esempio n. 16
0
    def from_notebook_node(self, nb, resources=None, **kw):
        # track conversion time.
        start_time = time()

        # Preprocess the notebook
        nb, resources = self._preprocess(nb, resources)

        # nbconvert options embedded in the metadata as a dict or a list.
        convert = nb.metadata.pop('nbconvert', {})
        if isinstance(convert, dict):
            convert = convert.items()

        for exporter, config in convert:
            app = NbConvertApp(config=Config(config))
            app.initialize([])
            app.exporter = get_exporter(exporter)(config=app.config)
            app.convert_single_notebook(self.filename)

        # How long did the conversions take?
        if convert:
            app.log.info(
                "Conversions completed in {} seconds.".format(time() -
                                                              start_time))

        return
Esempio n. 17
0
def run_quality_report(cik: str,
                       regen: bool = False,
                       convert_to_html: bool = False):
    nb_file = f'../notebooks/reports/{cik}.ipynb'
    if not regen and Path(nb_file).exists():
        logger.info(f"Notebook exists for {cik}. Skipping.")
        return

    pm.execute_notebook('resources/edgar_prelim_quality.ipynb',
                        nb_file,
                        parameters={'cik': cik})

    if convert_to_html:
        args = [
            "--Application.log_level=ERROR",
            "--TemplateExporter.exclude_input=True",
            "--output-dir=../out/notebooks", nb_file
        ]
        NbConvertApp.launch_instance(args)
Esempio n. 18
0
def export_notebook(notebook_path: Path, config: NbAutoexportConfig):
    """Export a given notebook file given configuration.

    Args:
        notebook_path (Path): path to notebook to export with nbconvert
        config (NbAutoexportConfig): configuration
    """
    with cleared_argv():
        converter = NbConvertApp()

        for export_format in config.export_formats:
            if config.organize_by == "notebook":
                subfolder = notebook_path.stem
            elif config.organize_by == "extension":
                subfolder = export_format.value

            converter.postprocessor = CopyToSubfolderPostProcessor(
                subfolder=subfolder, export_format=export_format)
            converter.export_format = export_format.value
            converter.initialize()
            converter.notebooks = [str(notebook_path)]
            converter.convert_notebooks()
Esempio n. 19
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='''
        Make (and run) a jupyter notebook visualizing the results of an output
        file from fixed_run.py.'''.strip())
    parser.add_argument('fname')
    parser.add_argument('--key', default='df')
    parser.add_argument('out_name', nargs='?')
    parser.add_argument('-n', type=int)
    parser.add_argument('--level', type=float, default=.1)
    parser.add_argument('--kernel-name')
    parser.add_argument('--force', '-f', action='store_true')
    args = parser.parse_args()

    if args.out_name is None:
        assert args.fname.endswith('.h5')
        base = args.fname[:-3]
        args.out_name = base + '.ipynb'
    else:
        d = os.path.dirname(args.out_name)
        if d and not os.path.isdir(d):
            os.makedirs(d)

    if not args.force and os.path.exists(args.out_name):
        parser.exit(
            "Output {} already exists; use --force to overwrite.".format(
                args.out_name))
    del args.force

    if args.n is None:
        match = re.search('/n(\d+)\.h5$', args.fname)
        if match:
            args.n = int(match.group(1))
        else:
            parser.error('-n must be specified unless filename has it')

    make_notebook(**vars(args))
    nbc = NbConvertApp()
    nbc.initialize([
        '--to=notebook', '--execute', '--output',
        os.path.basename(args.out_name), args.out_name
    ])
    nbc.convert_notebooks()
Esempio n. 20
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
        from nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
        from nbconvert.exporters import RSTExporter
        from nbformat import NotebookNode
    except ImportError:
        try:
            from IPython.nbconvert.nbconvertapp import NbConvertApp
            from IPython.nbconvert.writers import FilesWriter
            from IPython.nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
            from IPython.nbconvert.exporters import RSTExporter
            from IPython.nbformat import NotebookNode
        except ImportError:
            raise ImportError(
                'Failed to find Jupyter or IPython. Cannot build '
                'the notebooks embedded in the docs. Proceeding '
                'the rest of the doc build, but additional '
                'warnings are likely.')
            return

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ':orphan:\n\n' + output
            FilesWriter.write(self, output, resources, **kwargs)

    class AddSysPath(Preprocessor):
        """
        Adds the local system path to the top of the notebook.  This makes sure
        when build_sphinx is invoked that the notebook actually runs with the
        current build.
        """
        def preprocess(self, nb, resources):
            syspathstr = 'sys.path = {} + sys.path'.format(str(sys.path))
            cell = {
                'cell_type': 'code',
                'execution_count': None,
                'metadata': {},
                'outputs': [],
                'source': 'import sys\n' + syspathstr
            }
            nb.cells.insert(0, NotebookNode(cell))
            return nb, resources

    class RemoveSysPath(Preprocessor):
        """
        Removes the sys.path cell added by AddSysPath
        """
        def preprocess(self, nb, resources):
            if 'sys.path' in nb.cells[0].source:
                del nb.cells[0]
            return nb, resources

    class MonkeypatchCellExecutionError(execute.CellExecutionError):
        def __str__(self):
            sstr = super(MonkeypatchCellExecutionError, self).__str__()
            return sstr + ' Traceback:\n' + str(self.traceback)

    execute.CellExecutionError = MonkeypatchCellExecutionError

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        if os.path.isdir('notebooks'):
            os.chdir(os.path.join(srcdir, 'notebooks'))
            nbs = glob('*.ipynb')

            app.info(
                "Executing and converting these notebooks to sphinx files: " +
                str(nbs))

            nbc_app = NbConvertApp()
            nbc_app.initialize(argv=[])

            nbc_app.writer = OrphanizerWriter()

            nbc_app.export_format = 'rst'

            pps = RSTExporter().default_preprocessors
            pps.insert(0, AddSysPath)
            pps.append(RemoveSysPath)
            nbc_app.config.RSTExporter.preprocessors = pps

            nbc_app.notebooks = nbs

            nbc_app.start()
        else:
            app.info(
                'No notebook directory found in docs so not converting any notebooks.'
            )
    except:
        e = sys.exc_info()[0]
        app.warn('Failed to convert notebooks to RST (see above): ' + str(e))
    finally:
        os.chdir(olddir)
Esempio n. 21
0
def main():
    p = ArgumentParser()
    p.add_argument("ipynb_notebook_file")
    p.add_argument("-o", "--out")
    p.add_argument("-d", "--default-slide-type", default="skip")
    p.add_argument("--include-input-prompt", action="store_true")
    p.add_argument("--include-output-prompt", action="store_true")
    args = p.parse_args()
    # https://nbconvert.readthedocs.io/en/latest/config_options.html?highlight=TemplateExporter.exclude
    TemplateExporter.exclude_input_prompt = not args.include_input_prompt
    TemplateExporter.exclude_output_prompt = not args.include_output_prompt

    with open(args.ipynb_notebook_file) as nb, FakeSysArgv(
    ), tempfile.NamedTemporaryFile(suffix=".ipynb", mode="w") as out_nb:
        nb_content = json.load(nb)
        for i, cell in enumerate(nb_content["cells"]):
            nb_content["cells"][i].setdefault("metadata", {})
            nb_content["cells"][i]["metadata"].setdefault("slideshow", {})
            nb_content["cells"][i]["metadata"].setdefault("tags", [])
            if cell["cell_type"] == "markdown":
                regex = MARKDOWN_CODE_REGEX
            elif cell["cell_type"] == "code":
                regex = PY_CODE_REGEX
            slide_type = args.default_slide_type
            tags = []
            # check the first line
            if not cell["source"]:
                continue
            m = regex.match(cell["source"][0])
            if m:
                slideshow_config = m.group(1)
                slideshow_config_items = slideshow_config.split()
                slide_type = slideshow_config_items[0]
                if slide_type not in ALLOWED_SLIDE_TYPES:
                    raise ValueError(
                        "unknown slide type: {}".format(slide_type))
                # find tags in format "tags=tag1,tag2,..."
                for item in slideshow_config_items:
                    if not item.startswith("tags="):
                        continue
                    item_tags = item[len("tags="):].split(",")
                    for tag in item_tags:
                        # add only new tags, just in case
                        if tag not in cell["metadata"].get("tags", []):
                            tags.append(tag)
                nb_content["cells"][i]["source"] = cell["source"][
                    1:]  # remove the first line

            nb_content["cells"][i]["metadata"]["slideshow"][
                "slide_type"] = slide_type
            nb_content["cells"][i]["metadata"]["tags"] += tags
        json.dump(nb_content, out_nb)
        out_nb.flush()
        converter = NbConvertApp()
        converter.notebooks = [out_nb.name]
        converter.export_format = "slides"
        if not args.out:
            converter.postprocessor_class = "serve"
        converter.initialize()
        converter.convert_notebooks()

        if args.out:
            base = os.path.splitext(out_nb.name)[0]
            shutil.copy(base + ".slides.html", args.out)
Esempio n. 22
0
def get_config():
    """Load and return the user's nbconvert configuration
    """
    app = NbConvertApp()
    app.load_config_file()
    return app.config
Esempio n. 23
0
def convert_notebook(src_path, output_img_dir):
    app = NbConvertApp()
    app.output_files_dir = output_img_dir
    app.writer = FilesWriter()
    app.exporter = MarkdownExporter()
    app.convert_single_notebook(src_path)
Esempio n. 24
0
def convert_notebook(src_path):
    app = NbConvertApp()
    app.output_files_dir = './stage/img'
    app.writer = FilesWriter()
    app.exporter = MarkdownExporter()
    app.convert_single_notebook(src_path)
Esempio n. 25
0
def get_config():
    """Load and return the user's nbconvert configuration
    """
    app = NbConvertApp()
    app.load_config_file()
    return app.config
and the HTMLExporter both inherit from TemplateExporter. With the following config

.. code-block:: python

    c.TemplateExporter.exclude_input_prompt = False # The default
    c.PDFExporter.exclude_input_prompt = True

input prompts will not appear when converting to PDF, but they will appear when
exporting to HTML.

CLI Flags and Aliases
---------------------

When using Nbconvert from the command line, a number of aliases and flags are
defined as shortcuts to configuration options for convience.

"""

try:
    indir = os.path.dirname(__file__)
except NameError:
    indir = os.path.dirname(os.getcwd())
destination = os.path.join(indir, 'source/config_options.rst')

with open(destination, 'w') as f:
    app = NbConvertApp()
    f.write(header)
    f.write(app.document_flag_help())
    f.write(app.document_alias_help())
    f.write(app.document_config_options())
Esempio n. 27
0
def export_html(nbpath, template=_html_no_code_template):
    NbConvertApp.launch_instance([nbpath, '--template', template, '--to', 'html'])
Esempio n. 28
0
def export_pdf(nbpath, template=_pdf_no_code_template):
    NbConvertApp.launch_instance([nbpath, '--template', template, '--to', 'pdf'])
Esempio n. 29
0
Create config_options.rst, a Sphinx documentation source file.
Documents the options that may be set in nbconvert's configuration file,
jupyter_nbconvert_config.py.

"""
import os.path
from nbconvert.nbconvertapp import NbConvertApp

header = """\

.. This is an automatically generated file.
.. do not modify by hand.

Configuration options
=====================

Configuration options may be set in a file, ``~/.jupyter/jupyter_nbconvert_config.py``,
or at the command line when starting nbconvert, i.e. ``jupyter nbconvert --Application.log_level=10``.
"""

try:
    indir = os.path.dirname(__file__)
except NameError:
    indir = os.path.dirname(os.getcwd())
destination = os.path.join(indir, 'source/config_options.rst')

with open(destination, 'w') as f:
    f.write(header)
    f.write(NbConvertApp().document_config_options())
Esempio n. 30
0
def notebooks_to_rst(app):
    from glob import glob

    try:
        # post "big-split", nbconvert is a separate namespace
        from nbconvert.nbconvertapp import NbConvertApp
        from nbconvert.writers import FilesWriter
        from nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
        from nbconvert.exporters import RSTExporter
        from nbformat import NotebookNode
    except ImportError:
        try:
            from IPython.nbconvert.nbconvertapp import NbConvertApp
            from IPython.nbconvert.writers import FilesWriter
            from IPython.nbconvert.preprocessors import Preprocessor, ExecutePreprocessor, execute
            from IPython.nbconvert.exporters import RSTExporter
            from IPython.nbformat import NotebookNode
        except ImportError:
            raise ImportError(
                "Failed to find Jupyter or IPython. Cannot build "
                "the notebooks embedded in the docs. Proceeding "
                "the rest of the doc build, but additional "
                "warnings are likely."
            )
            return

    class OrphanizerWriter(FilesWriter):
        def write(self, output, resources, **kwargs):
            output = ":orphan:\n\n" + output
            FilesWriter.write(self, output, resources, **kwargs)

    class AddSysPath(Preprocessor):
        """
        Adds the local system path to the top of the notebook.  This makes sure
        when build_sphinx is invoked that the notebook actually runs with the
        current build.
        """

        def preprocess(self, nb, resources):
            syspathstr = "sys.path = {} + sys.path".format(str(sys.path))
            cell = {
                "cell_type": "code",
                "execution_count": None,
                "metadata": {},
                "outputs": [],
                "source": "import sys\n" + syspathstr,
            }
            nb.cells.insert(0, NotebookNode(cell))
            return nb, resources

    class RemoveSysPath(Preprocessor):
        """
        Removes the sys.path cell added by AddSysPath
        """

        def preprocess(self, nb, resources):
            if "sys.path" in nb.cells[0].source:
                del nb.cells[0]
            return nb, resources

    class MonkeypatchCellExecutionError(execute.CellExecutionError):
        def __str__(self):
            sstr = super(MonkeypatchCellExecutionError, self).__str__()
            return sstr + " Traceback:\n" + str(self.traceback)

    execute.CellExecutionError = MonkeypatchCellExecutionError

    olddir = os.path.abspath(os.curdir)
    try:
        srcdir = os.path.abspath(os.path.split(__file__)[0])
        if os.path.isdir("notebooks"):
            os.chdir(os.path.join(srcdir, "notebooks"))
            nbs = glob("*.ipynb")

            app.info("Executing and converting these notebooks to sphinx files: " + str(nbs))

            nbc_app = NbConvertApp()
            nbc_app.initialize(argv=[])

            nbc_app.writer = OrphanizerWriter()

            nbc_app.export_format = "rst"

            pps = RSTExporter().default_preprocessors
            pps.insert(0, AddSysPath)
            pps.append(RemoveSysPath)
            nbc_app.config.RSTExporter.preprocessors = pps

            nbc_app.notebooks = nbs

            nbc_app.start()
        else:
            app.info("No notebook directory found in docs so not converting any notebooks.")
    except:
        e = sys.exc_info()[0]
        app.warn("Failed to convert notebooks to RST (see above): " + str(e))
    finally:
        os.chdir(olddir)
Esempio n. 31
0
def post_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py scripts and html
       in a separate folder with the same name
    """
    # only do this for notebooks
    if model['type'] != 'notebook':
        return

    # only do this if we've added the special indicator file to the working directory
    cwd = os.path.dirname(os_path)
    save_procress_indicator = os.path.join(cwd, SAVE_PROCRESS_INDICATOR_FILE)
    should_convert = os.path.exists(save_procress_indicator)

    if should_convert:
        d, fname = os.path.split(os_path)
        subfolder = os.path.splitext(fname)[0]

        converter = NbConvertApp()
        converter.postprocessor = CopyToSubfolderPostProcessor(subfolder=subfolder)

        converter.export_format = 'script'
        converter.initialize(argv=[])
        converter.notebooks = [os_path]
        converter.convert_notebooks()

        converter.export_format = 'html'
        converter.initialize(argv=[])
        converter.notebooks = [os_path]
        converter.convert_notebooks()
Esempio n. 32
0
                                pass
                            else:
                                td.string = format_number(val, 3)
                        output.data['text/html'] = str(soup)

        try:
            nb.metadata.notebook
        except AttributeError:
            nb.metadata.notebook = notebook_name

        md_writer.files = files

        return nb, resources


app = NbConvertApp(output_base=output_name)
md_exporter = MarkdownExporter(template_file='./index.md.j2',
                               preprocessors=[CustomPreprocess])
md_writer = FilesWriter(build_directory=output_dir)
app.exporter = md_exporter
app.writer = md_writer
app.convert_single_notebook(notebook)

if len(md_writer.files) > 0:
    rootLogger.info("Collating files...")
    for file in md_writer.files:
        src = normpath(join(output_dir, file))
        dst = join(output_dir, output_name + '_files', basename(file))
        rename(src, dst)
        rootLogger.info("Moving '{}'".format(src))
        rootLogger.info("to '{}'".format(dst))
Esempio n. 33
0
def write_nb_to_html(filename):
    nbc = NbConvertApp()
    nbc.exporter = HTMLExporter()
    nbc.writer = FilesWriter()
    nbc.convert_single_notebook(filename)