Exemplo n.º 1
0
 def get_script_extension(self):
     # Match logic of nbconvert.exporters.script.ScriptExporter
     # Order of precedence is: nb_convert_exporter, language, file_extension, .txt
     lang_info = self.metadata.get("language_info", {})
     if "nbconvert_exporter" in lang_info:
         return get_exporter(lang_info.nbconvert_exporter)().file_extension
     if "name" in lang_info and lang_info.name in get_export_names():
         return get_exporter(lang_info.name)().file_extension
     return lang_info.get("file_extension", ".txt")
Exemplo n.º 2
0
def export_notebooks(path_to_notes: str = None,
                     export_path: str = None,
                     exporter_name: str = None,
                     exporter_args: dict = None) -> None:
    """
    Export notebooks via nbconvert.

    It reads all the indexed notebooks in `path_to_notes` and export them
    to the directory `export_path` using the exporter defined by
    `exporter_name`, with the arguments in `exporter_args`.

    The name of the exporter (`exporter_name`) must be one of the default
    exporters listed in `nbconvert.exporters.get_export_names()`.

    Parameters
    ----------
    path_to_notes : str
        The path to the directory that contains the notebooks, either
        absolute or relative to the script that calls `nbbinder.bind()`.

    export_path : str
        The path to the directory where the exported, or converted,
        files should be saved in.

    exporter_name : str
        The name of the exporter to be used in `nbconvert` via
        `nbconvert.exporters.get_exporter(exporter_name)`. Possible
        choices are 'markdown', 'pdf', 'slides', 'latex', etc.

    exporter_args : dict
        Arguments, if any, to be passed on to the exporter via
        `nbconvert.exporters.get_exporter(exporter_name)(**exporter_args)`.
    """
    assert isinstance(export_path, str), \
        "Argument `export_path` should be a string"
    assert exporter_name in exporters.get_export_names(), \
        "The `exporter_name` argument with value {} is not in the \
list of available exporters listed in \
`nbconvert.exporters.get_export_names()`".format(exporter_name)

    if os.path.isdir(export_path):
        for file in os.listdir(export_path):
            os.remove(os.path.join(export_path, file))
    else:
        os.mkdir(export_path)

    if exporter_args:
        exporter = exporters.get_exporter(exporter_name)(**exporter_args)
    else:
        exporter = exporters.get_exporter(exporter_name)()
    extension = exporter.file_extension

    for nb_name in indexed_notebooks(path_to_notes):
        nb_file = os.path.join(path_to_notes, nb_name)
        nb = nbformat.read(nb_file, as_version=4)
        body = exporter.from_notebook_node(nb)[0]
        for cell in nb.cells:
            for marker in (NAVIGATOR_MARKER, TOC_MARKER):
                if cell.source.startswith(marker):
                    source_new = ''
                    i = 0
                    for m in REG_LINK.finditer(cell.source):
                        source_new += cell.source[i:m.start(6)] + extension
                        i = m.end(6)
                    source_new += cell.source[i:]
                    cell.source = source_new

        LOGGER.info("Adjusting links for %s", export_path)
        body = exporter.from_notebook_node(nb)[0]
        export_filename = \
            os.path.join(export_path,
                         nb_name[:REG.match(nb_name).start(5)]
                         + extension)
        if isinstance(body, str):
            export_file = open(export_filename, 'w+')
        else:
            export_file = open(export_filename, 'wb+')
        export_file.write(body)
        export_file.close()
Exemplo n.º 3
0
def test_export_format_compatibility():
    """Test that export formats are compatible with Jupyter nbautoconvert.
    """
    nbconvert_export_names = get_export_names()
    for export_format in ExportFormat:
        assert export_format.value in nbconvert_export_names